我正在编写一个包含有关入口信息的应用程序。这是我的第一个应用程序,所以我对 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" />
两个文本视图的类只有 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