当我尝试从此片段中展开布局时出错

时间:2017-09-03 14:29:05

标签: android android-layout android-fragments layout-inflater

我已经在我的应用程序中使用过此方法,但它运行正常。我现在正在做同样的事情,但是我收到一条错误消息,上面写着:错误输出类android.view.View 我使用与前一个屏幕相同的基本XML布局。它有2个碎片。一个向上告诉用户他/她已登录,然后是我的新片段(表格)。然而,下面的一个似乎是问题。这是我的基本布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bignerdranch.android.personaltrainer.CustomerListActivity">

    <LinearLayout
        android:layout_width="304dp"
        android:layout_height="wrap_content"
        android:id="@+id/logged_in_fragment_container"
        android:orientation="horizontal"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="8dp"
        tools:layout_height="18dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/list_fragment_container"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

    </LinearLayout>

    <android.support.constraint.ConstraintLayout
        android:id="@+id/list_fragment_container"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="34dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

这是顶级的Fragement:不知道你是否真的需要这个但是在这里

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/container_for_loggedin_textview"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:padding="5dp"
             tools:context="com.bignerdranch.android.personaltrainer.LoggedInFragment">

    <TextView
        android:id="@+id/logged_in_text_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:text="You are logged in"
        android:textAlignment="center"
        android:textSize="18sp"
        android:textStyle="bold|italic"/>

</FrameLayout>

以下是片段我在尝试充气时遇到问题:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView_new_customer_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="16dp"
        android:text="@string/new_customer_title"
        android:textSize="24sp"
        android:textStyle="bold"
        android:typeface="serif"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginStart="16dp"/>

    <ImageView
        android:id="@+id/new_customer_pic"
        android:layout_width="84dp"
        android:layout_height="82dp"
        android:layout_marginLeft="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="60dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher"/>

    <EditText
        android:id="@+id/tv_first_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="152dp"
        android:ems="10"
        android:hint="First Name"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <EditText
        android:id="@+id/tv_last_name"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:hint="Last Name"
        android:inputType="textPersonName"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_first_name"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"/>

    <EditText
        android:id="@+id/tv_city"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:ems="10"
        android:hint="City"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="5dp"
        app:layout_constraintTop_toBottomOf="@+id/tv_address"/>

    <Spinner
        android:id="@+id/spinner_state"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:background="@color/colorPrimary"
        android:entries="@array/states"
        app:layout_constraintBottom_toBottomOf="@+id/tv_city"
        app:layout_constraintHorizontal_bias="0.433"
        app:layout_constraintLeft_toRightOf="@+id/tv_city"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_last_name"
        app:layout_constraintVertical_bias="0.913"/>

    <EditText
        android:id="@+id/tv_zip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="5dp"
        android:ems="10"
        android:hint="ZipCode"
        android:inputType="textPersonName"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_city"
        android:layout_marginStart="16dp"/>

    <View
        android:layout_width="0dp"
        android:layout_height="2dp"
        android:background="@color/colorPrimaryDark"
        android:backgroundTint="@drawable/divider"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/view"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintTop_toBottomOf="@+id/tv_zip"/>

    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cancel_button"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/view"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"/>

    <Button
        android:id="@+id/add_customer_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/add_button"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/view"
        android:layout_marginStart="64dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintLeft_toRightOf="@+id/camera_button"/>


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/camera_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_camera_black_24dp"
        app:backgroundTint="@color/colorPrimaryDark"
        app:elevation="6dp"
        app:fabSize="auto"
        app:rippleColor="?android:attr/colorBackground"
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@+id/view"
        android:layout_marginStart="64dp"
        app:layout_constraintLeft_toRightOf="@+id/cancel_button"
        android:layout_marginLeft="64dp"/>

    <Button
        android:id="@+id/billing_info_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/billing_info_button"
        android:layout_marginRight="16dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="60dp"
        android:layout_marginEnd="16dp"/>

    <EditText
        android:id="@+id/tv_address"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginTop="4dp"
        android:ems="10"
        android:hint="Street Address"
        android:inputType="textPersonName"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_last_name"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"/>

</android.support.constraint.ConstraintLayout>

以下是我使用片段管理器的基本活动:

package com.bignerdranch.android.personaltrainer;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;


public class NewCustomerActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.logged_in_fragment_container);

        if (fragment == null){
            fragment = new LoggedInFragment();
            fm.beginTransaction()
                    .add(R.id.logged_in_fragment_container, fragment)
                    .commit();
        }

        // don't be confused by "list_fragment_container" it's the same container used for the recyclerview
        Fragment fragment2 = fm.findFragmentById(R.id.list_fragment_container);

        if (fragment2 == null){
            fragment2 = new NewCustomerFragment();
            fm.beginTransaction()
                    .add(R.id.list_fragment_container, fragment2)
                    .commit();
        }

    }
}

以下是抛出错误的片段:

package com.bignerdranch.android.personaltrainer;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.bignerdranch.android.personaltrainer.model.Customer;

/**
 * Created by patricgekoski on 9/3/17.
 */

public class NewCustomerFragment extends Fragment {

    private Customer mCustomer;
    private Button mBillingInfoButton;
    private Button mCancelButton;
    private Button mAddButton;
    private FloatingActionButton mCameraButton;
    private ImageView mCustomerPic;
    private TextView mFirstName;
    private TextView mLastName;
    private TextView mAddress;
    private TextView mCity;
    private Spinner mState;
    private TextView mZip;

    public NewCustomerFragment(){
        // empty constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent,
                             Bundle savedInstanceState){

// !!!!!!!这下一行正在发生错误!!!!!!!!!!!

        View view = inflater.inflate(R.layout.fragment_new_customer, parent, false);


        mBillingInfoButton.findViewById(R.id.billing_info_button);
        mCancelButton.findViewById(R.id.cancel_button);
        mAddButton.findViewById(R.id.add_customer_button);
        mCameraButton.findViewById(R.id.camera_button);
        mCustomerPic.findViewById(R.id.new_customer_pic);
        mFirstName.findViewById(R.id.tv_first_name);
        mLastName.findViewById(R.id.tv_last_name);
        mAddress.findViewById(R.id.tv_address);
        mCity.findViewById(R.id.tv_city);
        mState.findViewById(R.id.spinner_state);
        mZip.findViewById(R.id.tv_zip);

        return view;

    }
}

最后这是我的堆栈跟踪:至少部分是

09-03 10:25:50.996 29649-29649/com.bignerdranch.android.personaltrainer E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                          Process: com.bignerdranch.android.personaltrainer, PID: 29649
                                                                                          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bignerdranch.android.personaltrainer/com.bignerdranch.android.personaltrainer.NewCustomerActivity}: android.view.InflateException: Binary XML file line #112: Binary XML file line #112: Error inflating class android.view.View
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                              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 #112: Binary XML file line #112: Error inflating class android.view.View
                                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                                                                                              at com.bignerdranch.android.personaltrainer.NewCustomerFragment.onCreateView(NewCustomerFragment.java:43)
                                                                                              at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
                                                                                              at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                                              at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
                                                                                              at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
                                                                                              at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
                                                                                              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
                                                                                              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                                                                                              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554)
                                                                                              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
                                                                                              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                                                                                              at android.app.Activity.performStart(Activity.java:6253)
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                              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 #112: Error inflating class android.view.View
                                                                                              at android.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                                                              at android.view.LayoutInflater.onCreateView(LayoutInflater.java:677)
                                                                                              at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:68)
                                                                                              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.rInflate(LayoutInflater.java:835)
                                                                                              at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
                                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                                                                                              at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
                                                                                              at com.bignerdranch.android.personaltrainer.NewCustomerFragment.onCreateView(NewCustomerFragment.java:43) 
                                                                                              at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641) 
                                                                                              at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153) 
                                                                                              at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063) 
                                                                                              at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388) 
                                                                                              at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:554) 
                                                                                              at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) 
                                                                                              at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237) 
                                                                                              at android.app.Activity.performStart(Activity.java:6253) 
                                                                                              at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
                                                                                              at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                              at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                              at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                              at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                              at android.os.Looper.loop(Looper.java:148) 
                                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                                              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) 

提前谢谢

2 个答案:

答案 0 :(得分:0)

我明白了。这是一个非常微妙的错误。我试图使用&#34; backgroundTint&#34;我的布局中的属性,但是引入的级别高于我的最小API 19。

答案 1 :(得分:0)

尝试注释掉该片段中的所有代码,从inflate开始到findView。如果问题仍然存在,可能会出现堆栈跟踪建议的NumbersActivity问题,请尝试注释掉调用NumbersFragment的部分以获取更多提示。希望能引导您走向解决方案。