有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将背景色应用为xml布局文件时发生java错误

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

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

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

        安卓:textStyle="bold"
        tools:text="subject" />

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

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

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

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

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

</LinearLayout>

我有一个可绘制的背景xml文件,颜色为蓝色。xml

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

</shape> 

如果我将线性布局背景颜色设置为安卓: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
                                                                        安卓.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
                                                                            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:543)
                                                                            at 安卓.view.LayoutInflater.inflate(LayoutInflater.java:427)
                                                                            at com.example.saurabh.helper.TimeTable.TitleAdapter.getView(TitleAdapter.java:33)
                                                                            at 安卓.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                            at 安卓.widget.ListView.makeAndAddView(ListView.java:1876)
                                                                            at 安卓.widget.ListView.fillDown(ListView.java:702)
                                                                            at 安卓.widget.ListView.fillFromTop(ListView.java:763)
                                                                            at 安卓.widget.ListView.layoutChildren(ListView.java:1685)
                                                                            at 安卓.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.support.v4.view.ViewPager.onLayout(ViewPager.java:1769)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at 安卓.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at 安卓.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at 安卓.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at 安卓.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                            at 安卓.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                            at 安卓.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                            at 安卓.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                            at com.安卓.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2680)
                                                                            at 安卓.view.View.layout(View.java:16672)
                                                                            at 安卓.view.ViewGroup.layout(ViewGroup.java:5438)
                                                                            at 安卓.view.ViewRootImpl.performLayout(ViewRootImpl.java:2207)
                                                                            at 安卓.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1963)
                                                                            at 安卓.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
                                                                            at 安卓.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6064)
                                                                            at 安卓.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
                                                                            at 安卓.view.Choreographer.doCallbacks(Choreographer.java:672)
                                                                            at 安卓.view.Choreographer.doFrame(Choreographer.java:608)
                                                                            at 安卓.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
                                                                            at 安卓.os.Handler.handleCallback(Handler.java:742)
                                                                            at 安卓.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at 安卓.os.Looper.loop(Looper.java:157)
                                                                            at 安卓.app.ActivityThread.main(ActivityThread.java:5571)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
                                                                            at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:652)
                                                                         Caused by: 安卓.view.InflateException: Binary XML file line #0: Error inflating class <unknown>
                                                                            at 安卓.view.LayoutInflater.createView(LayoutInflater.java:649)
                                                                            at com.安卓.internal.policy.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:58)
                                                                            at 安卓.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文件(应用程序):

apply plugin: 'com.安卓.application'

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

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

这是清单文件:

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

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

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

如果我不把bg应用为可绘制的,效果很好。 此外,如果我将所有活动和布局复制粘贴到一个新项目中,新应用程序运行良好,没有任何错误。 请大家帮帮我! 询问是否需要更多代码。 提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    我猜你错过了一个参数

    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> 
    

    试着让我知道

  2. # 2 楼答案

    试着移动蓝色。将xml改为res/color/而不是res/drawable,看看它是否能解决您的问题