简体   繁体   English

每次我将图像添加到应用程序时,它都会崩溃

[英]Every time i added an image to the app it crashes

Whenever I am adding an image to my app it starts crashing but without images, it works fine. 每当我将图像添加到我的应用程序时,它就会开始崩溃,但没有图像时,它将正常运行。 This is my 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 activity 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);
    }
}

here is .xml file 这是.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>

your stacktrace says Resources$NotFoundException . 您的堆栈跟踪显示Resources$NotFoundException Make sure any image with name a is contained in drawable folder. 确保可绘制文件夹中包含名称为a任何图像。

If it's there, then try with android:src="@drawable/a" 如果在那里,请尝试使用android:src="@drawable/a"

Hope this helps! 希望这可以帮助!

You have to Use android:src instead of app:srcCompat and make sure You have that image file in your drawable . 您必须使用android:src而不是app:srcCompat并确保在drawable中具有该图像文件。

<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" />

Below is my answer, Hope it will be useful for you: 以下是我的回答,希望对您有用:

1. At first put below code into your build.gradle: 1.首先,将以下代码放入build.gradle中:

    defaultConfig {
       vectorDrawables.useSupportLibrary = true
    }

to enable vector drawable used into your Applications. 使矢量可绘制用于您的应用程序。

2. Also Enable into your Application/Activity class like below: 2.还要如下所示启用到您的Application / Activity类中:

    static {
            AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
           }

to know your compiler using vector drawable into any of view like Image/Edittext/Text/Button at Runtime. 在运行时使用矢量可绘制到任何视图(例如Image / Edittext / Text / Button)中来了解编译器。

3. If you are using into TextView/Edittext/Button then create custom view with it's property like below: 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. put your property into style like : 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. And at Last used into layout file like : 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.That's All Stuff we are using Vector Drawable into our application. 6,这就是我们在应用程序中使用Vector Drawable的全部内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM