Application throws a NullPointerException
I created 3 Activities.
ElecBillActivity
AddBillActivity
ResultActivity
I successfully enterd into the second Activity using an Intent but as soon as I click the Submit button in the second Activity, I am getting an exception.
I tried many ways but I am unable to understand the problem.
This is my 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/开发者_如何学CAndroidRuntime( 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
I am also providing the link for my code.
As my problem seems to big and my class files are too big I provided the download link here.
As I am a new user I am unable to post the screenshots here.According to the logs you got a NullPointerException
in ResultActivity.java
on line 21... taking a look into your source code, this is caused by calling getIntent()
which is a member function of Activity
which can only be called after the Activity
is created. Therefore, move your initialization code into the onCreate
method.
Change your member definitions to:
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;
Further on you are calling getIntent().getExtras()
and store it in the variable bundle
. But as you never put any extras in your Intent
in AddBillActivity
, bundle
will be null
and when you try bundle.getString(...)
it will throw another NullPointerException
!
First change to make is to give an android:id
attribute to your RadioGroup
in the form.xml file. Set it to radios
and add the this line to your member variables in AddBillActivity
:
public static RadioGroup radios;
As well as this line to the setUpViews
method:
radios = (RadioGroup)findViewById(R.id.radios);
Then change the addTask
method in AddBillActivity
to
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);
And now you can get all those extras in your onCreate
method in ResultActivity
:
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));
This should do it ;)
精彩评论