0

我创建了一个 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);

    }
}

那么问题是什么?为什么它抛出运行时异常?

4

2 回答 2

0

提供的代码没有任何明显的问题,应该运行吗?至少对我有用(当然,drawables是我的)。

检查您是否有引用的资源letters_bgleftsignpost到位。这些必须在res/drawable. 清理项目。尝试提供更长的堆栈跟踪。

截屏

于 2013-02-02T11:59:57.237 回答
0

我试过这段代码......它真的很好......请检查你的图像和清单文件

于 2013-02-02T12:09:23.827 回答