0
我想允許一個ImageView點擊開始一個意圖,並打開一個新的活動。我讀過的一些問題編程做(我用butterknife),所以我想我會嘗試,並通過XML設置的onClick:IllegalStateException當設置ImageView點擊方法
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)
仍然產生相同的錯誤...我已經閱讀過不同的帖子,它與ImageView在FrameLayout中的事實有關.. – tccpg288
嗯,我可以告訴你,文檔明確指出,方法必須公開。不知道現在會出現什麼問題。 http://developer.android.com/reference/android/view/View.html#attr_android:onClick –
知道了,感謝您的幫助 – tccpg288