cannot resolve symbol setOnClickListener in android

后端 未结 3 2009
再見小時候
再見小時候 2021-01-29 16:13

When i run this program, it repeatedly saying that \"cannot resolve symbol setOnClickListener\".anyone please help me to solve this problem.

import android.suppo         


        
相关标签:
3条回答
  • 2021-01-29 16:42

    Your code should be inside the onCreate method, try :

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.activity_main);
    
        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);
    
            // Set a click listener on that View
            numbers.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
    
            @Override
            public void onClick(View view) {
                Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
                startActivity(numbersIntent);
            }
        }
    }
    

    Read more about Activities and their lifecycle : Activity Lifecycle

    0 讨论(0)
  • 2021-01-29 16:56

    Your onClickListener needs to be in your onCreate method.

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.activity_main);
            // Find the View that shows the numbers category
            TextView numbers = (TextView) findViewById(R.id.numbers);
    
            // Set a click listener on that View
            numbers.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when the numbers View is clicked on.
                @Override
                public void onClick(View view) {
                    Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
                    startActivity(numbersIntent);
               }
         });
         }
    }
    

    That should fix the error. Also make sure you have added Numbers.class in your AndroidManifest, otherwise you'll get another error when you start the intent.

    <activity
            android:name=".Numbers"
            android:label="Numbers"/>
    
    0 讨论(0)
  • 2021-01-29 16:58
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);
    
        // Find the View that shows the numbers category
        TextView numbers = (TextView) findViewById(R.id.numbers);
    
        // Set a click listener on that View
        numbers.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when the numbers View is clicked on.
    
        @Override
        public void onClick(View view) {
            Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
            startActivity(numbersIntent);
        }
    }
    

    or:

    public class MainActivity extends AppCompatActivity implements OnClickListener{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Set the content of the activity to use the activity_main.xml layout file
            setContentView(R.layout.activity_main);
    
            // Find the View that shows the numbers category
            TextView numbers = (TextView) findViewById(R.id.numbers);
    
            // Set a click listener on that View
            numbers.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            Intent numbersIntent = new Intent(MainActivity.this, Numbers.class);
            startActivity(numbersIntent);
        }
    }
    
    0 讨论(0)
提交回复
热议问题