尝试夸大LayoutInflater时出错

时间:2016-04-27 04:11:22

标签: android

我正试图给布局充气" panel_stream"进入我当前的视图,但它崩溃了错误消息:

  

android.view.InflateException:二进制XML文件行#2:二进制XML   文件行#2:错误膨胀类

 public void loadContent() {

    LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup parent = (ViewGroup)findViewById(R.id.mainRLayout);

    //this is the line where it crashes
    View v = vi.inflate(R.layout.panel_stream, parent);

 TextView title =  (TextView) v.findViewById(R.id.panelTitle);
    title.setText("your text");
    TextView author = (TextView) v.findViewById(R.id.panelAuthor);
    author.setText("your text");
    TextView content = (TextView) v.findViewById(R.id.panelContent);
    content.setText("your text");
    ViewGroup insertPoint = (ViewGroup) findViewById(R.id.contentHost);
    Log.d("LOGGINGSTUFF","5");
    insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

}

activity_board_stream_view.xml(我试图发布的视图):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.company.panels.BoardView"
android:background="@color/background"
android:id="@+id/mainRLayout">
<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/contentScroller" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:id="@+id/contentHost">
    </LinearLayout>
</ScrollView>
</RelativeLayout>

panel_stream.xml:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
    android:layout_margin="0dp"
    android:elevation="4dp"
android:clickable="true">

<RelativeLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/panel_header_shape"
    android:contextClickable="true"
    android:elevation="4dp"
    android:id="@+id/header"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title Here"
        android:id="@+id/panelTitle"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_centerVertical="true"
        android:elevation="4dp"
        android:textColor="#FFFFFF"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Author"
        android:id="@+id/panelAuthor"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:layout_alignBottom="@+id/panelTitle"
        android:textColor="#eeeeee"
        android:textStyle="bold|italic" />

</RelativeLayout>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/header"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:background="@drawable/panel_shape">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Test"
        android:textColor="#000000"
        android:background="@drawable/panel_shape"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_margin="10dp"
        android:id="@+id/panelContent" />

</RelativeLayout>

</RelativeLayout>

崩溃日志:

04-26 21:57:15.486 16137-16137/? E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.company.panels, PID: 16137
                                               android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                                                   at com.company.panels.BoardView.loadContent(BoardView.java:96)
                                                   at com.company.panels.BoardView.onCreateOptionsMenu(BoardView.java:42)
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2852)
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341)
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258)
                                                   at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1312)
                                                   at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1592)
                                                   at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:91)
                                                   at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:130)
                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                   at android.os.Looper.loop(Looper.java:148)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5422)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                   at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694)
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762)
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                   at com.company.panels.BoardView.loadContent(BoardView.java:96) 
                                                   at com.company.panels.BoardView.onCreateOptionsMenu(BoardView.java:42) 
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2852) 
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341) 
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1312) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1592) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:91) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:130) 
                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                Caused by: java.lang.reflect.InvocationTargetException
                                                   at java.lang.reflect.Constructor.newInstance(Native Method)
                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:619)
                                                   at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                   at com.company.panels.BoardView.loadContent(BoardView.java:96) 
                                                   at com.company.panels.BoardView.onCreateOptionsMenu(BoardView.java:42) 
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2852) 
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341) 
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1312) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1592) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:91) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:130) 
                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                Caused by: java.lang.ArrayIndexOutOfBoundsException: length=522; index=1332
                                                   at android.content.res.TypedArray.getValueAt(TypedArray.java:1132)
                                                   at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:661)
                                                   at android.view.View.<init>(View.java:4207)
                                                   at android.view.ViewGroup.<init>(ViewGroup.java:573)
                                                   at android.widget.RelativeLayout.<init>(RelativeLayout.java:248)
                                                   at android.widget.RelativeLayout.<init>(RelativeLayout.java:244)
                                                   at android.widget.RelativeLayout.<init>(RelativeLayout.java:240)
                                                   at java.lang.reflect.Constructor.newInstance(Native Method) 
                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
                                                   at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58) 
                                                   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:694) 
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:762) 
                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
                                                   at com.company.panels.BoardView.loadContent(BoardView.java:96) 
                                                   at com.company.panels.BoardView.onCreateOptionsMenu(BoardView.java:42) 
                                                   at android.app.Activity.onCreatePanelMenu(Activity.java:2852) 
                                                   at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:341) 
                                                   at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85) 
                                                   at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:258) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.preparePanel(AppCompatDelegateImplV7.java:1312) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.doInvalidatePanelMenu(AppCompatDelegateImplV7.java:1592) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7.access$100(AppCompatDelegateImplV7.java:91) 
                                                   at android.support.v7.app.AppCompatDelegateImplV7$1.run(AppCompatDelegateImplV7.java:130) 
                                                   at android.os.Handler.handleCallback(Handler.java:739) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                   at android.os.Looper.loop(Looper.java:148) 
                                                   at android.app.ActivityThread.main(ActivityThread.java:5422) 
                                                   at java.lang.reflect.Method.invoke(Native Method)

3 个答案:

答案 0 :(得分:0)

您尚未在布局xml文件中定义名称空间,这会导致错误。

在布局文件的根标记中包含名称空间,

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    ...>

修改:

orientation代码中删除RelativeLayout属性,因为它只能与LinearLayout一起使用。

答案 1 :(得分:0)

你必须像这样编码。

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    ViewGroup parent = (ViewGroup)findViewById(R.id.contentHost);

    //this is the line where it crashes
    View v = inflater.inflate(R.layout.activity_main2, parent);

    TextView title =  (TextView) v.findViewById(R.id.panelTitle);
    title.setText("Title");
    TextView author = (TextView) v.findViewById(R.id.panelAuthor);
    author.setText("author");
    TextView content = (TextView) v.findViewById(R.id.panelContent);
    content.setText("content");

你的activity_mainstream布局应该是这样的。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.company.panels.BoardView"
android:id="@+id/contentScroller">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
    <FrameLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="0dp"
        android:id="@+id/contentHost">
    </FrameLayout>
</LinearLayout></ScrollView>

答案 2 :(得分:-1)

至少第一行需要包含:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
"your other attributes here">