无法在应用程序恢复时启动活动ComponentInfo NullPointerException错误

时间:2012-09-26 10:39:28

标签: android android-intent android-fragments android-logcat android-fragmentactivity

我的应用程序工作正常,但是当它在后台运行时,然后在恢复后停止在logcat处理这些错误:

09-26 15:46:49.278: D/SqliteDatabaseCpp(5847): Registering sqlite logging func: /data/data/com.friends.roleon/databases/roleonDB
09-26 15:46:49.288: D/SqliteDatabaseCpp(5847): DB info: open db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, flag = 6, file size = 34816
09-26 15:46:49.288: D/SqliteDatabaseCpp(5847): DB info: path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle: 0x1f29030, type: w, r/w: (0,1), mode: truncate, disk free size: 213 M
09-26 15:46:49.318: D/SqliteDatabaseCpp(5847): DB info: close db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle = 0x1f29030, type = w, r/w = (0, 0)
09-26 15:46:49.318: D/check(5847): create fragmentnull
09-26 15:46:49.559: D/SqliteDatabaseCpp(5847): DB info: open db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, flag = 6, file size = 34816
09-26 15:46:49.599: D/SqliteDatabaseCpp(5847): DB info: path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle: 0x1f4a208, type: w, r/w: (0,1), mode: truncate, disk free size: 213 M
09-26 15:46:49.649: D/SqliteDatabaseCpp(5847): DB info: open db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, flag = 6, file size = 34816
09-26 15:46:49.649: D/AndroidRuntime(5847): Shutting down VM
09-26 15:46:49.649: W/dalvikvm(5847): threadid=1: thread exiting with uncaught exception (group=0x40aab228)
09-26 15:46:49.659: E/AndroidRuntime(5847): FATAL EXCEPTION: main
09-26 15:46:49.659: E/AndroidRuntime(5847): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.friends.roleon/com.friends.roleon.roleonMain}: java.lang.IllegalArgumentException: No view found for id 0x7f060038 for fragment RecordListFragment{40dd3b10 #2 id=0x7f060038}
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.os.Looper.loop(Looper.java:156)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread.main(ActivityThread.java:4987)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at java.lang.reflect.Method.invokeNative(Native Method)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at dalvik.system.NativeStart.main(Native Method)
09-26 15:46:49.659: E/AndroidRuntime(5847): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060038 for fragment RecordListFragment{40dd3b10 #2 id=0x7f060038}
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:864)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1195)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.Activity.performStart(Activity.java:4548)
09-26 15:46:49.659: E/AndroidRuntime(5847):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2178)
09-26 15:46:49.659: E/AndroidRuntime(5847):     ... 11 more
09-26 15:46:49.679: D/SqliteDatabaseCpp(5847): DB info: path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle: 0x1f5d218, type: w, r/w: (0,2), mode: truncate, disk free size: 213 M
09-26 15:46:49.829: W/System.err(5847): java.lang.NullPointerException
09-26 15:46:49.829: W/System.err(5847):     at com.friends.roleon.utility.DataBase.<init>(DataBase.java:27)
09-26 15:46:49.839: W/System.err(5847):     at com.friends.roleon.list.ListViewGenerator.RecordViewGenerator(ListViewGenerator.java:25)
09-26 15:46:49.839: W/System.err(5847):     at com.friends.roleon.list.record.RecordList$RecordListFragment$BackgroundTask.doInBackground(RecordList.java:97)
09-26 15:46:49.839: W/System.err(5847):     at com.friends.roleon.list.record.RecordList$RecordListFragment$BackgroundTask.doInBackground(RecordList.java:1)
09-26 15:46:49.849: W/System.err(5847):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-26 15:46:49.849: W/System.err(5847):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-26 15:46:49.849: W/System.err(5847):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-26 15:46:49.849: W/System.err(5847):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-26 15:46:49.859: W/System.err(5847):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-26 15:46:49.859: W/System.err(5847):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-26 15:46:49.859: W/System.err(5847):     at java.lang.Thread.run(Thread.java:864)
09-26 15:46:49.869: D/error(5847): this is the exception :java.lang.NullPointerException
09-26 15:46:49.879: W/DBVersion(5847): 3
09-26 15:46:49.879: D/SqliteDatabaseCpp(5847): DB info: open db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, flag = 6, file size = 34816
09-26 15:46:49.879: W/dalvikvm(5847): threadid=11: thread exiting with uncaught exception (group=0x40aab228)
09-26 15:46:49.879: D/SqliteDatabaseCpp(5847): DB info: path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle: 0x1f77c90, type: w, r/w: (0,3), mode: truncate, disk free size: 213 M
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847): crash in the same process: AsyncTask #1
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847): java.lang.RuntimeException: An error occured while executing doInBackground()
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.lang.Thread.run(Thread.java:864)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847): Caused by: java.lang.NullPointerException
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at com.friends.roleon.list.record.RecordList$RecordListFragment$BackgroundTask.doInBackground(RecordList.java:132)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at com.friends.roleon.list.record.RecordList$RecordListFragment$BackgroundTask.doInBackground(RecordList.java:1)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-26 15:46:49.889: E/AndroidRuntime_2_crash(5847):     ... 5 more
09-26 15:46:49.909: D/SqliteDatabaseCpp(5847): DB info: close db, path = /data/data/com.friends.roleon/databases , key = DvkadTace, handle = 0x1f77c90, type = w, r/w = (0, 2)

这是我的主要课程:

public class roleon extends SherlockFragmentActivity implements
        ActionBar.TabListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        getSupportActionBar().setDisplayShowHomeEnabled(false);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        AddTab(getString(R.string.tab_home));
        AddTab(getString(R.string.tab_activities));
        AddTab(getString(R.string.tab_settings));
    }

    private void AddTab(String tabName) {
        ActionBar.Tab tab = getSupportActionBar().newTab();
        tab.setText(tabName);
        tab.setTabListener(this);
        getSupportActionBar().addTab(tab);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Fragment fragment = null;
        if (tab.getText().equals(getText(R.string.tab_home))) {
            fragment = new ModuleList.ModuleListFragment();
        } else if (tab.getText().equals(getText(R.string.tab_activities))) {
            fragment = new ActivityStatePager.FragmentDisplay();
        } else if (tab.getText().equals(getText(R.string.tab_settings))) {
            fragment = new PreferenceScreen.PreferenceScreenFragment();
        }
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, fragment).commit();
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // Do nothing
    }

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

    }
}

这是应用程序在恢复时崩溃的片段类:

public class ActivityStatePager extends SherlockFragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.Theme_Sherlock_NoActionBar);
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            FragmentDisplay fragment = new FragmentDisplay();
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, fragment).commit();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
         ActivityStatePager.this.finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        startActivity(new Intent(ActivityStatePager.this,
                roleon.class));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityStatePager.this.finish();
    }

    public static class FragmentDisplay extends SherlockFragment {
        // Static Declarations
        static final int NUM_ITEMS = 3;

        private View mLayout;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v = inflater
                    .inflate(R.layout.fragment_pager, container, false);
            mLayout = v.findViewById(R.id.container);
            SetSpinner(v);
            return v;
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }

        private void SetSpinner(View v) {
            Spinner eventspinner = (Spinner) v.findViewById(R.id.eventspinner);

            ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(
                    getActivity(), android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

            DataBase db = new DataBase(getActivity(), "EventStatus");
            ArrayList<Record> eventsArray = db.SelectStatusList();
            db.close();

            for (Iterator<Record> iterator = eventsArray.iterator(); iterator
                    .hasNext();) {
                Record record = (Record) iterator.next();
                adapter.add(record.getEventStatus());
            }

            eventspinner.setAdapter(adapter);

            eventspinner
                    .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        public void onItemSelected(AdapterView<?> parent,
                                View view, int pos, long id) {
                            CreateFragment(parent.getItemAtPosition(pos)
                                    .toString());
                        }

                        public void onNothingSelected(AdapterView<?> parent) {
                            // do nothing
                        }
                    });
        }

        public void CreateFragment(String fragLabel) {
            FragmentTransaction ft = getSherlockActivity()
                    .getSupportFragmentManager().beginTransaction();
            Fragment fragment = new ActivityListFragment(fragLabel);//fragLabel
            ft.add(((LinearLayout) mLayout).getId(), fragment).commit();
        }

        @Override
        public void onDestroyView() {
            super.onDestroyView();
        }

        public static class ActivityListFragment extends SherlockFragment {
            private String mFragTag;
            private View mFrameLayout;
            private View mView;

            public ActivityListFragment(String fragName) {
                mFragTag = fragName;
            }

            public ActivityListFragment(){
                // empty constructor
            }

            @Override
            public View onCreateView(LayoutInflater inflater,
                    ViewGroup container, Bundle savedInstanceState) {
                mView = inflater.inflate(R.layout.fragment_pager_list,
                        container, false);
                mFrameLayout = mView.findViewById(R.id.frame_layout);

                return mView;
            }

            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                SetFragment();
            };

            public void SetFragment() {
                FragmentTransaction ft = getSherlockActivity()
                        .getSupportFragmentManager().beginTransaction();

                Fragment fragment = new RecordList.RecordListFragment();
                Bundle extras = new Bundle();
                extras.putString("eventstatus", mFragTag);
                fragment.setArguments(extras);

                ft.replace(((FrameLayout) mFrameLayout).getId(), fragment)
                        .commit();
            }

            @Override
            public void onDestroyView() {
                super.onDestroyView();
            }
        }
    }
}

R.id.frame_layout在R.java文件中具有id:0x7f060038

这是我的RecordList.RecordListFragment()所做的:

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            v = inflater.inflate(R.layout.record_list, container, false);
            mSP = PreferenceManager.getDefaultSharedPreferences(getActivity());
            GetIntent();
            getActivity().setTitle(label);
            new BackgroundTask().execute();
            return v;
        }


        private class BackgroundTask extends AsyncTask<Void, Void, Void> {
            ProgressDialog mDialog;

            protected void onPreExecute() {
                mDialog = ProgressDialog.show(getActivity(), "",
                        getString(R.string.progress_bar_loading), true);
            };

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    mModule = new CRMModule(name, getActivity(),
                            getActivity().getIntent());
                    mEnableOptions = true;
                    rarray = ListViewGenerator.RecordViewGenerator(mSP,
                            getActivity(), name);
                    Log.d("values","in doinbackground"+mModule+","+rarray);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // Amazing List View
                            View composer = v.findViewById(R.id.lsComposer);
                            mLsComposer = ((DynamicListView) composer);
                            try {
                                mLsComposer
                                        .setAdapter(mAdapter = new PaginationComposerAdapter(
                                                mSP, getActivity(),
                                                rarray, name,
                                                label, mEventStatus));
                                Options(men);
                            } catch (Exception e) {
                                Log.d("exception", "this is here at composer"+e);
                                e.printStackTrace();
                            }
                            if (mAdapter.getCount() > 10) {
                                mLsComposer
                                        .setLoadingView(getActivity()
                                                .getLayoutInflater()
                                                .inflate(
                                                        R.layout.lscomposer_loadingview,
                                                        null));
                            }
                            Log.d("complete","at the end");

                        }

                    });
                } catch (final Exception e) {
                    Log.d("error", "this is the exception :"+e);
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mEnableOptions = false;
                            // getActivity().supportInvalidateOptionsMenu();
                            MethodActivity.CheckLogin(mSP, getActivity()
                                    .getIntent(), getActivity(), e);
                        }
                    });
                }
                return null;
            }

            protected void onPostExecute(Void result) {
                mDialog.dismiss();
            };
        };

帮助我们解决这个问题。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

finish()已经呼叫onDestroy()的人查看the android documentation时,onDestroy()被触发,因此您不应在finish()上致电finish()。在onPause()事件上调用if (savedInstanceState == null) { FragmentDisplay fragment = new FragmentDisplay(); getSupportFragmentManager().beginTransaction() .add(android.R.id.content, fragment).commit(); } 也是一样,如果你想要实现的是从ActivityStatePager中分离你的FragmentDisplay你应该移动

onResume()

进入onPause()事件,并在java.lang.IllegalArgumentException: No view found for id 0x7f060038事件中分离此片段。使用您的代码,每次活动暂停时,您都会强制调用onCreate(),这是一项耗费精力的操作。

我认为异常onResume()来自您的活动RecordList.RecordListFragment.BackgroundTask的方式。

您也可以展示NullPointerException的作用吗? RecordList.RecordListFragment.BackgroundTask从那里开始。

添加AsyncTask.publishProgress()代码后进行修改:

好的,我想我得到了这个。在doInBackground()上执行操作时避免访问UIThread。使用AsyncTask.onProgressUpdate()并覆盖 View composer = v.findViewById(R.id.lsComposer); mLsComposer = ((DynamicListView) composer); try { mLsComposer .setAdapter(mAdapter = new PaginationComposerAdapter( mSP, getActivity(), rarray, name, label, mEventStatus)); Options(men); } catch (Exception e) { Log.d("exception", "this is here at composer"+e); e.printStackTrace(); } if (mAdapter.getCount() > 10) { mLsComposer .setLoadingView(getActivity() .getLayoutInflater() .inflate( R.layout.lscomposer_loadingview, null)); } Log.d("complete","at the end"); 对UI进行更改,而后台线程仍有工作要做(请查看AsynTask上的文档)。在你的情况下,你应该把

onPostExecute()

{{1}}因为您希望在完成长时间工作后对UI进行更改。