抛出:IllegalArgumentException:未找到ID为0x7f07003c片段视图 [英] IllegalArgumentException: No view found for id 0x7f07003c for fragment

查看:187
本文介绍了抛出:IllegalArgumentException:未找到ID为0x7f07003c片段视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的碎片。我想简单的片段例子。但它抛出的错误。我不知道什么是错的。

 公共类NewActivity延伸活动{
    INT mStackLevel = 1;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState == NULL){
            片段newFragment = NewFragment
                    .newInstance(mStackLevel);
            FragmentTransaction英尺= getFragmentManager()调用BeginTransaction()。
            ft.add(R.id.simple_fragment,newFragment).commit();
        }其他{
            mStackLevel = savedInstanceState.getInt(级别);
        }
    }
    @覆盖
    公共无效的onSaveInstanceState(捆绑outState){
        super.onSaveInstanceState(outState);
        outState.putInt(级别,mStackLevel);
    }
    公共静态类NewFragment扩展片段{
        INT MNUM;
        上下文语境;        静态的newInstance NewFragment(INT NUM){
            NewFragment F =新NewFragment();            //供应NUM输入作为参数。
            捆绑ARGS =新包();
            args.putInt(民,NUM);
            f.setArguments(参数);            返回F;
        }        @覆盖
        公共无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            MNUM = getArguments()!= NULL? getArguments()调用getInt(民):1。
            上下文= getActivity();
        }        @覆盖
        公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                捆绑savedInstanceState){
            视图V = inflater.inflate(R.layout.example_fragment,集装箱,
                    假);
            返回伏;
        }
    }}

我的logcat所示:

 了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.FragmentExample / com.example.FragmentExample.NewActivity}:java.lang.IllegalArgumentException异常:未找到ID视图0x7f07003c的片段NewFragment {40ff0850#0 ID = 0x7f07003c}

任何帮助将是非常美联社preciated。

感谢。


解决方案

  ft.add(R.id.simple_fragment,newFragment).commit();

您错过了的setContentView 为您的活动。没有视图层次不能添加您的片段。

请注意:您的片段是由活动主办。所以,你需要首先设置内容的活动。

I am new to Fragments. I tried simple fragment example. But it throws the error. I don't know what is wrong.

public class NewActivity extends Activity {
    int mStackLevel = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            Fragment newFragment = NewFragment
                    .newInstance(mStackLevel);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(R.id.simple_fragment, newFragment).commit();
        } else {
            mStackLevel = savedInstanceState.getInt("level");
        }
    }
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("level", mStackLevel);
    }
    public static class NewFragment extends Fragment {
        int mNum;
        Context context;

        static NewFragment newInstance(int num) {
            NewFragment f = new NewFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
            context = getActivity();
        }

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

}

My logcat shows:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.FragmentExample/com.example.FragmentExample.NewActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f07003c for fragment NewFragment{40ff0850 #0 id=0x7f07003c}

Any help will be highly appreciated.

Thanks.

解决方案

ft.add(R.id.simple_fragment, newFragment).commit();

you missed setContentView for your Activity. Your Fragment can not be added without a View hierarchy.

Note : Your Fragment is hosted by an activity. So you need to set the content to the activity first.

这篇关于抛出:IllegalArgumentException:未找到ID为0x7f07003c片段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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