我创建了一个 java 类和一个带有图像按钮的布局。当我设置内容视图时,它会引发运行时异常。
错误:
02-02 17:46:39.558: E/AndroidRuntime(954): FATAL EXCEPTION: main
02-02 17:46:39.558: E/AndroidRuntime(954): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.oceans/com.oceans.LettersScreenActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
02-02 17:46:39.558: E/AndroidRuntime(954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.app.ActivityThread.access$600(ActivityThread.java:130)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.os.Looper.loop(Looper.java:137)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.app.ActivityThread.main(ActivityThread.java:4745)
02-02 17:46:39.558: E/AndroidRuntime(954): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 17:46:39.558: E/AndroidRuntime(954): at java.lang.reflect.Method.invoke(Method.java:511)
02-02 17:46:39.558: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-02 17:46:39.558: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-02 17:46:39.558: E/AndroidRuntime(954): at dalvik.system.NativeStart.main(Native Method)
02-02 17:46:39.558: E/AndroidRuntime(954): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
02-02 17:46:39.558: E/AndroidRuntime(954): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-02 17:46:39.558: E/AndroidRuntime(954): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-02 17:46:39.558: E/AndroidRuntime(954): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
Xml 布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/letters_bg" >
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="49dp"
android:src="@drawable/leftsignpost" />
</RelativeLayout>
Java类代码:
public class LettersScreenActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.letters_screen); // error is here
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
那么问题是什么?为什么它抛出运行时异常?