如何处理错误:java.lang.IllegalStateException:尚未创建的内容视图

时间:2016-01-10 20:16:57

标签: android android-fragments android-intent android-activity

InboxFragment是MainActivity的一个片段。 在MainActivity的动作栏菜单(ActionBar上的那三个点)中,我添加了一个选项Refresh Inbox,它通过创建InboxFragment的对象来调用函数retrieveMessages()。 retrieveMessages是InboxFragment中的一个“成员”函数(早先在片段的onCreateView函数中检索消息,然后我重构它以提取代码以在MainActivity中重用它)。

我尝试使用intent来重新创建MainActivity,使用它,MainActivity.class作为Intent的参数,但这是一种丑陋的刷新。如何处理上述错误?

以下是必需的代码: 在MainActivity中:

 @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            int itemId = item.getItemId();

switch(itemId) {

//pieces of code we're not concerned with

    case R.id.action_refresh_inbox:
    InboxFragment inboxFragment = new InboxFragment();
    inboxFragment.retrieveMessages();
    break;
    }
}

在InboxFragment.java中 -

public class InboxFragment extends ListFragment {
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_inbox, container, false);

            return rootView;
    }
 @Override
    public void onResume() {
        super.onResume();

        retrieveMessages();
    }

    public void retrieveMessages() {
//code that was earlier running in onResume(){};
}

这是logcat:

01-11 01:35:44.515  21968-21968/com.ghostriley.sgt.ghostchat E/ViewRootImpl﹕ sendUserActionEvent() mView == null
01-11 01:35:44.535  21968-22032/com.ghostriley.sgt.ghostchat D/mali_winsys﹕ new_window_surface returns 0x3000,  [227x88]-format:1
01-11 01:35:44.945  21968-21968/com.ghostriley.sgt.ghostchat D/AndroidRuntime﹕ Shutting down VM
01-11 01:35:44.945  21968-21968/com.ghostriley.sgt.ghostchat E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ghostriley.sgt.ghostchat, PID: 21968
    java.lang.IllegalStateException: Content view not yet created
            at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
            at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
            at com.ghostriley.sgt.ghostchat.UI.InboxFragment$1.done(InboxFragment.java:67)
            at com.ghostriley.sgt.ghostchat.UI.InboxFragment$1.done(InboxFragment.java:55)
            at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:115)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5930)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

有没有办法调用InboxFragment.onResume()?

1 个答案:

答案 0 :(得分:0)

您尚未将Fragment附加到Activity。你需要的东西是:

getFragmentManager()
    .beginTransaction()
    .add(android.R.id.content, inboxFragment)
    .commit();