当前位置: 首页 > 知识库问答 >
问题:

二进制XML文件第20行:inflating类片段时出错

欧旻
2023-03-14

我想添加一个对象LinearLayout,但它抛出一个错误错误行:在me.solo_team.futureleader.ui.news.NewsFragment.onCreateView(NewsFragment.java:62)(addElement(uris.get(i),names.get(i));)

新闻片段

    private LayoutInflater inflater;
    private ViewGroup container;
    private LinearLayout nw;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        View root = inflater.inflate(R.layout.fragment_news, container, false);
        nw = root.findViewById(R.id.news_list);
        this.inflater = inflater;
        this.container = container;
        List<String> uris = Arrays.asList(
           // links...
);
        List<String> names = Arrays.asList(
             // names                                 
);
        for(int i =0;i<9;i++){
            addElement(uris.get(i),names.get(i));
        }
        return root;
    }

    private void addElement(String uri,String name){
        FrameLayout row = (FrameLayout) inflater.inflate(R.layout.news_news, container,false);
        ConstraintLayout cn = row.findViewById(R.id.news_obj);
        Picasso.get().load(uri).into((ImageView)cn.getChildAt(1));
        ((TextView)cn.getChildAt(2)).setText(name);
        nw.addView(row);
    }

fragment_news:

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.news.NewsFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:id="@+id/news_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent">

        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

mews_新闻。xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/news_obj"
    android:layout_width="match_parent"
    android:layout_height="180dp"
    android:layout_margin="15dp"
    android:orientation="vertical"
    app:contentPadding="10dp">


    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gray_gradient_with_corners"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/news_obj_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginRight="10dp"
        android:background="@drawable/gray_gradient_with_corners"
        android:src="@drawable/gray_gradient"
        app:layout_constraintBottom_toTopOf="@id/news_obj_name"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/news_obj_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="10dp"
        android:autoSizeMaxTextSize="20sp"
        android:autoSizeMinTextSize="12sp"
        android:autoSizeStepGranularity="2sp"
        android:autoSizeTextType="uniform"
        android:text="news text"
        android:textColor="@color/text_color1"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@id/news_obj"
        app:layout_constraintLeft_toLeftOf="@id/news_obj" />
</androidx.constraintlayout.widget.ConstraintLayout>

错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process:_xecutor.java:68_, PID: 23516hread.java:1808RuntimeException:无法启动活动ComponentInfo{android.os.android.app.andler.dispatch}:android.view.InflateException:二进制XML文件行#20:二进制XML文件行#20:错误inflating类片段android.os.ActivityTooper.loop启动活动(ActivityTooper.java:193)在android.app.ActivityThread.main启动活动(ActivityThread.java:6669)在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)在android.app.servertransaction.TransactionExecutor.execute回调(TransactionExecutor.java:108)在android.app.servertransaction.TransactionExecutor.execute(TransactionEme.solo)在team.futureleaderActivityThread$H. handleMessage(ActivityTjava.lang.)在me.soloHteam.futureleader/me.solo_team.futureleader.MainActivity消息(H)在android.app.Lhread.perform(Lhread.java:2913)在android.app.ActivityThread.handle(ActivityThread.java:3048)在com. android.InflateException:二进制XML文件行#20:二进制XML文件行#20:inflating类片段引起的错误:android. view。InflateException:二进制XML文件行#20:inflating类片段引起的错误:java. lang。ArrayIndexOutOfBoundsException:长度=8;java. util上的index=8。数组$ArrayList. get(Arrays. java: 3769)在我这里。solo_team未来领导者. ui. news。新闻片段. onCreateView(NewsFragment. java: 62)在androidx.碎片. app. FragmentStateManager. createView(Fragment. java: 2963)在androidx.碎片. app. FragmentStateManager. createView(FragmentStateManager. java: 518)LayoutInflater. createViewFromTag(LayoutInflater. java: 730)在android. view。LayoutInflater. rInflate(LayoutInflater. java: 863)在android. view。LayoutInflater. rInflateUNICEF(LayoutInflater. java: 824)在android. view。LayoutInflater.膨胀(LayoutInflater. java: 515)在android. view。LayoutInflater.膨胀(LayoutInflater. java: 423)在android. view。LayoutInflater.膨胀(LayoutInflater. java: 374)在androidx. appcompat. app. AppCompatateInplate. setContentView(AppCompatActive. java: 706)在androidx. appcompat. app. AppCompatActive. setContentView(AppCompatActive. java: 195)在我。solo_team未来领导者。

共有2个答案

闻飞跃
2023-03-14

这种类型的崩溃通常具有误导性,因为您只能在LogCat中看到最近(或最不最近)的异常。

每当我遇到导致应用程序崩溃的错误时,我都会故意忽略第一次崩溃并重复之前的行为以触发第二次崩溃。然后突然间,LogCat报告的异常完全不同,通常指向错误的正确来源。是的,这正是疯狂的定义,因为你在重复相同的行为,但得到不同的结果。但这是Android。疯狂来了。

易京
2023-03-14

原因:java。lang.ArrayIndexOutOfBoundsException:长度=8;java的索引=8。util。数组$ArrayList。把(Arrays.java:3769)对准我。solo_团队。未来领导者。用户界面。消息新闻片段。以下是崩溃的真正原因^

在我看来你的

for(int i =0;i<9;i++){
            addElement(uris.get(i),names.get(i));
        }

尝试获取超出界限的项目。应该是我

你也可以设置i

 类似资料: