为什么在我的应用程序上放置ImageView会使我的系统崩溃?

时间:2015-10-08 06:10:12

标签: android xml android-layout android-studio android-imageview

这是我的问题!我目前正在尝试开发移动杂货店应用程序。但是,当我进入这个布局(Baby_Diaper)时,它崩溃了?我发现这里的问题是ImageView,因为当我删除I​​mageView时它工作!

Baby_Diaper(JAVA)

public class Baby_Diaper extends ActionBarActivity {
ArrayList<String> selection = new ArrayList<String>();
TextView final_text;
Button addtoCart;
ImageView diaper_bckgrnd;
Intent i = new Intent(this, Shopping_List.class);


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_baby__diaper);
    addtoCart = (Button) findViewById(R.id.addtocart);
    final_text = (TextView)findViewById(R.id.final_shopping_diaper);
    diaper_bckgrnd = (ImageView) findViewById(R.id.ctgry_bckgrnd_dpr);

}


public void SelectItem (View view) {

    boolean checked = ((CheckBox) view) .isChecked();
    switch (view.getId())
    {
        case R.id.pampers:

            if(checked)
            {selection.add("Pampers");}
            else
            {
                selection.remove ("Pampers");
            }

            break;

        case R.id.huggies:

            if(checked)
            {selection.add("Huggies");}
            else
            {
                selection.remove ("Huggies");
            }

            break;

        case R.id.johnsons:

            if(checked)
            {selection.add("Johnsons");}
            else
            {
                selection.remove ("Johnsons");
            }

            break;

        case R.id.supreme:

            if(checked)
            {selection.add("Supreme");}
            else
            {
                selection.remove ("Supreme");
            }

            break;
    }
}

public void ocaddtocart(View view){
    String final_shopping_selection = "";

    for (String Selections  : selection){
        final_shopping_selection = final_shopping_selection + Selections + "\n";
    }

    final_text.setText(final_shopping_selection);
    final_text.setEnabled(true);}

public void ocgtshoppinglist (View view){
    Intent x = new Intent(Baby_Diaper.this, Shopping_List.class);
    x.putExtra("items", final_text.getText().toString());

    startActivity(x);
}

Baby_Diaper 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"
tools:context="com.example.admin.mobile_grocery.Baby_Diaper"
android:id="@+id/baby_diaper">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ctgry_bckgrnd_dpr"
    android:src="@drawable/category_background"
    android:scaleType="centerCrop"
    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/pampers"
    android:id="@+id/pampers"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="52dp"
    android:checked="false"
    android:onClick="SelectItem"
    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/huggies"
    android:id="@+id/huggies"
    android:layout_below="@+id/pampers"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"

    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/johnsons"
    android:id="@+id/johnsons"
    android:layout_below="@+id/huggies"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"
    android:inputType="textNoSuggestions"

    />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/supreme"
    android:id="@+id/supreme"
    android:layout_below="@+id/johnsons"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:checked="false"
    android:onClick="SelectItem"
    android:inputType="textNoSuggestions"
    />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/addtocart"
    android:id="@+id/addtocart"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:inputType="textNoSuggestions"
    android:onClick="ocaddtocart" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Hello Shoppers!"
    android:id="@+id/final_shopping_diaper"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="80dp" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO TO SHOPPING LIST"
    android:id="@+id/gt_shopping_list"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="ocgtshoppinglist"
    />

Logcat错误

&#13;
&#13;
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.mobile_grocery/com.example.admin.mobile_grocery.Baby_Diaper}: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
            at android.view.LayoutInflater.createView(LayoutInflater.java:619)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.admin.mobile_grocery.Baby_Diaper.onCreate(Baby_Diaper.java:29)
            at android.app.Activity.performCreate(Activity.java:5188)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:593)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.admin.mobile_grocery.Baby_Diaper.onCreate(Baby_Diaper.java:29)
            at android.app.Activity.performCreate(Activity.java:5188)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.OutOfMemoryError
            at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
            at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:636)
            at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:484)
            at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:813)
            at android.content.res.Resources.loadDrawable(Resources.java:2277)
            at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
            at android.widget.ImageView.<init>(ImageView.java:126)
            at android.widget.ImageView.<init>(ImageView.java:116)
            at java.lang.reflect.Constructor.constructNative(Native Method)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
            at android.view.LayoutInflater.createView(LayoutInflater.java:593)
            at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
            at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.admin.mobile_grocery.Baby_Diaper.onCreate(Baby_Diaper.java:29)
            at android.app.Activity.performCreate(Activity.java:5188)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
&#13;
&#13;
&#13;

希望有人会借助他们的知识来帮助我解决这个问题。 当我在logcat中单击java时,它在这里指向了setContentView(R.layout.activity_baby__diaper);

3 个答案:

答案 0 :(得分:0)

您将获得OutOfMemoryError

Caused by: java.lang.OutOfMemoryError

这可能是因为您在ImageView中使用了非常高分辨率的图像。 尝试使用较小的图像并检查结果。

答案 1 :(得分:0)

由于堆栈跟踪显示您正在获取OutOfMemory异常。 你的图像很大,高分辨率,解决方案很简单 - 将图像添加到drawable-xxhdpi并查看它是否有帮助。

请记住,默认的基本Android密度是mdpi,因此导致操作系统对已经非常大的图像进行重大调整。

答案 2 :(得分:0)

我认为您已在布局中加载了高分辨率图像。 请使用缩小图像而不是当前图像。

如果您无法解决问题,请尝试以下代码。

使用以下功能缩小实际图像:

public static Bitmap scaleImage(String path, int reqHeight, int reqWidth) throws Throwable
{
    Bitmap bitMap = null;
    System.gc();
    File file = new File(path);
    if (file.exists())
    {
        BitmapFactory.Options bitMapFactoryOptions = new BitmapFactory.Options();
        bitMapFactoryOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getPath(), bitMapFactoryOptions);
        bitMapFactoryOptions.inSampleSize = calculateInSampleSize(bitMapFactoryOptions, reqHeight, reqWidth);
        bitMapFactoryOptions.inJustDecodeBounds = false;
        bitMap = BitmapFactory.decodeFile(file.getPath(), bitMapFactoryOptions);
    }
    else
    {
        throw new Throwable(path + " not found or not a valid image");
    }
    return bitMap;
}


private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
    {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth)
        {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth)
            {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }

您还可以使用以下功能缩小资源图像:

public static Bitmap scaleImage(Context context, int resourceId, int width, int height, int sampleSize) throws Throwable
    {
        if (resourceId != 0)
        {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            // if Sample size 4 then image size (Original-Width/4) X
            // (Original-Heigth/4)
            bmpFactoryOptions.inSampleSize = sampleSize;
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, bmpFactoryOptions);
            if (bitmap != null)
            {
                // this line convert image size given to width X height
                return Bitmap.createScaledBitmap(bitmap, width, height, true);
            }
        }
        return null;
    }

替换布局中的imageview容器

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/ctgry_bckgrnd_dpr"
    android:scaleType="centerCrop"/>

在您的活动中写下以下代码

Bitmap bitmap;
ImageView diaper_bckgrnd;
diaper_bckgrnd = (ImageView) findViewById(R.id.ctgry_bckgrnd_dpr);

bitmap = scaleImage(context, getResources().getDrawable(R.drawable.category_background),500,500,2);
diaper_bckgrnd.setImageBitmap(bitmap);