2011-04-07 79 views
1

我創建了3個活動。應用程序拋出一個NullPointerException

  1. ElecBillActivity

  2. AddBillActivity

  3. ResultActivity

我成功地照耀處進入第二活動使用的意圖,但只要我單擊第二個活動Submit按鈕,我收到一個例外。

我嘗試了很多方法,但是我無法理解這個問題。

這是我的logcat

E/AndroidRuntime( 506): FATAL EXCEPTION: main 
E/AndroidRuntime( 506): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sai.android.billing/com.sai.android.billing.ResultActivity}: java.lang.NullPointerException 
E/AndroidRuntime( 506):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1544) 
E/AndroidRuntime( 506):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638) 
E/AndroidRuntime( 506):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
E/AndroidRuntime( 506):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928) 
E/AndroidRuntime( 506):  at android.os.Handler.dispatchMessage(Handler.java:99) 
E/AndroidRuntime( 506):  at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime( 506):  at android.app.ActivityThread.main(ActivityThread.java:3647) 
E/AndroidRuntime( 506):  at java.lang.reflect.Method.invokeNative(NativeMethod) 
E/AndroidRuntime( 506):  at java.lang.reflect.Method.invoke(Method.java:507) 
E/AndroidRuntime( 506):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
E/AndroidRuntime( 506):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
E/AndroidRuntime( 506):  at dalvik.system.NativeStart.main(Native Method) 
E/AndroidRuntime( 506): Caused by: java.lang.NullPointerException 
E/AndroidRuntime( 506):  at com.sai.android.billing.ResultActivity.<init>(ResultActivity.java:21) 
E/AndroidRuntime( 506):  at java.lang.Class.newInstanceImpl(Native Method) 
E/AndroidRuntime( 506):  at java.lang.Class.newInstance(Class.java:1409) 
E/AndroidRuntime( 506):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
E/AndroidRuntime( 506):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1536) 
E/AndroidRuntime( 506):  ... 11 more 
W/ActivityManager( 70): Force finishing activity com.sai.android.billing/.ResultActivity 
W/ActivityManager( 70): Force finishing activity com.sai.android.billing/.AddBillActivity 
W/ActivityManager( 70): Activity pause timeout for HistoryRecord{4079d8f8 com.sai.android.billing/.ResultActivity} 
I/ARMAssembler( 70): generated scanline__00000177:03515104_00001001_00000000 [91 ipp] (114 ins) at [0x445b36e0:0x445b38a8] in 1634844 ns 
I/Process ( 506): Sending signal. PID: 506 SIG: 9 
I/ActivityManager( 70): Process com.sai.android.billing (pid 506) has died. 
E/InputDispatcher( 70): channel '4060e840 com.sai.android.billing/com.sai.android.billing.ElecBillingActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x8 
E/InputDispatcher( 70): channel '4060e840 com.sai.android.billing/com.sai.android.billing.ElecBillingActivity (server)' ~ Channel is unrecoverably broken and will be disposed! 
I/WindowManager( 70): WIN DEATH: Window{4060e840 com.sai.android.billing/com.sai.android.billing.ElecBillingActivity paused=false} 
I/WindowManager( 70): WIN DEATH: Window{406f4868 com.sai.android.billing/com.sai.android.billing.AddBillActivity paused=true} 
E/InputDispatcher( 70): Received spurious receive callback for unknown input channel. fd=151, events=0x8 
W/InputManagerService( 70): Got RemoteException sending setActive(false) notification to pid 506 uid 10038 

我也提供了鏈接,我code

由於我的問題似乎很大,我的班級文件太大,我在此處提供了下載鏈接。
由於我是新用戶,因此無法在此處發佈屏幕截圖。

+1

請張貼在這個問題你的代碼的相關部分。它應該在LogCat輸出判斷的ResultActivity類的第21行左右。 – 2011-04-07 10:12:05

回答

4

根據你ResultActivity.java第21行得到了NullPointerException日誌...考慮進去一看你的源代碼,這是通過調用getIntent()這是後Activity是隻能被稱爲Activity成員函數引起的創建。因此,請將您的初始化代碼移至onCreate方法中。

更改您的成員定義到:

private TextView resultOk; 
int highLimit,lowLimit,highRate,lowRate,defaultRate,amount; 
Bundle bundle; 
String userName; 
String meterNumber; 
String prevReading; 
String currReading; 
int pR; 
int cR; 
int units; 
String u; 
String a; 

而且對您呼叫getIntent().getExtras(),並將其存儲在變量bundle。但是因爲您從未在您的IntentAddBillActivity中投入任何額外費用,因此bundle將爲null,並且當您嘗試bundle.getString(...)時,它會拋出另一個NullPointerException

首先要做的修改是在form.xml文件中爲屬性給RadioGroup。將它設置爲radios和這一行添加到您的成員變量在AddBillActivity

public static RadioGroup radios; 

除了這行來setUpViews方法:

radios = (RadioGroup)findViewById(R.id.radios); 

然後更改AddBillActivityaddTask方法

Intent intent1 = new Intent(AddBillActivity.this, ResultActivity.class); 
intent1.putExtra("bill_name", billNameEditText.getText().toString()); 
intent1.putExtra("bill_number", billNumEditText.getText().toString()); 
intent1.putExtra("bill_previous", billPrevEditText.getText().toString()); 
intent1.putExtra("bill_current", billCurrEditText.getText().toString()); 
intent1.putExtra("radio", radios.getCheckedRadioButtonId()); 
startActivity(intent1); 

現在您可以在中的onCreate方法中獲得所有這些附加信息:

bundle = getIntent().getExtras(); 
userName = bundle.getString("bill_name"); 
meterNumber = bundle.getString("bill_number"); 
prevReading = bundle.getString("bill_previous"); 
currReading = bundle.getString("bill_current"); 
pR = Integer.parseInt(prevReading); 
cR = Integer.parseInt(currReading); 
units = pR - cR; 
u = bundle.getString(String.valueOf(units)); 
a = bundle.getString(String.valueOf(units)); 

這應該這樣做;)

+0

@Michael Rose謝謝您的回覆。我應用了您建議的更改。但我收到了其他異常。我的Logcat說 'E/AndroidRuntime(622):導致:java.lang.NullPointerException E/AndroidRuntime(622):at com.sai.android.billing.ResultActivity.onCrea te(ResultActivity.java:38)' 我寫了代碼使用教程作爲參考,我會非常感激你,如果你可以幫幫我!! – 2011-04-07 10:54:03

+0

您是否在清單文件中添加了活動? – Salil 2011-04-07 11:10:29

+0

@Sai好的,我們去...你調用'getIntent()。getExtras()'來獲得'bundle',但是你絕不會在意圖中添加額外的東西!這應該在調用'startActivity(...)'之前在'AddBillActivity'中完成。我將在幾分鐘內更新我的答案,並提供所需的更改! – 2011-04-07 11:13:58

相關問題