Skip to content Skip to sidebar Skip to footer

Could Not Find A Method Onclick(view) In The Activity

The button click in inflate layout fails at runtime because the method cannot be found. E/AndroidRuntime(921): java.lang.IllegalStateException: Could not find a method onClick(View

Solution 1:

Remove the @Override annotation. Check out the official documentation for android:onClick - in their case the "selfDestruct(View)" handler method.

Update: as the others suggested, you should not use such a generic naming for your event-handler method - "onClick" is too generic. What if you have 10 buttons? Try something like "onClickLoginButton", "onClickSaveBtn" etc. I think you cannot use "onClick" as name for your even-handler method, because View.OnClickListener has "onClick" method, so you need to change the name anyways.

<Button
    android:id="@+id/volume_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/button_volume_up" 
    android:onClick="handleVolumeUp" />

And in your Activity class

publicvoidhandleVolumeUp(View view) {
    // TODO Auto-generated method stub    
}

Remove the implementation of OnClickListener so that the definition of your class becomes:

publicclassInsertViewActivityextendsActivity

UPDATE2: If you want all buttons to be handled by 1 click listener do something like that:

@OverridepublicvoidonCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Buttonbtn1= (Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(btnListener);
    Buttonbtn2= (Button)findViewById(R.id.btn2);
    btn2.setOnClickListener(btnListener);
    Buttonbtn3= (Button)findViewById(R.id.btn3);
    btn3.setOnClickListener(btnListener);
}

privateOnClickListenerbtnListener=newOnClickListener()
{

    publicvoidonClick(View v)
    {   
      //do the same stuff or use switch/case and get each button ID and do different   //stuff depending on the ID
    } 

};  

Solution 2:

A common problem I've seen is that beginners will declare the onClick method as protected or private. When you do this the XML engine cannot access the method which creates this problem.

Solution 3:

The problem in themes of layouts for Android 5.X The main activity do not have tag theme in layout.xml other has and it's a mistake for Android OS.

Solution is here: android 5 and onClick in xml layout

In short: 1) remove tag sheme from all layouts; 2) add tag with correct sheme in manifest

I have tryed for android 5.X phones - it works.

Solution 4:

Your not overriding a method so remove @Override

To avoid this in the future you can create your own annotation to declare your methods that are added in the Android XML layout.

Here is a blog post tutorial:

http://blog.blundell-apps.com/using-annotations-for-android-xml-onclick-visibility/

Solution 5:

Is your second activity in the manifest.xml?

You need to add al your activity's in there. See code below Change SecondActivity in your activity. And set the package name to were your activity is located

<applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="PackageName.MainActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activityandroid:name="PackageName.SecondActivityName" ></activity></application>

Post a Comment for "Could Not Find A Method Onclick(view) In The Activity"