我有一个刚刚添加到布局中的 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