当我点击 ListFragment 中的任何元素时,我试图在 MyCustomView 中接收一个回调,这是一个代表片段的类(或者我们可以说,它是片段的视图)。日志附在下面:
04-12 11:56:04.321: D/FirstFragment(762): setting ui listener callback
04-12 11:56:04.321: D/AndroidRuntime(762): Shutting down VM
04-12 11:56:04.321: W/dalvikvm(762): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-12 11:56:04.331: E/AndroidRuntime(762): FATAL EXCEPTION: main
04-12 11:56:04.331: E/AndroidRuntime(762): java.lang.ClassCastException: com.gaurav.listing.ListFragmentActivity
04-12 11:56:04.331: E/AndroidRuntime(762): at com.gaurav.listing.FirstFragment.onListItemClick(FirstFragment.java:66)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.widget.ListView.performItemClick(ListView.java:3513)
04-12 11:56:04.331: E/AndroidRuntime(762): at
android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.os.Handler.handleCallback(Handler.java:587)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.os.Looper.loop(Looper.java:123)
04-12 11:56:04.331: E/AndroidRuntime(762): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-12 11:56:04.331: E/AndroidRuntime(762): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 11:56:04.331: E/AndroidRuntime(762): at java.lang.reflect.Method.invoke(Method.java:507)
04-12 11:56:04.331: E/AndroidRuntime(762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-12 11:56:04.331: E/AndroidRuntime(762): at
04-12 11:56:04.331: E/AndroidRuntime(762): at dalvik.system.NativeStart.main(Native Method)com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
然后我从我试图设置这个回调的地方附加片段的代码。
package com.gaurav.listing;
import android.app.Activity;
public class FirstFragment extends ListFragment {
private static final String TAG = "FirstFragment";
public UIListener uiCallback;
ArrayAdapter adapter;
ListView list;
View view;
String[] values;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// some code here...
}
@Override
public void onResume() {
// some code here...
}
Activity activity = null;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
this.activity = activity;
uiCallback = (UIListener) activity;
} catch (ClassCastException e) {
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.d("FirstFragment", "onListItemClick");
String shape = l.getItemAtPosition(position).toString();
Log.d(TAG, shape);
if (null == uiCallback) {
Log.d("FirstFragment", "setting ui listener callback");
// problem occur here
uiCallback = (UIListener) activity;
}
uiCallback.onButtonClicked(l, v, position, id, shape);
}
}
请让我知道我该如何解决这个问题。关于我想要接收回调的目标的更多详细信息:片段的 id:片段的 second_fragment 视图类:SecondFragment.java