【问题标题】:Android Studio :How to solve this execution errorAndroid Studio:如何解决这个执行错误
【发布时间】:2020-01-22 10:09:53
【问题描述】:

我是 android studio 的新手,我不明白为什么我的应用在开始使用搜索栏时会崩溃。 有人可以帮我吗?

公共类 MainActivity 扩展 AppCompatActivity { 搜索栏搜索栏; //声明搜索栏对象 文本视图文本视图,文本视图2; ListView 简单的Listview; //声明SeekBar的成员变量 整数离散; 诠释开始= 50; int start_position = 50; //进度跟踪器 国际温度 = 0; //为 ViewStub 声明对象 ViewStub 存根; 复选框复选框; 列表视图 lv; //声明Listview对象 按钮按钮; 查看视图;

public MainActivity() {
    discrete = 0;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //declare viewstub object
    stub = findViewById(R.id.viewStub1);
    @SuppressWarnings("unused")
    View inflated = stub.inflate();
    stub.setVisibility(View.INVISIBLE);
    //ViewStub logic
    checkBox = findViewById(R.id.checkBox1);
    //handle checkbox click event
    checkBox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
        public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
            if (isChecked) {
                //remove objs from parent view to allow for child view objs
                checkBox.setVisibility(View.GONE);
                seekBar.setVisibility(View.GONE);
                textView.setVisibility(View.GONE);
                stub.setVisibility(View.VISIBLE);

            }
        }
    });
    //seekbar logic
    textView = findViewById(R.id.textview);
    textView = findViewById(R.id.textView2);
    textView.setText(" Celsius at 0 degrees");



    //set default view
    seekBar = findViewById(R.id.seekbar);
    seekBar.setProgress(start_position);


    //create event handler for SeekBar
    seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

            if (temp == 0) {
                //for initial view result
                //Toast.makeText(getBaseContext(), "Fahrenheit result: 32 degrees",
                        //Toast.LENGTH_SHORT).show();
                textView2.setText("Fahrenheit result 32 degrees") ;

            }
            else{
                //Toast.makeText(getBaseContext(), "Fahrenheit result: "
                        //+ String.valueOf(discrete) +
                        //" degrees",Toast.LENGTH_SHORT).show();
                textView2.setText("Fahrenheit result:" + String.valueOf(discrete) + "degrees");
            }
        }
        @Override
        public void onStartTrackingTouch(SeekBar seekBar){ }

        @Override
        public void onProgressChanged(SeekBar seekBar,
                                      int progress, boolean fromUser) {
            // To convert progress passed as discrete (Fahrenheit) value
            temp = progress - start;
            discrete = (int) Math.round((((temp * 9.0) /
                    5.0) + 32));
            //convert C to F temp textView.setText(" Celsius at " + temp + " degrees");
            textView.setText(" Celsius at " + temp + " degrees");
        }

    });

【问题讨论】:

  • 能否分享一下错误日志

标签: java android


【解决方案1】:
textView = findViewById(R.id.textview);
textView = findViewById(R.id.textView2);

textView2 没有在这里初始化...

把上面的代码改成

textView = findViewById(R.id.textview);
textView2 = findViewById(R.id.textView2);

【讨论】:

    【解决方案2】:

    TextView textView = findViewById(R.id.textview); TextView textView2 = findViewById(R.id.textView2);

    【讨论】:

      【解决方案3】:

      初始化 textView2 你似乎错过了那部分

      【讨论】:

      • 谢谢(抱歉回复晚了,上次没有上传)
      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 2019-04-05
      • 2017-10-05
      • 2020-01-27
      • 1970-01-01
      相关资源
      最近更新 更多