每当我向应用程序添加图像时,它都会崩溃

时间:2017-12-12 17:47:20

标签: java android

每当我向我的应用程序添加图像时,它都会开始崩溃但没有图像,它可以正常工作。 这是我的logcat

12-12 17:38:15.616 1301-1301/com.govinddixit.govindscurrencyconverter E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        java.lang.RuntimeException: Unable to start activity ComponentInfo{com.govinddixit.govindscurrencyconverter/com.govinddixit.govindscurrencyconverter.MainActivity}: android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
                                                                                            at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                            at android.os.Looper.loop(Looper.java:137)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5041)
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
                                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                                         Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class ImageView
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
                                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
                                                                                            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                                                                                            at com.govinddixit.govindscurrencyconverter.MainActivity.onCreate(MainActivity.java:28)
                                                                                            at android.app.Activity.performCreate(Activity.java:5104)
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                                            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                            at android.os.Looper.loop(Looper.java:137) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                                            at dalvik.system.NativeStart.main(Native Method) 
                                                                                         Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f060001
                                                                                            at android.content.res.Resources.getValue(Resources.java:1014)
                                                                                            at android.support.v7.widget.ResourcesWrapper.getValue(ResourcesWrapper.java:208)
                                                                                            at android.support.v7.widget.AppCompatDrawableManager.loadDrawableFromDelegates(AppCompatDrawableManager.java:330)
                                                                                            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:195)
                                                                                            at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:188)
                                                                                            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:78)
                                                                                            at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
                                                                                            at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
                                                                                            at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1024)
                                                                                            at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1081)
                                                                                            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:675)
                                                                                            at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
                                                                                            at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
                                                                                            at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                                                                                            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                                                                                            at com.govinddixit.govindscurrencyconverter.MainActivity.onCreate(MainActivity.java:28) 
                                                                                            at android.app.Activity.performCreate(Activity.java:5104) 
                                                                                            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
                                                                                            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
                                                                                            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
                                                                                            at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                            at android.os.Looper.loop(Looper.java:137) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:5041) 
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
                                                                                            at dalvik.system.NativeStart.main(Native Method) 
12-12 17:38:15.820 437-688/system_process W/ActivityManager:   Force finishing activity com.govinddixit.govindscurrencyconverter/.MainActivity
12-12 17:38:16.200 437-688/system_process D/dalvikvm: GC_FOR_ALLOC freed 102K, 19% free 5433K/6648K, paused 101ms, total 101ms
12-12 17:38:16.224 437-688/system_process I/dalvikvm-heap: Grow heap (frag case) to 5.778MB for 380492-byte allocation
12-12 17:38:16.532 437-491/system_process D/dalvikvm: GC_FOR_ALLOC freed 2K, 18% free 5802K/7020K, paused 306ms, total 306ms
12-12 17:38:16.636 437-453/system_process D/dalvikvm: GC_FOR_ALLOC freed 7K, 16% free 5918K/7020K, paused 23ms, total 23ms
12-12 17:38:16.656 437-453/system_process I/dalvikvm-heap: Grow heap (frag case) to 6.965MB for 1127532-byte allocation
12-12 17:38:16.696 437-445/system_process D/dalvikvm: GC_FOR_ALLOC freed <1K, 14% free 7019K/8124K, paused 39ms, total 39ms
12-12 17:38:16.768 437-439/system_process D/dalvikvm: GC_CONCURRENT freed 6K, 13% free 7111K/8124K, paused 49ms+3ms, total 69ms

                                                      [ 12-12 17:38:16.860   409:  946 D/         ]
                                                      HostConnection::get() New Host Connection established 0xb80a6870, tid 946


                                                      [ 12-12 17:38:16.892   437:  453 D/         ]
                                                      HostConnection::get() New Host Connection established 0xb7c43788, tid 453
12-12 17:38:17.104 437-450/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{5321514c u0 com.govinddixit.govindscurrencyconverter/.MainActivity}
12-12 17:38:17.528 662-662/com.android.launcher W/EGL_genymotion: eglSurfaceAttrib not implemented
12-12 17:38:19.044 437-453/system_process I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
12-12 17:38:20.536 437-452/system_process I/Choreographer: Skipped 49 frames!  The application may be doing too much work on its main thread.
12-12 17:38:21.504 437-452/system_process I/Choreographer: Skipped 57 frames!  The application may be doing too much work on its main thread.
12-12 17:38:22.496 437-437/system_process I/ActivityManager: No longer want com.android.email (pid 1074): empty #17
12-12 17:38:22.648 437-518/system_process I/qtaguid: Failed write_ctrl(s 0 10052) res=-1 errno=1
12-12 17:38:22.648 437-518/system_process W/NetworkManagementSocketTagger: setKernelCountSet(10052, 0) failed with errno -1

Java活动

package com.govinddixit.govindscurrencyconverter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity
{
    public void convert (View view)
    {
        EditText dollarField=(EditText) findViewById(R.id.dollarField);
       Double dollarAmount =Double.parseDouble(dollarField.getText().toString());

        Double rupee=dollarAmount*64.50;
        Toast.makeText(getApplicationContext(),"Rs " + rupee.toString(),Toast.LENGTH_LONG).show();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.govinddixit.govindscurrencyconverter.MainActivity">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="347dp"
        android:layout_height="33dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Enter The Amount In Dollar"
        android:textSize="25sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.52" />

    <EditText
        android:id="@+id/dollarField"
        android:layout_width="335dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="numberDecimal"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.63" />

    <Button
        android:id="@+id/button"
        android:layout_width="160dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:onClick="convert"
        android:text="Convert"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.8" />

    <EditText
        android:id="@+id/textView1"
        android:layout_width="347dp"
        android:layout_height="55dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="        Dollar To Ruppes"
        android:textSize="30sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.37" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="67dp"
        android:layout_height="74dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.097"
        app:srcCompat="@drawable/a" />

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

你的stacktrace说Resources$NotFoundException。确保名称为a的任何图像都包含在可绘制文件夹中。

如果有,请尝试使用android:src="@drawable/a"

希望这有帮助!

答案 1 :(得分:0)

您必须使用android:src代替app:srcCompat,并确保您的可绘图中包含该图像文件。

<ImageView
        android:id="@+id/imageView8"
        android:layout_width="67dp"
        android:layout_height="74dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.097"
        android:src="@drawable/a" />

答案 2 :(得分:0)

以下是我的答案,希望它对您有用:

<强> 1。首先将以下代码放入build.gradle:

    defaultConfig {
       vectorDrawables.useSupportLibrary = true
    }

启用用于您的应用程序的矢量drawable。

<强> 2。同样启用您的Application / Activity类,如下所示:

    static {
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
           }

在运行时使用矢量绘制到任何视图(如Image / Edittext / Text / Button)来了解您的编译器。

第3。如果您正在使用TextView / Edittext / Button,那么使用它的属性创建自定义视图,如下所示:

   public class CustomTextView extends AppCompatTextView 
   {

    public CustomTextView(Context context) {
        super(context);
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(context, attrs);
    }

    void initAttrs(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray attributeArray = context.obtainStyledAttributes(
                    attrs,
                    R.styleable.CustomTextView);

            Drawable drawableLeft = null;
            Drawable drawableRight = null;
            Drawable drawableBottom = null;
            Drawable drawableTop = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                drawableLeft = attributeArray.getDrawable(R.styleable.CustomTextView_drawableLeftCompat);
                drawableRight = attributeArray.getDrawable(R.styleable.CustomTextView_drawableRightCompat);
                drawableBottom = attributeArray.getDrawable(R.styleable.CustomTextView_drawableBottomCompat);
                drawableTop = attributeArray.getDrawable(R.styleable.CustomTextView_drawableTopCompat);
            } else {
                final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableLeftCompat, -1);
                final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableRightCompat, -1);
                final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableBottomCompat, -1);
                final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomTextView_drawableTopCompat, -1);

                if (drawableLeftId != -1)
                    drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
                if (drawableRightId != -1)
                    drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
                if (drawableBottomId != -1)
                    drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
                if (drawableTopId != -1)
                    drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
            }
            setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
            attributeArray.recycle();
        }
    }
    }

<强> 4。把你的财产变成如下样式:

    <declare-styleable name="CustomTextView">
        <attr name="drawableLeftCompat" format="reference"/>
        <attr name="drawableRightCompat" format="reference"/>
        <attr name="drawableTopCompat" format="reference"/>
        <attr name="drawableBottomCompat" format="reference"/>
    </declare-styleable>

<强> 5。最后用于布局文件,如:

    <com.app.myapplication.CustomTextView
        style="@style/editText"
        android:layout_below="@id/edtSample"
        app:drawableLeftCompat="@drawable/ic_android_black_24dp"
        app:drawableRightCompat="@drawable/ic_android_black_24dp">
     </com.app.myapplication.CustomTextView>

6.我们正在使用Vector Drawable进入我们的应用程序。