在活动中找不到onClick(View)方法

在充气布局button单击失败在运行时,因为该方法找不到。 E / AndroidRuntime(921):java.lang.IllegalStateException:在活动中找不到onClick(View)方法,但如果从主布局单击button,则工作良好。 我做错了什么?

更新:更改代码… stil失败…添加错误日志我想所有的button都在ome方法中处理。

主要活动课

package com.android.insertview; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Toast; public class InsertViewActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); /*LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup parent = (ViewGroup)findViewById(R.id.main_view); inflater.inflate(R.layout.volume_control, parent);*/ LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.volume_control, null); View insertPoint = findViewById(R.id.main_view); ((ViewGroup) insertPoint).addView(v, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT , ViewGroup.LayoutParams.WRAP_CONTENT)); } public void btnclick(View v) { // TODO Auto-generated method stub Context context = getApplicationContext(); Toast toast = Toast.makeText(context, "This is Toast Notification", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } } 

main.xml中

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:id="@+id/main_view"> </LinearLayout> 

充气布局

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="@drawable/service_button_container_background" android:id="@+id/volume_view"> <Button android:id="@+id/volume_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_up" android:onClick="btnclick" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_mute_off" android:onClick="btnclick"/> <Button android:id="@+id/volume_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_down" android:onClick="btnclick"/> </LinearLayout> 

错误日志

 03-19 15:31:45.184: W/dalvikvm(1071): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 03-19 15:31:45.184: E/AndroidRuntime(1071): Uncaught handler: thread main exiting due to uncaught exception 03-19 15:31:45.194: E/AndroidRuntime(1071): java.lang.IllegalStateException: Could not find a method genclick(View) in the activity 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.View$1.onClick(View.java:2020) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.View.performClick(View.java:2364) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.View.onTouchEvent(View.java:4179) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.widget.TextView.onTouchEvent(TextView.java:6541) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.View.dispatchTouchEvent(View.java:3709) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 03-19 15:31:45.194: E/AndroidRuntime(1071): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659) 03-19 15:31:45.194: E/AndroidRuntime(1071): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.app.Activity.dispatchTouchEvent(Activity.java:2061) 03-19 15:31:45.194: E/AndroidRuntime(1071): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.os.Looper.loop(Looper.java:123) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.app.ActivityThread.main(ActivityThread.java:4363) 03-19 15:31:45.194: E/AndroidRuntime(1071): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 15:31:45.194: E/AndroidRuntime(1071): at java.lang.reflect.Method.invoke(Method.java:521) 03-19 15:31:45.194: E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 03-19 15:31:45.194: E/AndroidRuntime(1071): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 03-19 15:31:45.194: E/AndroidRuntime(1071): at dalvik.system.NativeStart.main(Native Method) 03-19 15:31:45.194: E/AndroidRuntime(1071): Caused by: java.lang.NoSuchMethodException: genclick 03-19 15:31:45.194: E/AndroidRuntime(1071): at java.lang.ClassCache.findMethodByName(ClassCache.java:308) 03-19 15:31:45.194: E/AndroidRuntime(1071): at java.lang.Class.getMethod(Class.java:1014) 03-19 15:31:45.194: E/AndroidRuntime(1071): at android.view.View$1.onClick(View.java:2017) 03-19 15:31:45.194: E/AndroidRuntime(1071): ... 22 more 

更新2:

好吧,如果我这样做,我可以处理所有的button点击一个方法genericClick()

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/front_end" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- Main --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@drawable/service_button_container_background" > <ImageButton android:id="@+id/radio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_radio" android:clickable="true" android:onClick="genericClick" /> <ImageButton android:id="@+id/player" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_player" android:onClick="genericClick" /> <ImageButton android:id="@+id/navi" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_navigation" android:onClick="genericClick" /> </LinearLayout> <!-- Volume controls --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/volume_controls" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/service_button_container_background" android:orientation="vertical" > <ImageButton android:id="@+id/volume_up" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_up" android:onClick="genericClick" /> <ImageButton android:id="@+id/volume_mute" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_mute_off" android:onClick="genericClick" /> <ImageButton android:id="@+id/volume_down" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_volume_down" android:onClick="genericClick" /> </LinearLayout> <!-- Radio controls --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/radio_controls" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignRight="@+id/main_view" android:layout_marginRight="100dp" android:background="@drawable/service_button_container_background" android:visibility="invisible" > <ImageButton android:id="@+id/radio_preset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="genericClick" android:background="@drawable/button_menu"/> <ImageButton android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_rewind" android:onClick="genericClick" /> <ImageButton android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_play" android:onClick="genericClick" /> <ImageButton android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_forward" android:onClick="genericClick" /> </LinearLayout> <!-- Radio Info --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/radio_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/radio_controls" android:layout_alignLeft="@+id/main_view" android:layout_marginLeft="100dp" android:background="@drawable/service_button_container_background" android:orientation="vertical" android:visibility="invisible"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FM" android:textSize="18.0px" /> <TextView android:id="@+id/preset" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#ffd6d6d6" android:textSize="18.0px" /> <TextView android:layout_width="10.0dip" android:layout_height="wrap_content" android:text=" " android:textSize="18.0px" /> <TextView android:id="@+id/frequency" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="108.0" android:textColor="#ffd6d6d6" android:textSize="18.0px" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/title" android:layout_width="195.0dip" android:layout_height="24.0dip" android:text="Русское радио" android:textColor="#ffd6d6d6" android:textSize="18.0px" /> </LinearLayout> </LinearLayout> </RelativeLayout> 

删除@Override注释。 查看android的官方文档 :onClick – 在他们的情况下,“selfDestruct(View)”处理方法。

更新:正如其他人所build议的,你不应该为你的事件处理程序方法使用这样一个通用的命名 – “onClick”太泛化了。 如果你有10个button呢? 尝试像“onClickLoginButton”,“onClickSaveBtn”等。我认为你不能使用“onClick”作为你的偶处理方法的名称,因为View.OnClickListener有“onClick”方法,所以你需要改变名称。

 <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" /> 

并在您的活动类

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

删除OnClickListener的实现,以便您的类的定义变为:

 public class InsertViewActivity extends Activity 

更新2:如果你想要所有的button被1点击监听器处理这样的事情:

 @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 } }; 

你不重写一个方法,所以删除@Override

为了避免将来出现这种情况,您可以创build自己的注释来声明在Android XML布局中添加的方法。

这是一篇博客文章教程:

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

Android 5.X布局主题中的问题主要活动在layout.xml中没有标签主题,而Android OS则是错误的。

解决scheme是在这里: android 5和onClick在xml布局

总之:1)从所有布局中删除标签; 2)在manifest中添加正确的sheme标签

我已经尝试了Android 5.X手机 – 它的工作原理。

您是在manifest.xml中的第二个活动?

你需要在那里添加你的活动。 在您的活动中查看下面的代码更改SecondActivity。 并将包名称设置为您的活动所在的位置

 <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> 

你应该注意另一个:public void onclick_method(){你的逻辑;}。工作室不给你一个提示。 我只是这样写,浪费很多时间来find答案,最后我发现正确的方法是这样的:public void onclick_method( View view ){your logic;}所以,不要忘记括号中的参数!

大段引用