1

我有一个刚刚添加到布局中的 ImageButton,但是当我在布局上调用 setContentView 时,我的程序现在崩溃了。查看 logcat,在尝试创建此 ImageButton 时似乎给了我一个 NullPointerException。ic_menu_send.png 位于适当的可绘制文件夹中。这是 ImageButton 的 xml:

<ImageButton
    android:id="@+id/imageButton1"
    android:src="@drawable/ic_menu_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/enterPassword2"
    android:layout_alignRight="@id/enterPassword1"
    android:layout_alignTop="@id/enterPassword2"
    android:background="@style/AppTheme"
    android:contentDescription="@string/submit_button" />

这是此 Activity 的 onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_time); // causes nullpointerexception
    EditText enterPassword1 = (EditText)findViewById(R.id.enterPassword1);
    enterPassword1.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return onEditAction(v);
        }
    });
    EditText enterPassword2 = (EditText)findViewById(R.id.enterPassword2);
    enterPassword2.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            return onEditAction(v);
        }
    });
    /*ImageButton submit1 = (ImageButton)findViewById(R.id.imageButton1);
    submit1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            EditText enterPassword1 = (EditText)findViewById(R.id.enterPassword1);
            onEditAction(enterPassword1);
        }
    });*/

代码的 ImageButton 部分只是为了测试而被注释掉,但程序崩溃得更早。有谁知道可能导致这种情况的原因?谢谢您的帮助。

编辑:这里是 logcat 日志:

01-27 15:52:11.264: E/AndroidRuntime(31195): FATAL EXCEPTION: main
01-27 15:52:11.264: E/AndroidRuntime(31195): java.lang.RuntimeException: Unable to start activity ComponentInfo{us.securecow.nearfieldunlock/us.securecow.nearfieldunlock.FirstTimeActivity}: android.view.InflateException: Binary XML file line #50: Error inflating class android.widget.ImageButton
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.os.Looper.loop(Looper.java:137)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.main(ActivityThread.java:5039)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Method.invokeNative(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Method.invoke(Method.java:511)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at dalvik.system.NativeStart.main(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.view.InflateException: Binary XML file line #50: Error inflating class android.widget.ImageButton
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Activity.setContentView(Activity.java:1881)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at us.securecow.nearfieldunlock.FirstTimeActivity.onCreate(FirstTimeActivity.java:19)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Activity.performCreate(Activity.java:5104)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 11 more
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: java.lang.reflect.InvocationTargetException
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Constructor.constructNative(Native Method)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 24 more
01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.content.res.Resources.loadDrawable(Resources.java:1927)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.view.View.<init>(View.java:3328)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageView.<init>(ImageView.java:114)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageButton.<init>(ImageButton.java:87)
01-27 15:52:11.264: E/AndroidRuntime(31195):    at android.widget.ImageButton.<init>(ImageButton.java:83)
01-27 15:52:11.264: E/AndroidRuntime(31195):    ... 27 more
4

6 回答 6

1

这是你应该寻找的:

01-27 15:52:11.264: E/AndroidRuntime(31195): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}

尝试删除 @style 行,看看会发生什么,因为你说其他资源已经到位

于 2013-01-27T22:14:20.307 回答
1

堆栈跟踪说明了一切:

资源不是可绘制的(颜色或路径):

根据 Android文档,背景属性需要 A drawable 或颜色。

完整报价:

用作背景的可绘制对象。这可以是对完整可绘制资源的引用(例如 PNG 图像、9-patch、XML 状态列表描述等),也可以是纯色,例如“#ff000000”(黑色)。

样式不符合任何这些标准,因此,您的应用程序在运行时崩溃。

于 2013-01-27T22:18:08.040 回答
1

该问题与以下情况之一有关:

  • android:src="@drawable/ic_menu_send"
  • android:background="@style/AppTheme"

很可能是第二个。这些参数需要一种drawable资源,其中一个不是drawable(如堆栈跟踪所示)。

于 2013-01-27T22:12:10.923 回答
1

从布局 xml 中删除 "android:background="@style/AppTheme"。

于 2013-01-27T22:12:21.277 回答
0

检查您的图像(ic_menu_send)是否在您的项目中的可绘制文件夹下。

我有同样的情况,但我的图像已损坏,当我检查它们时它们不可见。

在我将所有图像复制到drawable forlder下后,一切正常。

于 2013-05-10T20:09:23.843 回答
0
Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f050001 a=-1 r=0x7f050001}

嗯……呼……

于 2013-01-27T22:11:57.723 回答