Could not find a method onClick(View) in the activity

ⅰ亾dé卋堺 提交于 2019-11-30 06:41:49

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

public void handleVolumeUp(View view) {
    // TODO Auto-generated method stub    
}

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

public class InsertViewActivity extends Activity

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

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

private OnClickListener btnListener = new OnClickListener()
{

    public void onClick(View v)
    {   
      //do the same stuff or use switch/case and get each button ID and do different   

      //stuff depending on the ID
    } 

};  

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/

Denis

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.

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.

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity 
        android:name="PackageName.SecondActivityName" >     
    </activity>

</application>

I had the same problem. I used the <Button> instead of using <android.support.v7.widget.AppCompatButton>. I've tried any of the methods that other people have said.But none was a good answer for me. With that small change I was able to overcome the problem.

and you should be note another : public void onclick_method(){your logic;}.the studio do not give you a hint. I just write like this and waste much time to find the answer, finally i found that the right method is like this : public void onclick_method(View view){your logic;} so, donot be forget the parameter in the bracket!

Blockquote

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!