无法替换导航抽屉中列表视图中的Onitemclick上的碎片

时间:2014-09-02 09:45:58

标签: android listview android-fragments android-listview navigation-drawer

在我的项目中,我有一个使用导航器打开/导航到不同屏幕(片段)的活动,我最初打开了一个片段,将其视为主屏幕。我在导航抽屉中使用了listview,这样如果我点击列表视图中的任何项目,它应该替换该活动上的片段....我在listview的项目点击上替换片段时遇到问题。 我的主要活动课程:

public class Main extends Activity {
    DrawerLayout drawerlayout;
    ListView LVdrawer;
    ImageView toplogo;
    View rL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        FragmentManager FM = getFragmentManager();
        final FragmentTransaction Trans = FM.beginTransaction();
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation_drawer);
        final ActionBar actionBar = getActionBar();
        rL = findViewById(R.id.toplogoaction);
        drawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        LVdrawer = (ListView) findViewById(R.id.left_drawer);
        LVdrawer.setAdapter(new CustomAdapter(this, 0));
        Trans.setCustomAnimations(R.anim.actionbarin, R.anim.actionbarout, R.anim.actionbarin, R.anim.actionbarout);
        Trans.add(R.id.content_frame, new CreatenBuy(), "AddAppsF");
        Trans.commit();
        LVdrawer.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                TextView ttt=(TextView) view.findViewById(R.id.detail);
                String s=(String) ttt.getText();
                Trans.replace(R.id.content_frame, new CreatenBuy(), "addappsF");

            Trans.commit();

            }
        });
    }
}

我的Fragmnetclass:

    public class CreatenBuy extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            View page=inflater.inflate(R.layout.createnbuy, container, false);
            return page;
        }

}

logcat的:

09-02 16:14:56.371: I/Process(5172): Sending signal. PID: 5172 SIG: 9
09-02 16:14:57.583: I/Adreno-EGL(5479): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018_msm8610_LNX.LA.3.5.1_RB1__release_AU ()
09-02 16:14:57.583: I/Adreno-EGL(5479): OpenGL ES Shader Compiler Version: E031.24.00.08
09-02 16:14:57.583: I/Adreno-EGL(5479): Build Date: 03/07/14 Fri
09-02 16:14:57.583: I/Adreno-EGL(5479): Local Branch: 
09-02 16:14:57.583: I/Adreno-EGL(5479): Remote Branch: quic/LNX.LA.3.5.1_RB1.1
09-02 16:14:57.583: I/Adreno-EGL(5479): Local Patches: NONE
09-02 16:14:57.583: I/Adreno-EGL(5479): Reconstruct Branch: AU_LINUX_ANDROID_LNX.LA.3.5.1_RB1.04.04.02.048.018 + f2fd134 +  NOTHING
09-02 16:14:57.613: D/OpenGLRenderer(5479): Enabling debug mode 0
09-02 16:14:58.763: I/gf(5479): android.widget.LinearLayout{41bd5820 V.E..... ......I. 0,0-0,0}
09-02 16:14:58.802: I/gf(5479): android.widget.LinearLayout{41bd5820 V.E..... ......I. 0,0-0,0}
09-02 16:14:58.814: I/gf(5479): android.widget.LinearLayout{41bf4030 V.E..... ......I. 0,0-0,0}
09-02 16:14:58.933: I/gf(5479): android.widget.LinearLayout{41c1aea0 V.E..... ......I. 0,0-0,0}
09-02 16:15:00.630: I/dafasgsdfds(5479): Add App
09-02 16:15:00.632: D/AndroidRuntime(5479): Shutting down VM
09-02 16:15:00.633: W/dalvikvm(5479): threadid=1: thread exiting with uncaught exception (group=0x415ecd40)
09-02 16:15:00.641: E/AndroidRuntime(5479): FATAL EXCEPTION: main
09-02 16:15:00.641: E/AndroidRuntime(5479): Process: com.example.apeonomy, PID: 5479
09-02 16:15:00.641: E/AndroidRuntime(5479): java.lang.IllegalStateException: commit already called
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:583)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.app.BackStackRecord.commit(BackStackRecord.java:575)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at com.digiapes.apeonomy.Main$1.onItemClick(Main.java:66)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.widget.AdapterView.performItemClick(AdapterView.java:299)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.widget.AbsListView$3.run(AbsListView.java:3645)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.os.Handler.handleCallback(Handler.java:733)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.os.Handler.dispatchMessage(Handler.java:95)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.os.Looper.loop(Looper.java:136)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at android.app.ActivityThread.main(ActivityThread.java:5086)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at java.lang.reflect.Method.invokeNative(Native Method)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at java.lang.reflect.Method.invoke(Method.java:515)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
09-02 16:15:00.641: E/AndroidRuntime(5479):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在onItemClick方法中移动final FragmentTransaction Trans = FM.beginTransaction();