应用程序崩溃因为onclick?

时间:2017-11-10 07:42:09

标签: java android

当我在虚拟设备上测试我的应用时,我遇到了以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.attendancekeeper.mohilkhare.attendancekeeper, PID: 4150
              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) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
               Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
                  at android.content.res.Resources.getText(Resources.java:312)
                  at android.widget.TextView.setText(TextView.java:4417)
                  at com.attendancekeeper.mohilkhare.attendancekeeper.MainActivity.displayMessageOnnumOfTotalDays(MainActivity.java:32)
                  at com.attendancekeeper.mohilkhare.attendancekeeper.MainActivity.present(MainActivity.java:25)
                  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) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

当我将以下内容添加到MainActivity.java时出现此错误:

  • 一个函数,它增加了几个整数,并使用文本视图ID将值显示到目标文本视图。

任何人都可以解释为什么会发生这种情况?我是新来的。

2 个答案:

答案 0 :(得分:2)

这是因为你在TextView中设置了int值。因为setText()只接受String值或String的资源ID(实际上是int)。

变化

displayMessageOnnumOfTotalDays.setText(totalDays);

displayMessageOntv1.setText(daysAttended);

 displayMessageOnnumOfTotalDays.setText(String.valueOf(totalDays));

 displayMessageOntv1.setText(String.valueOf(daysAttended));

答案 1 :(得分:0)

您可以使用以下方式。

displayMessageOnnumOfTotalDays.setText(totalDays+"");
displayMessageOntv1.setText(daysAttended+"");