AppCompatActivity导致应用程序崩溃

时间:2017-02-15 06:04:33

标签: android android-appcompat

我正在开发一个Android应用程序,其中我使用AppCompantActivity进行Material Design和SupportActionBar等因为我的应用程序正在Android Jelly Bean 4.2.2上崩溃 KitKat 4.4 但是当我在 Marshmallow 上运行应用程序时,它运行正常。

logcat的

   org.xmlpull.v1.XmlPullParserException: Binary XML file line #17<vector> tag requires viewportWidth > 0
            at android.support.graphics.drawable.VectorDrawableCompat.updateStateFromTypedArray(VectorDrawableCompat.java:717)
            at android.support.graphics.drawable.VectorDrawableCompat.inflate(VectorDrawableCompat.java:654)
            at android.support.graphics.drawable.VectorDrawableCompat.createFromXmlInner(VectorDrawableCompat.java:617)
            at android.support.v7.widget.AppCompatDrawableManager$VdcInflateDelegate.createFromXmlInner(AppCompatDrawableManager.java:745)
            at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:365)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
            at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:723)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
            at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
            at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
            at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
            at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
            at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
            at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:74)
            at com.dmdmax.testing.MainActivity.onCreate(MainActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
02-15 00:57:57.920    1432-1432/com.dmdmax.testing D/AndroidRuntime﹕ Shutting down VM
02-15 00:57:57.920    1432-1432/com.dmdmax.testing W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41465730)
02-15 00:57:57.950    1432-1432/com.dmdmax.testing E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dmdmax.testing/com.dmdmax.testing.MainActivity}: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.content.res.Resources$NotFoundException: File res/drawable/abc_vector_test.xml from drawable resource ID #0x7f020052
            at android.content.res.Resources.loadDrawable(Resources.java:2096)
            at android.content.res.Resources.getDrawable(Resources.java:695)
            at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
            at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:723)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
            at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
            at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
            at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
            at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
            at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
            at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:74)
            at com.dmdmax.testing.MainActivity.onCreate(MainActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #17: invalid drawable tag vector
            at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:897)
            at android.graphics.drawable.Drawable.createFromXml(Drawable.java:837)
            at android.content.res.Resources.loadDrawable(Resources.java:2092)
            at android.content.res.Resources.getDrawable(Resources.java:695)
            at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:374)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:200)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
            at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:723)
            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
            at android.support.v7.widget.TintTypedArray.getDrawableIfKnown(TintTypedArray.java:81)
            at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:127)
            at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:147)
            at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:27)
            at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:53)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:205)
            at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
            at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:525)
            at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:74)
            at com.dmdmax.testing.MainActivity.onCreate(MainActivity.java:12)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"

    defaultConfig {
        applicationId "com.example.testing"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:25.0.0'
}

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是xml

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

这是menu_main.xml

<menu 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" tools:context=".MainActivity">
    <item android:id="@+id/action_settings" android:title="@string/action_settings"
        android:orderInCategory="100" app:showAsAction="never" />
</menu>

2 个答案:

答案 0 :(得分:0)

在build.gradle中添加此内容

  android {  
   defaultConfig {  
     vectorDrawables.useSupportLibrary = true  
    }  
 }

答案 1 :(得分:0)

项目的“build.gradle”中将gradle版本从1.3更改为1.5;

classpath 'com.android.tools.build:gradle:1.5.0'

应用

的build.gradle中更新
buildToolsVersion "23.0.2"

更新了从23.2到23.4的模块依赖关系:

compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.4.0'
compile 'com.android.support:support-vector-drawable:23.4.0'
compile 'com.android.support:animated-vector-drawable:23.4.0'

现在,根据你使用的gradle版本,使用以下两个片段中的任何一个。我使用的是gradle 1.5.0,我使用了这个 -

defaultConfig {
            generatedDensities = []
            }

    aaptOptions {
        additionalParameters "--no-version-vectors"
        }
But if you are using Gradle 2.1.0 use this instead

       defaultConfig {  
         vectorDrawables.useSupportLibrary = true  
         }

在xml文件中,我在我的ImageView(customIV)中使用了它 -

应用程式:srcCompat = “@绘制/ animDroid”  ; animDroid是一个动画矢量文件,包含一组动画。

CustomIV.setImageResource(R.drawable.animDroid);
Drawable drawable = imageView.getDrawable();
if (drawable instanceof Animatable) {
  ((Animatable) drawable).start();
}