0

我正在尝试为选项卡中的列表实现侦听器,当您单击一个列表项时,我想从显示的 ListFragment 切换到另一个 ListFragment。

现在,第二个 listfragment 打开,但第一个仍然在后台。我该如何解决这个问题?

有人愿意帮助新手吗?

单击第一个列表中的一个项目时应用程序崩溃,LogCat 显示:

04-22 18:51:47.359: D/dalvikvm(614): Not late-enabling CheckJNI (already on)
04-22 18:51:47.840: E/Trace(614): error opening trace file: No such file or directory (2)
04-22 18:51:48.510: D/dalvikvm(614): GC_FOR_ALLOC freed 69K, 3% free 8052K/8259K, paused 34ms, total 38ms
04-22 18:51:48.520: I/dalvikvm-heap(614): Grow heap (frag case) to 8.498MB for 614416-byte allocation
04-22 18:51:48.659: D/dalvikvm(614): GC_CONCURRENT freed 1K, 3% free 8651K/8903K, paused 86ms+23ms, total 136ms
04-22 18:51:48.809: D/gralloc_goldfish(614): Emulator without GPU emulation detected.
04-22 18:51:52.110: I/Choreographer(614): Skipped 43 frames!  The application may be doing too much work on its main thread.
04-22 18:52:02.020: D/AndroidRuntime(614): Shutting down VM
04-22 18:52:02.020: W/dalvikvm(614): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-22 18:52:02.060: E/AndroidRuntime(614): FATAL EXCEPTION: main
04-22 18:52:02.060: E/AndroidRuntime(614): java.lang.NullPointerException
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.app.BackStackRecord.run(BackStackRecord.java:609)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.os.Handler.handleCallback(Handler.java:615)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.os.Looper.loop(Looper.java:137)
04-22 18:52:02.060: E/AndroidRuntime(614):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-22 18:52:02.060: E/AndroidRuntime(614):  at java.lang.reflect.Method.invokeNative(Native Method)
04-22 18:52:02.060: E/AndroidRuntime(614):  at java.lang.reflect.Method.invoke(Method.java:511)
04-22 18:52:02.060: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-22 18:52:02.060: E/AndroidRuntime(614):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-22 18:52:02.060: E/AndroidRuntime(614):  at dalvik.system.NativeStart.main(Native Method)
04-22 18:52:04.049: I/Process(614): Sending signal. PID: 614 SIG: 9

这是我的代码:主要:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    actionBar.setDisplayShowTitleEnabled(true);

    Tab tab = actionBar.newTab().setText("Kategorier")
        .setTabListener(new CustomTabListener<KategorierFragment>(this, "kategorier", KategorierFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Profil")
        .setTabListener(new CustomTabListener<ProfilFragment>(this, "profil", ProfilFragment.class));
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Info")
        .setTabListener(new CustomTabListener<InfoFragment>(this, "info", InfoFragment.class));
    actionBar.addTab(tab);
    }
}

自定义标签监听器

public class CustomTabListener<T extends Fragment> implements TabListener {

private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;

public CustomTabListener(Activity activity, String tag, Class<T> clz){
    mActivity = activity;
    mTag = tag;
    mClass = clz;
}

@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {

}

@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {

    if(mFragment==null){
        mFragment = Fragment.instantiate(mActivity, mClass.getName());
        ft.add(android.R.id.content, mFragment, mTag);
    }else{
        ft.attach(mFragment);
    }
}

@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    if(mFragment!=null)
        ft.detach(mFragment);
    }
}

第一个选项卡下显示的 listfragment:

public class KategorierFragment extends ListFragment {

String kategorier[] = new String[]{
         "Mat","El","Resor","Aktiviteter","Övrigt"};

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_selectable_list_item, kategorier);
    setListAdapter(adapter);
    return super.onCreateView(inflater, container, savedInstanceState);
}

    @Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Fragment mFragment = new KategorierFragment();
    switch(position){
    case 0:
        mFragment = new MatFragment();
        break;
    case 1:
        mFragment = new ElFragment();
        break;
    case 2:
        mFragment = new ResorFragment();
        break;
    case 3:
        mFragment = new AktiviteterFragment();
        break;
    case 4:
        mFragment = new AnnatFragment();
        break;

    }

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ListFragment current = (ListFragment) fm.findFragmentById(R.id.Frame);
    ft.add(R.id.Frame, mFragment);
    ft.addToBackStack(null);
    ft.hide(current);
    ft.commit();
    }
}


@Override
public void onStart() {
    super.onStart();
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
} 

我要切换到的第一个列表片段:

public class MatFragment extends ListFragment{

String mat[] = {"Tips1", "Tips2", "Tips3", "Tips4", "Tips5", "Tips6", "Tips7"};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub

     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_selectable_list_item, mat);
     setListAdapter(adapter);

    return super.onCreateView(inflater, container, savedInstanceState);
    }
}
4

1 回答 1

0

为了避免您的列表被叠加,您需要在提交事务之前隐藏之前的片段。

ListFragment current = (ListFragment) fm.findFragmentById(android.R.id.content);
ft.hide(current);

如果您想使用“返回”导航回最后一个片段,您可以使用

ft.add(android.R.id.content, mFragment);
ft.addToBackStack(null);
ft.hide(current);
ft.commit();
于 2013-04-22T15:56:25.703 回答