0

我有一个主要活动。在 Main Activity 的 Oncreat{} 中,我创建了一个名为“ChannelList”代码的片段:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //initVtcId();
    setContentView(R.layout.activity_main);
    FragmentManager fm = getSupportFragmentManager();

    if (fm.findFragmentById(android.R.id.tabcontent) == null) { 

          FragmentTransaction fragmentTransaction = fm.beginTransaction();
          ChannelList list = new ChannelList();
          //fragmentTransaction.add(android.R.id.content, list);

          fragmentTransaction.add(android.R.id.tabcontent, list);
          fragmentTransaction.commit();
      }

为了与 ChannelList 进行通信,以与名称为“ChannelActivity”的 Activity 进行通信。我在 ChannelList 中使用名称为“OnListItemSelectedListener”的接口及其方法“onListItemSelected”

public class ChannelList extends ListFragment {
private OnListItemSelectedListener mCallback;
public interface OnListItemSelectedListener {
    public void onListItemSelected(ListView l, int position);
}

Fragment ChannelList 在其 onAttach() 生命周期方法期间捕获接口实现

     @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception.
        try {
            mCallback = (OnListItemSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnListItemSelectedListener");
        }
    }

当用户单击列表项时,将调用片段中的以下方法。片段使用回调接口将事件传递给父活动。

    public void onListItemClick(ListView l, View v, int position, long id) {
    mCallback.onListItemSelected(l, mChannelAdapter.getItem(position)
            .getID());
}

在 ChannelActivity 上,我实现了这个接口及其方法来调用名为“ChannelDetailFragment”的片段

public class ChannelActivity extends FragmentActivity implements
    ChannelList.OnListItemSelectedListener {

private FragmentManager fm;
private Fragment mChannelDetailFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.channel_activity);
    fm = getSupportFragmentManager();

    Fragment mChannelListFragment = fm
            .findFragmentByTag(ChannelList.TAG);
    if (mChannelListFragment == null) {
        addFragment(new ChannelList(), false,
                ChannelList.TAG);
    }

}

@Override
public void onListItemSelected(ListView l, int position) {
    Bundle args = new Bundle();
    args.putInt(ChannelDetailFragment.CHANNEL_ID, position);

    mChannelDetailFragment = fm
            .findFragmentByTag(ChannelDetailFragment.TAG);
    if (mChannelDetailFragment == null) {
        mChannelDetailFragment = new ChannelDetailFragment();
    }
    mChannelDetailFragment.setArguments(args);
    addFragment(mChannelDetailFragment, true, ChannelDetailFragment.TAG);
}

我在以下位置关注此 tut:http: //developer.android.com/training/basics/fragments/communicating.html#Deliver 不幸的是,我从 OnAttach{} 抛出错误。我多次检查我的代码,但没有发现哪里不正确。某人帮帮我!

10-02 14:07:40.156: E/AndroidRuntime(980): FATAL EXCEPTION: main
10-02 14:07:40.156: E/AndroidRuntime(980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.channellistfragment/com.example.channellistfragment.MainActivity}: java.lang.ClassCastException: com.example.channellistfragment.MainActivity@a68d01b8 must implement OnListItemSelectedListener
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.os.Looper.loop(Looper.java:137)
10-02 14:07:40.156: E/AndroidRuntime(980):  at   android.app.ActivityThread.main(ActivityThread.java:4745)
10-02 14:07:40.156: E/AndroidRuntime(980):  at java.lang.reflect.Method.invokeNative(Native Method)
10-02 14:07:40.156: E/AndroidRuntime(980):  at java.lang.reflect.Method.invoke(Method.java:511)
10-02 14:07:40.156: E/AndroidRuntime(980):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-02 14:07:40.156: E/AndroidRuntime(980):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-02 14:07:40.156: E/AndroidRuntime(980):  at dalvik.system.NativeStart.main(Native Method)
10-02 14:07:40.156: E/AndroidRuntime(980): Caused by: java.lang.ClassCastException: com.example.channellistfragment.MainActivity@a68d01b8 must implement OnListItemSelectedListener
10-02 14:07:40.156: E/AndroidRuntime(980):  at com.example.channellistfragment.ChannelList.onAttach(ChannelList.java:79)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:867)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.Activity.performStart(Activity.java:5018)
10-02 14:07:40.156: E/AndroidRuntime(980):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
10-02 14:07:40.156: E/AndroidRuntime(980):  ... 11 more
4

1 回答 1

0

您有 2 个活动并向它们添加 ChannelList 片段。第一个(主活动)没有实现你的回调接口,你得到 ClassCast 例外。

于 2013-10-02T08:01:31.580 回答