为什么在设置参数的片段应用程序崩溃应用程序、片段、参数

2023-09-04 03:07:46 作者:戒不掉嘚溫柔

我想从一个活动转移到一个片段的参数,添加到另一个。该应用程序崩溃,并说夸大的这个问题。

这是我的错误日志:

  9月9号至26日:22:03.968 1524年至1524年/ com.example.user.unchained E / AndroidRuntime:致命异常:主要
工艺:com.example.user.unchained,PID:1524
java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.example.user.unchained / com.example.user.unchained.HomesActivity}:android.view.InflateException:二进制XML文件中的行#36:错误充气类片段
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:135)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:136)
 

的code相关部件

 最后片段HF =新HeaderFragment();

    捆绑I = getArguments();
    捆绑II =新包();

    查看标题= inflater.inflate(R.layout.fragment_header_fragement,NULL);
    查看尾部= inflater.inflate(R.layout.fragment_footer,NULL);


    mDrawerListView.addHeaderView(头);
    mDrawerListView.addFooterView(页脚);

    ii.putString(ID,i.getString(ID));
    ii.putString(姓名,i.getString(名称));
    ii.putString(imgUrl的,i.getString(imgUrl的));

    hf.setArguments(二);

    header.addOnAttachStateChangeListener(新View.OnAttachStateChangeListener(){
        @覆盖公共无效onViewDetachedFromWindow(视图v){}
        @覆盖公共无效onViewAttachedToWindow(视图v){
            getChildFragmentManager()
                    .beginTransaction()
                    。新增(R.id.fragment,HF)
                    。承诺();
        }
    });
 

解决方案

它看起来像你没有指定根查看。

按照的Andr​​oid文档:

  

公开查看的inflate(INT资源的ViewGroup根)

     

从充气指定一个新的视图层次   XML资源。抛出InflateException如果存在一个错误。

     

参数   资源= ID为一个XML布局资源加载(如R.layout.main_page)

     

根=可选的观点是产生层次的父。返回充气层次的根视图。

     NSIS NSIS下载 v2.51 中文版 起点软件园

如果根供给,这是根查看;否则它是膨胀的XML文件的根

由于您没有定义任何声明根视图,您是从将头作为根查看,然后立即切换到页脚作为根查看。

I’m trying to get the arguments transferred from an activity to a fragment, to put into another. The app crashed and said that problem of inflating.

This my error log :

09-26 09:22:03.968    1524-1524/com.example.user.unchained E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.user.unchained, PID: 1524
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.unchained/com.example.user.unchained.HomesActivity}: android.view.InflateException: Binary XML file line #36: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)

The concerned part of code

    final Fragment hf = new HeaderFragment();

    Bundle i = getArguments();
    Bundle ii = new Bundle();

    View header = inflater.inflate(R.layout.fragment_header_fragement, null);
    View footer = inflater.inflate(R.layout.fragment_footer, null);


    mDrawerListView.addHeaderView(header);
    mDrawerListView.addFooterView(footer);

    ii.putString("Id",i.getString("Id"));
    ii.putString("Name",i.getString("Name"));
    ii.putString("ImgUrl",i.getString("ImgUrl"));

    hf.setArguments(ii);

    header.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override public void onViewDetachedFromWindow(View v) {}
        @Override public void onViewAttachedToWindow(View v) {
            getChildFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment, hf)
                    .commit();
        }
    });

解决方案

It looks like you're not specifying the root View.

According to the Android documentation:

public View inflate (int resource, ViewGroup root)

Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.

Parameters resource = ID for an XML layout resource to load (e.g., R.layout.main_page)

root = Optional view to be the parent of the generated hierarchy. Returns The root View of the inflated hierarchy.

If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.

Since you didn't define a root View in either statement, you are going from header as your root View and then immediately switching to footer as the root View.