我正在编写一个包含入口信息的应用程序。这是我的第一个应用程序,所以我对android有点新鲜。
我有一个由8个按钮组成的菜单。对于其中一个,它会打开一个包含两个图像按钮的活动。每个图像按钮都会打开一个不同的活动,其中只包含一个文本视图。
我的问题是这个。当我第一次运行应用程序时,如果我点击任一图像按钮,它就会崩溃。然后它返回菜单。如果我回到任何一个图像按钮,现在它的工作原理。
我在运行4.2.2调试的GS4上运行我的应用程序它给了我找不到的错误源。不确定什么是错的。
带有两个按钮的课程
public class Factions extends Activity implements View.OnClickListener {
ImageButton resis, enligh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_factions);
setup();
}
private void setup() {
resis = (ImageButton) findViewById(R.id.ibResistance);
resis.setOnClickListener(this);
enligh = (ImageButton) findViewById(R.id.ibEnlightened);
enligh.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.factions, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i;
switch (v.getId()) {
case R.id.ibEnlightened:
i = new Intent(getApplicationContext(), AboutEnlightened.class);
startActivity(i);
break;
case R.id.ibResistance:
i = new Intent(getApplicationContext(), AboutResistance.class);
startActivity(i);
break;
}
}
}
该类的xml
<ImageButton
android:id="@+id/ibResistance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/resistance_logo_button" />
<ImageButton
android:id="@+id/ibEnlightened"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:src="@drawable/enlightened_logo_button" />
两个textviews的类只有onCreate和onCreateOptionsMenu方法。我不需要改变它们或添加任何东西。如果您需要任何其他信息,请告诉我。提前谢谢。
logcat显示了这个
08-09 22:07:00.541: I/dalvikvm(11028): at android.content.res.Resources.loadDrawable(Resources.java:2854)
08-09 22:07:00.541: I/dalvikvm(11028): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.View.<init>(View.java:3460) 08-09 22:07:00.541: I/dalvikvm(11028): at android.view.ViewGroup.<init>(ViewGroup.java:446)
08-09 22:07:00.541: I/dalvikvm(11028): at android.widget.LinearLayout.<init>(LinearLayout.java:176)
08-09 22:07:00.541: I/dalvikvm(11028): at android.widget.LinearLayout.<init>(LinearLayout.java:172)
08-09 22:07:00.541: I/dalvikvm(11028): at java.lang.reflect.Constructor.constructNative(Native Method)
08-09 22:07:00.541: I/dalvikvm(11028): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
08-09 22:07:00.541: I/dalvikvm(11028): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-09 22:07:00.541: I/dalvikvm(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-09 22:07:00.541: I/dalvikvm(11028): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.Activity.setContentView(Activity.java:1928)
08-09 22:07:00.541: I/dalvikvm(11028): at com.kaveman.ingress.faqs.AboutResistance.onCreate(AboutResistance.java:12)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.Activity.performCreate(Activity.java:5250)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.ActivityThread.access$700(ActivityThread.java:152)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
08-09 22:07:00.541: I/dalvikvm(11028): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 22:07:00.541: I/dalvikvm(11028): at android.os.Looper.loop(Looper.java:137)
08-09 22:07:00.541: I/dalvikvm(11028): at android.app.ActivityThread.main(ActivityThread.java:5328)
08-09 22:07:00.541: I/dalvikvm(11028): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 22:07:00.541: I/dalvikvm(11028): at java.lang.reflect.Method.invoke(Method.java:511)
08-09 22:07:00.541: I/dalvikvm(11028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-09 22:07:00.541: I/dalvikvm(11028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-09 22:07:00.541: I/dalvikvm(11028): at dalvik.system.NativeStart.main(Native Method)
08-09 22:07:00.541: D/skia(11028): --- allocation failed for scaled bitmap
08-09 22:07:00.541: D/AndroidRuntime(11028): Shutting down VM
08-09 22:07:00.541: W/dalvikvm(11028): threadid=1: thread exiting with uncaught exception (group=0x41173ac8)
08-09 22:07:00.571: E/AndroidRuntime(11028): FATAL EXCEPTION: main
08-09 22:07:00.571: E/AndroidRuntime(11028): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kaveman.ingress.faqs/com.kaveman.ingress.faqs.AboutResistance}: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread.access$700(ActivityThread.java:152)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.os.Looper.loop(Looper.java:137)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread.main(ActivityThread.java:5328)
08-09 22:07:00.571: E/AndroidRuntime(11028): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 22:07:00.571: E/AndroidRuntime(11028): at java.lang.reflect.Method.invoke(Method.java:511)
08-09 22:07:00.571: E/AndroidRuntime(11028): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-09 22:07:00.571: E/AndroidRuntime(11028): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-09 22:07:00.571: E/AndroidRuntime(11028): at dalvik.system.NativeStart.main(Native Method)
08-09 22:07:00.571: E/AndroidRuntime(11028): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class <unknown>
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.createView(LayoutInflater.java:619)
08-09 22:07:00.571: E/AndroidRuntime(11028): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:467)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-09 22:07:00.571: E/AndroidRuntime(11028): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.Activity.setContentView(Activity.java:1928)
08-09 22:07:00.571: E/AndroidRuntime(11028): at com.kaveman.ingress.faqs.AboutResistance.onCreate(AboutResistance.java:12)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.Activity.performCreate(Activity.java:5250)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-09 22:07:00.571: E/AndroidRuntime(11028): ... 11 more
08-09 22:07:00.571: E/AndroidRuntime(11028): Caused by: java.lang.reflect.InvocationTargetException
08-09 22:07:00.571: E/AndroidRuntime(11028): at java.lang.reflect.Constructor.constructNative(Native Method)
08-09 22:07:00.571: E/AndroidRuntime(11028): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.LayoutInflater.createView(LayoutInflater.java:593)
08-09 22:07:00.571: E/AndroidRuntime(11028): ... 23 more
08-09 22:07:00.571: E/AndroidRuntime(11028): Caused by: java.lang.OutOfMemoryError 08-09 22:07:00.571: E/AndroidRuntime(11028): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:596)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:817)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.content.res.Resources.loadDrawable(Resources.java:2854)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.View.<init>(View.java:3460) 08-09 22:07:00.571: E/AndroidRuntime(11028): at android.view.ViewGroup.<init>(ViewGroup.java:446)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.widget.LinearLayout.<init>(LinearLayout.java:176)
08-09 22:07:00.571: E/AndroidRuntime(11028): at android.widget.LinearLayout.<init>(LinearLayout.java:172)
08-09 22:07:00.571: E/AndroidRuntime(11028): ... 26 more
答案 0 :(得分:1)
这是你应该感兴趣的一行:
Caused by: java.lang.OutOfMemoryError 08-09 22:07:00.571: E/AndroidRuntime(11028): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
您使用的是Android虚拟设备吗?尝试在虚拟设备管理器中增加VM。