我正在尝试允许ImageView单击以启动意图并打开新活动。我已经阅读了一些以编程方式执行此操作的问题(我使用的是butterknife),所以我想我会尝试通过XML设置onClick:
private void toComments(View rootView) {
switch (rootView.getId()) {
case R.id.comments_label_icon:
Intent toComments = new Intent(getActivity().getApplicationContext(), Discussion_Activity.class);
toComments.putExtra("POLL_ID", pollID);
startActivity(toComments);
}
}
这是我的XML:
<ImageView
android:onClick="toComments"
android:clickable="true"
android:id="@+id/comments_label_icon"
android:layout_marginBottom="3dp"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/android_chat"
android:layout_gravity="bottom|start"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
/>
我收到此错误:
03-01 20:28:46.105 15074-15074/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sourcey.materialloginexample, PID: 15074
java.lang.IllegalStateException: Could not find method toComments(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'comments_label_icon'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4652)
at android.view.View$PerformClick.run(View.java:19311)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5692)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
改为公开评论:
public void toComments(View rootView)
它也应该存在于显示视图的Activity中。