我用的android studio做項目的,別人用的ELIPSE 今天在XML中定義了一個點擊事件sys_setting,然後在JAVA中實現卻出現找不到方法,怎麼回事?
XML
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginTop="2dp"
android:layout_marginRight="0.5dp"
android:onClick="sys_setting"
android:background="@drawable/set_btn">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:drawableLeft="@drawable/set"
android:text=" 設置"
android:textColor="#f3f3f3"
android:textSize="20dp" />
Java中
public class DevListActivity extends FragmentActivity {
public static Context context;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dev_list);
}
public void sys_setting(View view){
Intent intent = new Intent(context, SysSettingActivity.class);
startActivity(intent);
}
}
錯誤
java.lang.IllegalStateException: Could not find a method sys_setting(View) in the activity class com.xfengche.wwww.xfegncheradio_net.DevListActivity for onClick handler on view class android.widget.LinearLayout
at android.view.View$1.onClick(View.java:3712)
at android.view.View.performClick(View.java:4262)
at android.view.View$PerformClick.run(View.java:17351)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4935)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoSuchMethodException: sys_setting [class android.view.View]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getMethod(Class.java:915)
at android.view.View$1.onClick(View.java:3705)
at android.view.View.performClick(View.java:4262)
at android.view.View$PerformClick.run(View.java:17351)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4935)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
從代碼上和寫法上沒看出問題來
1、檢查一下xml布局和activity是不是匹配的,另外DevListActivity是不是就只有一個
2、重新編譯一下工程看看,是不是DevListActivity沒有編譯
3、把onclick通過代碼方式添加試試。