java.lang.IllegalStateException:无法在android

时间:2016-12-17 07:25:56

标签: java android textview

我对Android很新,并且创建了我的第一个应用。我正在使用以下代码:

public class MainActivity extends AppCompatActivity {

int netScore = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addOne(View view) {
   netScore = netScore + 1;
    displayScore(netScore);
}

private void displayScore(int printScore) {
    TextView varScore = (TextView) findViewById(R.id.score);
    varScore.setText(printScore);
}

}

当我单击该按钮时,它会在调试中抛出此错误:

FATAL EXCEPTION: main

java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                               at android.view.View.performClick(View.java:5198)
                                                                               at android.view.View$PerformClick.run(View.java:21147)
                                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                            Caused by: java.lang.reflect.InvocationTargetException
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                               at android.view.View.performClick(View.java:5198) 
                                                                               at android.view.View$PerformClick.run(View.java:21147) 
                                                                               at android.os.Handler.handleCallback(Handler.java:739) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                               at android.os.Looper.loop(Looper.java:148) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                               at java.lang.reflect.Method.invoke(Native Method)

有人对此错误有所了解吗?

2 个答案:

答案 0 :(得分:0)

尝试在xml-layout中添加onClick吗?

android:onClick="method"

如果是这样,请确保没有拼写错误。 在你的情况下,它可能是:

android:onClick="addOne"

您还可以通过编程方式侦听点击事件。查看this

答案 1 :(得分:0)

您可以在OnCreate方法中设置TextView:

TextView varScore = (TextView) findViewById(R.id.score);

之后,将ClickListener添加到 varScore

enter code here
varScope.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
         // do anything whatever you want
    }
});

首先,尝试使用 onCreate 方法启动 TextView

顺便说一句,您能告诉我们您的.xml文件吗?