将背景颜色应用为xml布局文件时出错

时间:2018-04-17 09:16:48

标签: java android xml android-layout android-studio

我有一个名为item.xml代码的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
       <TextView
        android:id="@+id/subject"
        android:layout_width="match_parent"

        android:gravity="center"
        android:textColor="@android:color/background_light"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textColorLink="@android:color/white"

        android:textStyle="bold"
        tools:text="subject" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="match_parent"

        android:gravity="center"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textColorLink="@android:color/white"
        tools:text="desc" />

    <TextView
        android:id="@+id/batch"
        android:layout_width="match_parent"

        android:gravity="center"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textColorLink="@android:color/white"
        tools:text="batch" />

    <TextView
        android:id="@+id/faculty"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:textColorLink="@android:color/white"
        tools:text="faculty" />

</LinearLayout>

我有一个可绘制的背景xml文件color_blue.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#1E88E5"/>
    <stroke android:width="2dp"/>
    <corners android:radius="10dp"/>

</shape> 

如果我将线性布局背景颜色设置为android:background =“@ color / grey”或十六进制代码,则没有问题。但是当我将背景应用为@ drawable / color_blue.xml时,错误发生在:

04-17 14:25:20.002 11970-11970/com.example.saurabh.helper E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.saurabh.helper, PID: 11970
                                                                        android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                            at com.example.saurabh.helper.TimeTable.TitleAdapter.getView(TitleAdapter.java:33)
                                                                            at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                            at android.widget.ListView.makeAndAddView(ListView.java:1876)
                                                                            at android.widget.ListView.fillDown(ListView.java:702)
                                                                            at android.widget.ListView.fillFromTop(ListView.java:763)
                                                                            at android.widget.ListView.layoutChildren(ListView.java:1685)
                                                                            at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1769)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2680)
                                                                            at android.view.View.layout(View.java:16672)
                                                                            at android.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2207)
                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1963)
                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6064)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:608)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                                                                            at android.os.Handler.handleCallback(Handler.java:742)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:157)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:5571)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
                                                                         Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                                                                            at android.view.LayoutInflater.createView(LayoutInflater.java:649)
                                                                            at com.android.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:698)

我认为标题适配器没有问题,但这里是代码:

public class TitleAdapter extends ArrayAdapter<Title> {
public TitleAdapter(Context context, ArrayList<Title> titles) {
    super(context, 0, titles);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View listItemView = convertView;
    Title currentTitle = getItem(position);

    if(currentTitle.getLtp() == 0) {
        //if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);
        //}

        TextView subject = listItemView.findViewById(R.id.subject);
        subject.setText(currentTitle.getSubjectName());

        TextView desc = listItemView.findViewById(R.id.desc);
        desc.setText(currentTitle.getDesc());

        TextView batch = listItemView.findViewById(R.id.batch);
        batch.setText(currentTitle.getBatch());

        TextView faculty = listItemView.findViewById(R.id.faculty);
        faculty.setText(currentTitle.getFacultyName());

        TextView startTime = listItemView.findViewById(R.id.start_time);
        startTime.setText(currentTitle.getStartTime());

        TextView endTime = listItemView.findViewById(R.id.end_time);
        endTime.setText(currentTitle.getEndTime());

        return listItemView;
    }



    else if(currentTitle.getLtp() == 1) {
        //if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_tutorial, parent, false);
        //}
        //1st block
        TextView subject1 = listItemView.findViewById(R.id.subject_tut_1);
        subject1.setText(currentTitle.getSubjectName1());

        TextView desc1 = listItemView.findViewById(R.id.desc_tut_1);
        desc1.setText(currentTitle.getDesc1());

        TextView batch12 = listItemView.findViewById(R.id.batch12);
        batch12.setText(currentTitle.getBatch12());

        TextView faculty1 = listItemView.findViewById(R.id.faculty_tut_1);
        faculty1.setText(currentTitle.getFacultyName1());

        //2nd block
        TextView subject2 = listItemView.findViewById(R.id.subject_tut_2);
        subject2.setText(currentTitle.getSubjectName2());

        TextView desc2 = listItemView.findViewById(R.id.desc_tut_2);
        desc2.setText(currentTitle.getDesc2());

        TextView batch34 = listItemView.findViewById(R.id.batch34);
        batch34.setText(currentTitle.getBatch34());

        TextView faculty2 = listItemView.findViewById(R.id.faculty_tut_2);
        faculty2.setText(currentTitle.getFacultyName2());

        TextView startTime = listItemView.findViewById(R.id.start_time);
        startTime.setText(currentTitle.getStartTime());

        TextView endTime = listItemView.findViewById(R.id.end_time);
        endTime.setText(currentTitle.getEndTime());

        return listItemView;
    }

    //if(listItemView == null && currentTitle.getLtp() == 2) {
    else {
        //if(listItemView == null) {
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_practical, parent, false);
        //}
        //1st block
        TextView subject1 = listItemView.findViewById(R.id.subject_prac_1);
        subject1.setText(currentTitle.getSubjectName1());

        TextView desc1 = listItemView.findViewById(R.id.desc_prac_1);
        desc1.setText(currentTitle.getDesc1());

        TextView batch1 = listItemView.findViewById(R.id.batch1);
        batch1.setText(currentTitle.getBatch1());

        TextView faculty1 = listItemView.findViewById(R.id.faculty_prac_1);
        faculty1.setText(currentTitle.getFacultyName1());

        //2nd block
        TextView subject2 = listItemView.findViewById(R.id.subject_prac_2);
        subject2.setText(currentTitle.getSubjectName2());

        TextView desc2 = listItemView.findViewById(R.id.desc_prac_2);
        desc2.setText(currentTitle.getDesc2());

        TextView batch2 = listItemView.findViewById(R.id.batch2);
        batch2.setText(currentTitle.getBatch2());

        TextView faculty2 = listItemView.findViewById(R.id.faculty_prac_2);
        faculty2.setText(currentTitle.getFacultyName2());

        //3rd block
        TextView subject3 = listItemView.findViewById(R.id.subject_prac_3);
        subject3.setText(currentTitle.getSubjectName3());

        TextView desc3 = listItemView.findViewById(R.id.desc_prac_3);
        desc3.setText(currentTitle.getDesc3());

        TextView batch3 = listItemView.findViewById(R.id.batch3);
        batch3.setText(currentTitle.getBatch3());

        TextView faculty3 = listItemView.findViewById(R.id.faculty_prac_3);
        faculty3.setText(currentTitle.getFacultyName3());

        //4th block
        TextView subject4 = listItemView.findViewById(R.id.subject_prac_4);
        subject4.setText(currentTitle.getSubjectName4());

        TextView desc4 = listItemView.findViewById(R.id.desc_prac_4);
        desc4.setText(currentTitle.getDesc4());

        TextView batch4 = listItemView.findViewById(R.id.batch4);
        batch4.setText(currentTitle.getBatch4());

        TextView faculty4 = listItemView.findViewById(R.id.faculty_prac_4);
        faculty4.setText(currentTitle.getFacultyName4());

        TextView startTime = listItemView.findViewById(R.id.start_time);
        startTime.setText(currentTitle.getStartTime());

        TextView endTime = listItemView.findViewById(R.id.end_time);
        endTime.setText(currentTitle.getEndTime());

        return listItemView;
    }
}

}

此外,这是我的Gradle文件(app):

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.saurabh.helper"
    minSdkVersion 16
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner"
}
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro'
    }
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:27.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso- 
core:3.0.1'
}

以下是清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.saurabh.helper">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".addTTActivity"
         />
    <activity
        android:name=".StudentLoginActivity"
       />
    <activity
        android:name=".FacultyLoginActivity"
       />
    <activity
        android:name=".MainActivity"
       >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SignUpStudent"
        />
    <activity android:name=".TtActivity"></activity>
</application>

如果我不将bg作为drawable使用,可以正常工作。 此外,如果我将所有活动和布局复制粘贴到新项目中,新应用程序运行正常,没有任何错误。 请各位帮助我! 询问是否需要更多代码。 提前谢谢

2 个答案:

答案 0 :(得分:0)

尝试将blue_color.xml移动到res / color /而不是res / drawable,看看它是否解决了你的问题。

答案 1 :(得分:0)

我猜你错过了其中一个参数

android:shape="rectangle"

示例代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <solid android:color="#1E88E5"/>
    <stroke android:width="2dp"/>
    <corners android:radius="10dp"/>
</shape> 

试着让我知道。