我的应用每次崩溃,并显示错误 “不幸的是,应用已停止” ,但在运行API 16 to 24
的android模拟器上却在API 24
上运行使用ImageView。但是,当删除Imageview时,它可以正常工作。我需要ImageView来构建项目。我该如何解决?下面是我的xml代码:
activity_main.xml
<?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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/note1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:contentDescription="@null"
android:gravity="center"
android:padding="8dp"
android:text="@string/note1"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/note2"/>
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="20dp" />
<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_end="20dp" />
<TextView
android:id="@+id/note2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/note2"
android:layout_marginEnd="8dp"
android:layout_marginTop="20dp"
android:layout_marginStart="8dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:padding="8dp"
android:contentDescription="@null"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@id/note1"
app:layout_constraintBottom_toTopOf="@+id/barrier"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
/>
<android.support.constraint.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="bottom"
app:constraint_referenced_ids="note1,note2"
app:layout_constraintBottom_toBottomOf="@+id/logoimage"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="46dp"
android:layout_marginBottom="50dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:background="@drawable/buttongradient"
android:clickable="true"
android:focusable="true"
android:text="@string/button_name"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/barrier2" />
<android.support.constraint.Barrier
android:id="@+id/barrier2"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:barrierDirection="top"
app:constraint_referenced_ids="button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<ImageView
android:id="@+id/logoimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:contentDescription="@null"
app:layout_constraintBottom_toTopOf="@+id/barrier2"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/barrier"
app:srcCompat="@drawable/logo" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.example.godwinacquah.ourstudybuddy;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Intent load;
Button but1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = findViewById(R.id.button);
but1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
load = new Intent( MainActivity.this ,Home.class);
startActivity(load);
finish();
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
}
}
logcat
08-08 15:46:40.522 3506-3506/com.example.godwinacquah.ourstudybuddy W/ResourceType: Failure getting entry for 0x7f070097 (t=6 e=151) in package 0 (error -75)
Failure getting entry for 0x7f070097 (t=6 e=151) in package 0 (error -75)
08-08 15:46:40.522 3506-3506/com.example.godwinacquah.ourstudybuddy D/AndroidRuntime: Shutting down VM
08-08 15:46:40.522 3506-3506/com.example.godwinacquah.ourstudybuddy W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9cd74b20)
08-08 15:46:40.532 3506-3506/com.example.godwinacquah.ourstudybuddy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.godwinacquah.ourstudybuddy, PID: 3506
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.godwinacquah.ourstudybuddy/com.example.godwinacquah.ourstudybuddy.MainActivity}: android.view.InflateException: Binary XML file line #100: Error inflating class ImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #100: Error inflating class ImageView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:714)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.godwinacquah.ourstudybuddy.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f070097
at android.content.res.Resources.getValue(Resources.java:1123)
at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:208)
at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:328)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:193)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:58)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:77)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:67)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:181)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1035)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.godwinacquah.ourstudybuddy.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
08-08 15:46:40.562 3506-3506/com.example.godwinacquah.ourstudybuddy D/dalvikvm: GC_FOR_ALLOC freed 328K, 11% free 3297K/3700K, paused 17ms, total 26ms
08-08 15:46:40.632 1629-3519/system_process D/dalvikvm: GC_FOR_ALLOC freed 2846K, 40% free 12585K/20720K, paused 70ms, total 71ms
答案 0 :(得分:0)
“ @ drawable / logo”是矢量图像吗?如果是,则需要添加支持库以缓解依赖关系
等级:
defaultConfig {
applicationId "com.app"
minSdkVersion 15
versionCode 1
versionName "x"
vectorDrawables.useSupportLibrary = true
}
dependencies {
//...
compile "com.android.support:support-vector-drawable:$appcompatVersion"
}