我有2个班级:主要和学习。当我按下Main的布局上的“study”按钮时,它应该转到Study类但应用程序崩溃了。
这是主类代码:
package com.example.geograpp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
public class Main extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View studyButton=this.findViewById(R.id.study_button);
studyButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.study_button:
Intent studyIntent = new Intent(this, Study.class);
startActivity(studyIntent);
break;
}
}
}
这是Main的布局:
<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"
tools:context=".Main"
android:background="@drawable/mainmenu_background">
<Button
android:id="@+id/study_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="94dp"
android:text="Study"
android:textColor="@color/white"
android:textSize="22sp"
android:background="@color/black"/>
<Button
android:id="@+id/about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/game_button"
android:layout_below="@+id/game_button"
android:layout_marginTop="40dp"
android:background="@color/black"
android:text="About"
android:textColor="@color/white"
android:textSize="22sp" />
<Button
android:id="@+id/game_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/study_button"
android:layout_below="@+id/study_button"
android:layout_marginTop="38dp"
android:background="@color/black"
android:text="Game"
android:textColor="@color/white"
android:textSize="22sp" />
<Button
android:id="@+id/exit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/about_button"
android:layout_alignRight="@+id/study_button"
android:layout_below="@+id/about_button"
android:layout_marginTop="42dp"
android:background="@color/black"
android:text="Exit"
android:textColor="@color/white"
android:textSize="22sp" />
</RelativeLayout>
这是学习类代码:
package com.example.geograpp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Study extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
}
}
这是研究的布局:
<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"
tools:context=".Study"
android:background="@drawable/study_background">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:text="Choose a Continent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/white" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/worldmap_clickable" />
</RelativeLayout>
这是表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geograpp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.geograpp.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.geograpp.Study"
android:label="@string/title_activity_study" >
</activity>
</application>
</manifest>
暗恋后的日志猫:
09-04 22:54:17.176: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 49K, 53% free 2551K/5379K, external 1625K/2137K, paused 135ms
09-04 22:54:45.306: D/dalvikvm(334): GC_EXTERNAL_ALLOC freed 12K, 53% free 2577K/5379K, external 23321K/25369K, paused 62ms
09-04 22:54:45.396: E/dalvikvm-heap(334): 22216740-byte external allocation too large for this process.
09-04 22:54:45.496: E/GraphicsJNI(334): VM won't let us allocate 22216740 bytes
09-04 22:54:45.506: D/dalvikvm(334): GC_FOR_MALLOC freed <1K, 53% free 2577K/5379K, external 23321K/25369K, paused 35ms
09-04 22:54:45.506: D/skia(334): --- decoder->decode returned false
09-04 22:54:45.506: D/AndroidRuntime(334): Shutting down VM
09-04 22:54:45.506: W/dalvikvm(334): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-04 22:54:45.538: E/AndroidRuntime(334): FATAL EXCEPTION: main
09-04 22:54:45.538: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.geograpp/com.example.geograpp.Study}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-04 22:54:45.538: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:507)
09-04 22:54:45.538: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-04 22:54:45.538: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-04 22:54:45.538: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.createView(LayoutInflater.java:518)
09-04 22:54:45.538: E/AndroidRuntime(334): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:386)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-04 22:54:45.538: E/AndroidRuntime(334): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.Activity.setContentView(Activity.java:1657)
09-04 22:54:45.538: E/AndroidRuntime(334): at com.example.geograpp.Study.onCreate(Study.java:12)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-04 22:54:45.538: E/AndroidRuntime(334): ... 11 more
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.reflect.InvocationTargetException
09-04 22:54:45.538: E/AndroidRuntime(334): at java.lang.reflect.Constructor.constructNative(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.LayoutInflater.createView(LayoutInflater.java:505)
09-04 22:54:45.538: E/AndroidRuntime(334): ... 21 more
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.View.<init>(View.java:1951)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.View.<init>(View.java:1899)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.ViewGroup.<init>(ViewGroup.java:286)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
09-04 22:54:45.538: E/AndroidRuntime(334): ... 24 more
09-04 22:55:01.926: I/Process(334): Sending signal. PID: 334 SIG: 9
答案 0 :(得分:2)
您的应用程序崩溃,因为您的应用程序允许超出内存限制。解决此问题的唯一方法是减少应用程序的内存占用量。我的猜测是你的整个问题与你学习布局中设置的背景有关。
android:background="@drawable/study_background"
根据我的经验,图像几乎总是OutOfMemory错误的原因。看一下这个图像的分辨率,并尝试将其缩小到更合适的移动尺寸。测试这是否是问题的一种方法是完全删除背景行并运行应用程序。如果您没有看到任何错误或您看到的错误不一样,您就会知道图像导致您的内存问题。
要解释我是如何得出这个结论的,请深入了解堆栈跟踪中由行引起的最后一个:
09-04 22:54:45.538: E/AndroidRuntime(334): Caused by: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.content.res.Resources.loadDrawable(Resources.java:1709)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.View.<init>(View.java:1951)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.View.<init>(View.java:1899)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.view.ViewGroup.<init>(ViewGroup.java:286)
09-04 22:54:45.538: E/AndroidRuntime(334): at android.widget.RelativeLayout.<init>(RelativeLayout.java:173)
如果您在构造函数调用RelativeLayout期间查看它,并且在尝试解码位图时内存不足。在堆栈跟踪中更高的位置,您会看到它导致xml膨胀错误。这意味着当Android系统试图将您的xml转换为java对象并在屏幕上绘制时出现问题。您解释说,当您的代码尝试转移到研究活动时,您会看到此问题,这表明该问题位于xml中用于研究活动的某个位置。您的学习活动xml只有一个RelativeLayout,背景设置在其上,表明这就是问题所在。
最后,我想指出你还在图像视图上设置了一个位图,也可能需要为移动设备调整大小。
android:src="@drawable/worldmap_clickable"