不太确定为什么我会收到以下错误,因为代码从另一个页面解除并在那里正常工作...
01-19 22:38:06.443: E/AndroidRuntime(27945): FATAL EXCEPTION: main
01-19 22:38:06.443: E/AndroidRuntime(27945): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fooditemmonitor/com.example.fooditemmonitor.CurrentItems}: java.lang.NullPointerException
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread.access$700(ActivityThread.java:159)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.os.Looper.loop(Looper.java:176)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread.main(ActivityThread.java:5419)
01-19 22:38:06.443: E/AndroidRuntime(27945): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 22:38:06.443: E/AndroidRuntime(27945): at java.lang.reflect.Method.invoke(Method.java:525)
01-19 22:38:06.443: E/AndroidRuntime(27945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
01-19 22:38:06.443: E/AndroidRuntime(27945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
01-19 22:38:06.443: E/AndroidRuntime(27945): at dalvik.system.NativeStart.main(Native Method)
01-19 22:38:06.443: E/AndroidRuntime(27945): Caused by: java.lang.NullPointerException
01-19 22:38:06.443: E/AndroidRuntime(27945): at com.example.fooditemmonitor.CurrentItems.onCreate(CurrentItems.java:22)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.Activity.performCreate(Activity.java:5372)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
01-19 22:38:06.443: E/AndroidRuntime(27945): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
01-19 22:38:06.443: E/AndroidRuntime(27945): ... 11 more
current_inventory.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1" >
<Button
android:id="@+id/editItemCurrent"
android:layout_width="145dp"
android:layout_column="0"
android:layout_gravity="right|bottom"
android:layout_row="0"
android:text="@string/editItem" />
<Button
android:id="@+id/scanCurrent"
android:layout_width="160dp"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="0"
android:text="@string/scan" />
</GridLayout>
CurrentItems.java
package com.example.fooditemmonitor;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class CurrentItems extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.current_inventory);
final Button addButton = (Button) findViewById(R.id.scanner);
final Button editInventoryButton = (Button) findViewById(R.id.editItem);
addButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CurrentItems.this, AddItem.class));
}
});
editInventoryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(CurrentItems.this, EditItems.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
}
addButton.setOnClickListener(new OnClickListener(){ // LINE 22
从文件发布的代码将与此相关。检查了引用文件的名称是否正确。
所有帮助将不胜感激 - 谢谢!
答案 0 :(得分:5)
我认为你写错了,应该是:
final Button addButton = (Button) findViewById(R.id.editItemCurrent);
final Button editInventoryButton = (Button) findViewById(R.id.scanCurrent);
我的布局中没有标记为scanner
或editItem
的按钮。
答案 1 :(得分:3)
R.id.scanner和R.id.editItem不存在于current_inventory.xml文件中。
然后,当你在(Button)中尝试setOnClickListener时,addButton为null并抛出NullPointerException。