两个不同活动的两个不同背景崩溃了应用程序

时间:2014-03-29 08:35:51

标签: java android eclipse android-layout android-intent

我在为两个不同的活动设置两个不同的背景时遇到问题,程序在打开第二个活动后崩溃。知道我已将两张照片添加到res ==>只有drawable-hdpi,两者的格式都是JPEG,大小约为200 Kbyte。

这是主要的活动java文件

=============================================== ==

package com.example.ewrwer;


import com.example.ewrwer.Main1Activity;
import com.example.ewrwer.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {


@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.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    default:
        // this.closeOptionsMenu();
        Intent InfoIntent= new Intent(this,Main1Activity.class);
        this.startActivity(InfoIntent);
        //    startActivity(new Intent(this, InfoActivity.class));
        //    startActivityForResult(new Intent(this, InfoActivity.class), 0);
        return true;

    }
}

}

========================================

这是主要活动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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/info"
tools:context=".MainActivity" >

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

=============================================== =================

这是第二个活动java文件

=============================================== ==

 package com.example.ewrwer;

 import android.os.Bundle;
 import android.app.Activity;
 import android.view.Menu;

  public class Main1Activity extends Activity {

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

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

 }

=============================================

这是第二个活动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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/bg1233"
tools:context=".Main1Activity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

<RatingBar
    android:id="@+id/ratingBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginTop="66dp" />

</RelativeLayout>

,这是LOGCAT

   03-29 06:32:42.700: D/dalvikvm(1343): GC_FOR_ALLOC freed 53K, 5% free 2890K/3028K, paused 60ms, total 63ms
   03-29 06:32:43.000: I/dalvikvm-heap(1343): Grow heap (frag case) to 28.630MB for 26981992-byte allocation
   03-29 06:32:43.120: D/dalvikvm(1343): GC_FOR_ALLOC freed 2K, 1% free 29237K/29380K, paused 116ms, total 117ms
   03-29 06:32:44.820: D/gralloc_goldfish(1343): Emulator without GPU emulation detected.
   03-29 06:32:51.900: I/Choreographer(1343): Skipped 56 frames!  The application may be doing too much work on its main thread.
   03-29 06:32:52.250: I/Choreographer(1343): Skipped 73 frames!  The application may be doing too much work on its main thread.
   03-29 06:32:52.910: W/InputEventReceiver(1343): Attempted to finish an input event but the input event receiver has already been disposed.
   03-29 06:32:52.910: W/InputEventReceiver(1343): Attempted to finish an input event but the input event receiver has already been disposed.
   03-29 06:32:52.910: W/ViewRootImpl(1343): Dropping event due to root view being removed: MotionEvent { action=ACTION_UP, id[0]=0, x[0]=279.0, y[0]=-23.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=10130541, downTime=10130377, deviceId=0, source=0x1002 }
   03-29 06:32:54.260: I/Choreographer(1343): Skipped 39 frames!  The application may be doing too much work on its main thread.
   03-29 06:32:54.590: I/Choreographer(1343): Skipped 81 frames!  The application may be doing too much work on its main thread.
   03-29 06:32:57.110: D/dalvikvm(1343): GC_FOR_ALLOC freed 48K, 1% free 30093K/30224K, paused 498ms, total 502ms
   03-29 06:32:57.110: I/dalvikvm-heap(1343): Forcing collection of SoftReferences for 26981992-byte allocation
   03-29 06:32:57.550: D/dalvikvm(1343): GC_BEFORE_OOM freed 10K, 1% free 30082K/30224K, paused 436ms, total 440ms
   03-29 06:32:57.560: E/dalvikvm-heap(1343): Out of memory on a 26981992-byte allocation.
   03-29 06:32:57.590: I/dalvikvm(1343): "main" prio=5 tid=1 RUNNABLE
   03-29 06:32:57.590: I/dalvikvm(1343):   | group="main" sCount=0 dsCount=0 obj=0xb3a39ca8 self=0xb78ed380
   03-29 06:32:57.590: I/dalvikvm(1343):   | sysTid=1343 nice=0 sched=0/0 cgrp=apps handle=-1225637548
   03-29 06:32:57.590: I/dalvikvm(1343):   | state=R schedstat=( 3130000000 3390000000 627 ) utm=256 stm=57 core=0
   03-29 06:32:57.660: I/dalvikvm(1343):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
   03-29 06:32:57.660: I/dalvikvm(1343):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
   03-29 06:32:57.690: I/dalvikvm(1343):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.content.res.Resources.loadDrawable(Resources.java:2110)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.view.View.<init>(View.java:3554)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.view.View.<init>(View.java:3484)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.view.ViewGroup.<init>(ViewGroup.java:464)
   03-29 06:32:57.700: I/dalvikvm(1343):   at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
   03-29 06:32:57.700: I/dalvikvm(1343):   at java.lang.reflect.Constructor.constructNative(Native Method)
   03-29 06:32:57.700: I/dalvikvm(1343):   at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.createView(LayoutInflater.java:594)
   03-29 06:32:57.710: I/dalvikvm(1343):   at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   03-29 06:32:57.710: I/dalvikvm(1343):   at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.Activity.setContentView(Activity.java:1929)
   03-29 06:32:57.710: I/dalvikvm(1343):   at com.example.ewrwer.Main1Activity.onCreate(Main1Activity.java:12)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.Activity.performCreate(Activity.java:5231)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.ActivityThread.access$800(ActivityThread.java:135)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.os.Handler.dispatchMessage(Handler.java:102)
   03-29 06:32:57.710: I/dalvikvm(1343):   at android.os.Looper.loop(Looper.java:136)
   03-29 06:32:57.710: I/dalvikvm(1343):   at  android.app.ActivityThread.main(ActivityThread.java:5017)
   03-29 06:32:57.710: I/dalvikvm(1343):   at java.lang.reflect.Method.invokeNative(Native  Method)
   03-29 06:32:57.770: I/dalvikvm(1343):   at java.lang.reflect.Method.invoke(Method.java:515)
   03-29 06:32:57.770: I/dalvikvm(1343):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   03-29 06:32:57.770: I/dalvikvm(1343):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   03-29 06:32:57.770: I/dalvikvm(1343):   at dalvik.system.NativeStart.main(Native Method)
   03-29 06:32:57.800: D/skia(1343): --- decoder->decode returned false
   03-29 06:32:57.800: D/AndroidRuntime(1343): Shutting down VM
   03-29 06:32:57.800: W/dalvikvm(1343): threadid=1: thread exiting with uncaught exception (group=0xb3a38ba8)
   03-29 06:32:57.950: E/AndroidRuntime(1343): FATAL EXCEPTION: main
   03-29 06:32:57.950: E/AndroidRuntime(1343): Process: com.example.ewrwer, PID: 1343
   03-29 06:32:57.950: E/AndroidRuntime(1343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ewrwer/com.example.ewrwer.Main1Activity}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at  android.app.ActivityThread.access$800(ActivityThread.java:135)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.os.Handler.dispatchMessage(Handler.java:102)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.os.Looper.loop(Looper.java:136)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.ActivityThread.main(ActivityThread.java:5017)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at java.lang.reflect.Method.invokeNative(Native Method)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at java.lang.reflect.Method.invoke(Method.java:515)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at dalvik.system.NativeStart.main(Native Method)
   03-29 06:32:57.950: E/AndroidRuntime(1343): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.createView(LayoutInflater.java:620)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at  android.app.Activity.setContentView(Activity.java:1929)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at com.example.ewrwer.Main1Activity.onCreate(Main1Activity.java:12)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.Activity.performCreate(Activity.java:5231)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  ... 11 more
   03-29 06:32:57.950: E/AndroidRuntime(1343): Caused by: java.lang.reflect.InvocationTargetException
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at java.lang.reflect.Constructor.constructNative(Native Method)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.LayoutInflater.createView(LayoutInflater.java:594)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  ... 23 more
   03-29 06:32:57.950: E/AndroidRuntime(1343): Caused by: java.lang.OutOfMemoryError
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.content.res.Resources.loadDrawable(Resources.java:2110)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.View.<init>(View.java:3554)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.View.<init>(View.java:3484)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.view.ViewGroup.<init>(ViewGroup.java:464)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  at android.widget.RelativeLayout.<init>(RelativeLayout.java:236)
   03-29 06:32:57.950: E/AndroidRuntime(1343):  ... 26 more
   03-29 06:33:05.810: I/Process(1343): Sending signal. PID: 1343 SIG: 9

1 个答案:

答案 0 :(得分:0)

将JPG加载到框架使用的内部位图时,内存不足。用于位图的内存可以比源JPG大许多倍。

有关有效加载大位图的一些指导,请参阅this guide