如何处理错误:java.lang.IllegalStateException:内容视图尚未创建 [英] How to handle Error: java.lang.IllegalStateException: Content view not yet created

查看:1124
本文介绍了如何处理错误:java.lang.IllegalStateException:内容视图尚未创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

InboxFragment是MainActivity的片段。
在MainActivity(在动作条这三个点)的操作栏菜单中,我增加了一个选项刷新收件箱中,通过创建InboxFragment的对象调用一个函数retrieveMessages()。
retrieveMessages是InboxFragment一个成员功能​​(它早些时候片段onCreateView功能是获取消息,然后我重构它来提取code重用它MainActivity)。

InboxFragment is a fragment of MainActivity. In action bar menu of the MainActivity (those three dots on the ActionBar), I added an option Refresh Inbox, which calls a function retrieveMessages() by creating an object of InboxFragment. retrieveMessages is a 'member' function in InboxFragment (which was earlier in onCreateView function of the fragment to retrieve messages, then I refactored it to extract code to reuse it in MainActivity).

我以这个意图使用重新创建MainActivity试过,MainActivity.class作为意向的参数,但是这是一种丑陋的刷新。如何处理上述错误?

I have tried using intent to re-create MainActivity by using this, MainActivity.class as parameters of the Intent but that is kind of an ugly refresh. How to handle the above error?

下面是需要code片:
在MainActivity:

Here are the required pieces of code: In 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 -

In 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的:

Here is the 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)

有什么办法CAL InboxFragment.onResume()?

Is there any way to cal InboxFragment.onResume() ?

推荐答案

您还没有连接你的片段你的活动。你需要的线沿线的东西:

You haven't attached your Fragment to your Activity. You'll need something along the lines of:

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

这篇关于如何处理错误:java.lang.IllegalStateException:内容视图尚未创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆