简体   繁体   English

setOnClickListener 错误 - 无法解析符号

[英]setOnClickListener error - cannot resolve symbol

I want to create a button which, when clicked, it will show another intent.我想创建一个按钮,单击该按钮时会显示另一个意图。

This is the code:这是代码:

   <Button
    android:text="Bine ati venit!"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/button1"
    android:background="#479fc7"
    android:textSize="40dp"
    android:textColor="@color/colorPrimaryDark"
    android:textStyle="normal|bold|italic"
    android:textAllCaps="false"
    android:fontFamily="casual"
    android:onClick="next_page"/>

and from the .java class:从 .java 类:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
}

Button button = (Button) findViewById(R.id.button1);

button.setOnClickListener( new View.OnClickListener()
{
    public void onClick (View v){
    next_page(v);
}
});

public void next_page(View v) {
    Intent intent = new Intent(this, MapsActivity.class);
    startActivity(intent);
}

I get the errors: "Cannot resolve symbol 'setOnClickListener'", "variable onClick is never used", "; expected"....我收到错误消息:“无法解析符号‘setOnClickListener’”、“从未使用过变量 onClick”、“; 预期”....

You should put that particular snippet inside OnCreate() .您应该将该特定片段放在OnCreate() That solved the issue for me.那为我解决了这个问题。

// You must move the code you type into the class method and work like the following // 您必须将您键入的代码移动到类方法中并按如下方式工作

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        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, NumberActivity.class);
                startActivity(numbersIntent);
            }
        });
    }

You need to change your code like this您需要像这样更改代码

Button button;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_welcome); 
    button = (Button) findViewById(R.id.button1);
    button.setOnClickListener( new View.OnClickListener(){ 
        public void onClick (View v){ 
            next_page(v); 
        } 
    }); 

} 

public void next_page(View v) {
    Intent intent = new Intent(this, MapsActivity.class);
    startActivity(intent);
} 

and you don't need to pass View object to next_page() if you don't use it there如果您不在那里使用它,则不需要将 View 对象传递给next_page()

You either declare onClick attribute with method name in your xml or programaticaly set it in onCreate method.您可以在 xml 中使用方法名称声明 onClick 属性,或者在 onCreate 方法中以编程方式设置它。

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);

    Button button = (Button) findViewById(R.id.button1);

    button.setOnClickListener( new View.OnClickListener()
    {
        public void onClick (View v){
           next_page(v);
        }
    });
}

public void next_page(View v) {
    Intent intent = new Intent(this, MapsActivity.class);
    startActivity(intent);
}

您应该声明按钮变量并在oncreate()函数下编写Setonclicklistner()函数。否则它将无法正常工作。

With regards to the error: "Cannot resolve symbol 'setOnClickListener'" It might be worth making sure that you have imported TextView by adding关于错误:“无法解析符号'setOnClickListener'”可能值得确保您通过添加导入了TextView

import android.widget.TextView;

to the top of your file.到文件的顶部。

You just need to change the location of the code.您只需要更改代码的位置。

@Override
protected void onCreate(Bundle savedInstanceState) {
 ...
 Button button = (Button) findViewById(R.id.button1);

 button.setOnClickListener( new View.OnClickListener(){
     public void onClick (View v){
     next_page(v);
     }
 });
}

Cut the Code and paste it in the OnCreate method.剪切代码并将其粘贴到 OnCreate 方法中。 As it doesn't make sense to set the OnClickListener after the button is created.因为在按钮创建后设置OnClickListener没有意义。 That's why the error occured.这就是错误发生的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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