应用程序适用于API 26但在API 23(marshmallow)上崩溃

时间:2018-04-14 15:57:18

标签: java android

应用程序工作正常,在API 26模拟器中没有任何意外行为,但在API 23中崩溃。应用程序在启动时正确。我对android很新,所以不太确定要适应API 23会做出哪些改变。

这是我的傻瓜

apply plugin: 'com.android.application'



    android {
        compileSdkVersion 26
        defaultConfig {
            applicationId "com.set.www.assignment_1_mobile"
            minSdkVersion 15
            targetSdkVersion 26
            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.constraint:constraint-layout:1.0.2'
        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'
    }

这是错误日志

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.set.www.assignment_1_mobile/com.set.www.assignment_1_mobile.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>
    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 #0: Binary XML file line #0: 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.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
    at android.app.Activity.setContentView(Activity.java:2166)
    at com.set.www.assignment_1_mobile.MainActivity.onCreate(MainActivity.java:46)
    at android.app.Activity.performCreate(Activity.java:6237)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    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 #0: 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.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.set.www.assignment_1_mobile.MainActivity.onCreate(MainActivity.java:46) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    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: 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.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.set.www.assignment_1_mobile.MainActivity.onCreate(MainActivity.java:46) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    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.content.res.Resources$NotFoundException: Resource "com.set.www.assignment_1_mobile:drawable/nyteimg" (7f030004) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f030004 a=-1 r=0x7f030004}
    at android.content.res.Resources.loadDrawableForCookie(Resources.java:2602)
    at android.content.res.Resources.loadDrawable(Resources.java:2540)
    at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
    at android.view.View.<init>(View.java:3948)
    at android.view.ViewGroup.<init>(ViewGroup.java:573)
    at android.widget.LinearLayout.<init>(LinearLayout.java:203)
    at android.widget.LinearLayout.<init>(LinearLayout.java:199)
    at android.widget.LinearLayout.<init>(LinearLayout.java:195)
    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.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.set.www.assignment_1_mobile.MainActivity.onCreate(MainActivity.java:46) 
    at android.app.Activity.performCreate(Activity.java:6237) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
    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)

任何帮助将不胜感激。我希望我提供了足够的信息,如果我还没有,请告诉我。感谢。

更新
这是我的main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:background="@drawable/nyteimg"
    tools:context="com.setconestoga.www.assignment_1_mobile.MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:fontFamily="sans-serif"
        android:text="@string/main_menu"
        android:textAlignment="center"
        android:textColor="#FFFFFF"
        android:textSize="30sp"
        android:textStyle="bold"
        android:visibility="visible" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="26dp" />

    <Button
        android:id="@+id/PlaceOrder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/_place_order"
        android:textColor="@android:color/white" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="27dp" />

    <Button
        android:id="@+id/Settings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/settingScreenTitle"
        android:textColor="@android:color/white" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <Button
        android:id="@+id/ViewOrders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/txtViewOrder"
        android:textColor="@android:color/white" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="24dp" />

    <Button
        android:id="@+id/VisitWebsite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Visit Website"
        android:textColor="@android:color/white" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="28dp" />

    <Button
        android:id="@+id/Notes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Feedback"
        android:textColor="@android:color/white" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="29dp" />

    <Button
        android:id="@+id/ExitButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/exit"
        android:textColor="@android:color/white" />
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

放置在drawable-v26内的每张图片仅适用于Android api版本 26 。因此,您需要将其移动到drawable或创建多个图像到drawable-hdpi,drawable-xhdpi等。