简体   繁体   中英

App crashing because of onclick?

While I was testing my app on a virtual device, I faced the following error:

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) 

This error came when I added the following to MainActivity.java:

  • A function that incremented a couple of integers and displayed the values to text view targetted using text view id.

Can anyone please explain why is this happening? I am new to this.

it's because you set int value in TextView.Because setText() accepts only String values or Resource ID of a String (which is infact int).

change

displayMessageOnnumOfTotalDays.setText(totalDays);

displayMessageOntv1.setText(daysAttended);

To

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

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

As well as you can use following way.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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