我正在为课堂作业创建一个非常基本的 POS 系统。我不断收到可怕的空指针异常。我知道错误来自主要活动的第 72 行,但它在主要活动和 xml 中都声明了。
这是日志猫:
10-07 18:05:36.946: D/AndroidRuntime(1279): Shutting down VM
10-07 18:05:36.946: W/dalvikvm(1279): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
10-07 18:05:36.958: E/AndroidRuntime(1279): FATAL EXCEPTION: main
10-07 18:05:36.958: E/AndroidRuntime(1279): java.lang.RuntimeException: Unable to start activity ComponentInfo{hotchkissmobilesolutions.kudlerpos/hotchkissmobilesolutions.kudlerpos.MainActivity}: java.lang.NullPointerException
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.os.Handler.dispatchMessage(Handler.java:99)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.os.Looper.loop(Looper.java:137)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-07 18:05:36.958: E/AndroidRuntime(1279): at java.lang.reflect.Method.invokeNative(Native Method)
10-07 18:05:36.958: E/AndroidRuntime(1279): at java.lang.reflect.Method.invoke(Method.java:525)
10-07 18:05:36.958: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-07 18:05:36.958: E/AndroidRuntime(1279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-07 18:05:36.958: E/AndroidRuntime(1279): at dalvik.system.NativeStart.main(Native Method)
10-07 18:05:36.958: E/AndroidRuntime(1279): Caused by: java.lang.NullPointerException
10-07 18:05:36.958: E/AndroidRuntime(1279): at hotchkissmobilesolutions.kudlerpos.MainActivity.onCreate(MainActivity.java:72)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.Activity.performCreate(Activity.java:5133)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-07 18:05:36.958: E/AndroidRuntime(1279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-07 18:05:36.958: E/AndroidRuntime(1279): ... 11 more
这是安卓清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hotchkissmobilesolutions.kudlerpos"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="hotchkissmobilesolutions.kudlerpos.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是主要活动:
package hotchkissmobilesolutions.kudlerpos;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
double dairy = 4.99, seafood = 15.99, wine = 20, meat = 5.99,
cheese = 2.99, fruit = 1.99, vegetable = 0.69;
double total, tax = 0.0725;
String total2;
Button btnDairy, btnSeafood, btnWine, btnMeat, btnFruit, btnVegetable,
btnTotal, btnClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDairy = (Button) findViewById(R.id.btnDairy);
btnSeafood = (Button) findViewById(R.id.btnSeafood);
btnWine = (Button) findViewById(R.id.btnWine);
btnMeat = (Button) findViewById(R.id.btnMeat);
btnFruit = (Button) findViewById(R.id.btnFruit);
btnVegetable = (Button) findViewById(R.id.btnVegetable);
btnClear = (Button) findViewById(R.id.btnClear);
btnDairy.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + dairy) + (total * tax);
System.out.println(total);
}
});
btnSeafood.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + seafood) + (total * tax);
System.out.println(total);
}
});
btnWine.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + wine) + (total * tax);
System.out.println(total);
}
});
btnMeat.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + meat) + (total * tax);
System.out.println(total);
}
});
btnFruit.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + fruit) + (total * tax);
System.out.println(total);
}
});
btnVegetable.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = (total + vegetable) + (total * tax);
System.out.println(total);
}
});
btnTotal.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total2 = new Double(total).toString();
final TextView textView = (TextView) findViewById(R.id.textViewTotal);
textView.setText(total2);
System.out.println(total);
System.out.println(total2);
}
});
btnClear.setOnClickListener(new OnClickListener() {
public void onClick(View V) {
total = 0;
total2 = new Double(total).toString();
final TextView textView = (TextView) findViewById(R.id.textViewTotal);
textView.setText(total2);
System.out.println(total);
}
});
}
这是activity_main.xml:
<?xml version="1.0" encoding="UTF-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="6"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:rowCount="8"
tools:context=".MainActivity"
tools:targetApi="14" >
<TextView android:id="@+id/txtViewInstructions"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="@string/InstructionText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button android:id="@+id/btnDairy"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="3"
android:onClick="onCLickDairy"
android:text="@string/Dairy" />
<Button android:id="@+id/btnWine"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="3"
android:onClick="onCLickWine"
android:text="@string/Wine" />
<Button android:id="@+id/btnMeat"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="4"
android:onClick="onCLickMeat"
android:text="@string/Meat" />
<Button android:id="@+id/btnSeafood"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="4"
android:onClick="onCLickSeafood"
android:text="@string/Seafood" />
<Button android:id="@+id/btnVegetable"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="5"
android:onClick="onCLickVegetable"
android:text="@string/Vegetable" />
<TextView android:id="@+id/txtViewTotalDue"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="7"
android:text="@string/TotalDue"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView android:id="@+id/textViewTotal"
android:layout_width="260dp"
android:layout_height="93dp"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="6"
android:text="@string/total"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button android:id="@+id/btnTotal"
android:layout_column="0"
android:layout_gravity="left|center_vertical"
android:layout_row="7"
android:onClick="onCLickTotal"
android:text="@string/TotalButton" />
<Button android:id="@+id/btnFruit"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="5"
android:onClick="onCLickFruit"
android:text="@string/Fruit" />
<Button android:id="@+id/btnClear"
android:layout_column="0"
android:layout_gravity="left|bottom"
android:layout_row="7"
android:text="@string/ClearButton" />
</GridLayout>
找到主要 activity.java 中 btnTotal 行的问题的任何帮助都会非常有帮助。提前致谢!