cannot resolve symbol setOnClickListener in android

我怕爱的太早我们不能终老 提交于 2021-02-17 07:21:39

问题


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

import android.support.v7.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

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);
    }
}

}


回答1:


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




回答2:


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"/>



回答3:


@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);
    }
}


来源:https://stackoverflow.com/questions/39298396/cannot-resolve-symbol-setonclicklistener-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!