Android从另一个窗口返回后崩溃

时间:2014-01-11 12:18:10

标签: java android

我正在使用一个最后有线程操作的活动来初始化一个新片段。运行应用程序时没有问题。但是当从另一个窗口(例如设置)返回时,它会崩溃。

我的代码如下。请注意,实际错误是ft.commit()。

private void preHaberler()
{
    try
    {
        new Thread(new Runnable(){
            @Override 
            public void run(){
                haberler();
            }
        }).start();
    }
    catch(Exception ex)
    {
        test(ex.getMessage());
    }
}

private void haberler()
{
    try
    {
        int sayfa = 1;
        int sayi = 10;
        int modul = -1;
        int ekModul = -1;
        HaberlerListModel[] haberler = Haberler.list(modul, ekModul, sayi, sayfa);
        final Bundle arguments = new Bundle();  
        arguments.putParcelableArray(HaberListesiFragment.ARG_HABERLER, haberler);
        arguments.putInt(HaberListesiFragment.ARG_SAYFA, sayfa);
        arguments.putInt(HaberListesiFragment.ARG_SAYI, sayi);
        arguments.putInt(HaberListesiFragment.ARG_MODUL, modul);
        arguments.putInt(HaberListesiFragment.ARG_EKMODUL, ekModul);

        runOnUiThread(new Runnable(){
            @Override 
            public void run(){
                HaberListesiFragment hlf = new HaberListesiFragment();
                hlf.setArguments(arguments);
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                //ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
                ft.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
                ft.replace(R.id.main_container, hlf);
                ft.addToBackStack("bir");
                ft.commit(); // here is the error
//
        }
        });
    }
    catch(Exception ex)
    {
        test(ex.getMessage());
    }
}

这是堆栈:

DalvikVM[localhost:8629]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        <VM does not provide monitor information>   
        ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2180    
        ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2230 
        ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 141    
        ActivityThread$H.handleMessage(Message) line: 1234  
        ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
        Looper.loop() line: 137 
        ActivityThread.main(String[]) line: 5041    
        Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
        Method.invoke(Object, Object...) line: 511  
        ZygoteInit$MethodAndArgsCaller.run() line: 817  
        ZygoteInit.main(String[]) line: 584 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<10> Binder_2] (Running)    
    Thread [<9> Binder_1] (Running) 
    Thread [<13> Binder_3] (Running)    
    Thread [<11> AsyncTask #1] (Running)    
    Thread [<12> AsyncTask #2] (Running)    
    Thread [<14> AsyncTask #3] (Running)    
    Thread [<15> AsyncTask #4] (Running)    
    Thread [<16> AsyncTask #5] (Running)    


***

DalvikVM[localhost:8629]    
    Thread [<1> main] (Suspended (exception RuntimeException))  
        <VM does not provide monitor information>   
        ZygoteInit$MethodAndArgsCaller.run() line: 823  
        ZygoteInit.main(String[]) line: 584 
        NativeStart.main(String[]) line: not available [native method]  
    Thread [<10> Binder_2] (Running)    
    Thread [<9> Binder_1] (Running) 
    Thread [<13> Binder_3] (Running)    
    Thread [<11> AsyncTask #1] (Running)    
    Thread [<12> AsyncTask #2] (Running)    
    Thread [<14> AsyncTask #3] (Running)    
    Thread [<15> AsyncTask #4] (Running)    
    Thread [<16> AsyncTask #5] (Running)    

如何克服这个问题?

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您需要创建活动才能执行任何FragmentTransaction。

请尝试将您的呼叫转移到onActivityCreated方法。