Android Studio:如何解决此执行错误

时间:2019-09-21 20:18:47

标签: java android

我是android studio的新手,我不明白为什么当我开始使用seekbar时我的应用程序崩溃。 有人可以帮我吗?

公共类MainActivity扩展了AppCompatActivity {     SeekBar seekBar; //声明seekbar对象     TextView textView,textView2;     ListView simpleListview;     //声明SeekBar的成员变量     int离散     int start = 50;     int start_position = 50;     //进度跟踪器     int temp = 0;     //为ViewStub声明对象     ViewStub存根;     CheckBox复选框;     ListView 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");
        }

    });

3 个答案:

答案 0 :(得分:2)

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

textView2尚未在此处初始化...

将以上代码更改为

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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