java.lang.NullPointerException 无法找出导致错误的原因
Posted
技术标签:
【中文标题】java.lang.NullPointerException 无法找出导致错误的原因【英文标题】:java.lang.NullPointerException Cant figure out what is causing the error 【发布时间】:2013-10-14 16:27:49 【问题描述】:我正在为课堂作业创建一个非常基本的 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 ComponentInfohotchkissmobilesolutions.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
这是 android 清单:
<?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_
android:layout_
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_
android:layout_
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 行的问题的任何帮助都会非常有帮助。提前致谢!
【问题讨论】:
顺便说一句,我已经清理了系统。 第 72 行是什么?你说你知道它来自第 72 行。要我数行数吗? ait 是 btnTotal 的 onclicklistener 【参考方案1】:您没有btnTotal
,因此当您单击它时,其值为null
。
见:
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);
//add this
btnTotal = (Button) findViewById(R.id.btnTotal);
btnTotal
没有按钮。添加那个按钮,你会没事的。简单的错误。无时无刻不在发生。祝你好运。
【讨论】:
就像我说的......它一直在发生。有时你只需要另一双眼睛。很高兴我能帮忙:)【参考方案2】:你没有初始化你的 total
字段,所以它会抛出 NPE
【讨论】:
不,即使初始化了它仍然会抛出相同的异常以上是关于java.lang.NullPointerException 无法找出导致错误的原因的主要内容,如果未能解决你的问题,请参考以下文章
亲測,Eclipse报"An error has occurred,See error log for more details. java.lang.NullPointerExce