简体   繁体   中英

Android Studio not recognizing any of my textviews

When I search for the views by their id's, i find them. But when I use the variables that store the textviews, they can't be recognized and i'm constantly told that they are no where to be found. My button don't give me probles. Only the textviews keeps saying "Cannot resolve symbol"

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button1, button2, button3;
    button1 = ((Button) findViewById(R.id.button1));
    button2 = ((Button) findViewById(R.id.button2));
    button3 = ((Button) findViewById(R.id.button3));
    button1.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
    TextView textview1, textview2, textview3, questionView, questionView2, questionNumber;

    textview1 = ((TextView) findViewById(R.id.textView1));
    textview2 = ((TextView) findViewById(R.id.textView2));
    textview3 = ((TextView) findViewById(R.id.textView3));
    questionView = ((TextView) findViewById(R.id.questionView));
    questionView2 = ((TextView) findViewById(R.id.questionView2));
    questionNumber = (TextView) findViewById(R.id.questionNumber);
}

   @Override

        public void onClick( View v) {
            switch (v.getId()) {
                case R.id.button1:



                    if(questionIndex < questions.length -1
                            )

                        if (questionIndex != 5){
                            textview2.setText("");
                        } if (questionIndex == 5){
                    textview2.setText(halfQues8);
                }

                    // make sure the index is in bounds

                    questionIndex++ ; // increment the index


                    questionValue ++;



                    questionView.setText(Questions[questionIndex]); // set the text to the next question

    /*
     * set the text to the next answer options
     */
                    textview1.setText(answerButtA[questionIndex]);
                    textview2.setText(answerButtB[questionIndex]);
                    textview3.setText(answerButtC[questionIndex]);
                    questionNumber.setText("Question " + questionValueS + ".");

            }

            else{

                questionView.setText("Complete");
                questionNumber.setText("");
                questionView2.setText("");

                textview1.setText("");
                textview2.setText("");
                textview3.setText("");
                Results.setText("Click here for results");
            }






            break;
                case R.id.button2:
                    //DO something
                    break;
                case R.id.button3:
                    //DO something
                    break;
            }

Your TextViews are local variables.
Assign them to member fields so that they are visible in your onClick overload.

You have declared your buttons as method/local variables. Therefore you can't access them from another method. In order to solve this declare them as global variables Eg:

Button button1, button2, button3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = ((Button) findViewById(R.id.button1));
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM