Skip to content
Search

Android:Troubleshooting:Exception

lastmod 2018-08-17created 2014-02-265 edits

InvocationTargetException

Occurs when the findViewById function is called while declaring a private variable of the Activity class.
R is generated after the project is run, so findViewById() must be called inside a function such as onCreate().

IllegalStateException: Could not find a method in the activity class

There are some issues with ListView and ListViewAdapter (extends BaseAdapter and any other Adapter corresponding to ListView), and android:onClick declared in xml.
When a user-defined Adapter creates a View with an inflater and returns it, onClickListener is often assigned through android:onClick inside the XML Layout loaded with the Inflater.
Here, the Context used to create the Adapter is very important: if you pass this as usual, the View created by the Adapter may not recognize that Context properly.
As a result, when onClick fires, the linked method cannot be found and the app itself crashes.

java.lang.IllegalStateException: Could not find a method true(View) in the activity class android.test.ResultList for onClick handler on view class android.widget.Button with id 'addFavorite'

The exact reproduction path is probably when there is another parent above. It seems this is passed as the parent's Activity rather than the Activity that created the Adapter, so the method corresponding to onClick cannot be found.
So the Context given when creating the Adapter should be Activity.this. ex) MainActivity.this
This way, it immediately understands which Activity the passed Context derives from, so the method corresponding to onClick is called smoothly.

You need to use a Theme.AppCompat theme

This error occurs when you first create a project in Android Studio. Add the following code to AndroidManifest.xml.

<activity android:theme="@style/Theme.AppCompat" ... />

NoClassDefFoundError

A NoClassDefFoundError exception may occur during Android development. In this case, fix it as follows.

Turns out r17 of Android Tools requires the /lib folder to be /libs, and the SDK has to be placed in there and referenced internally in order for it to be included in the apk.

In other words, library files must be placed in the /libs directory.

See also