我创建了3个活动。
电子账单活动
AddBillActivity
结果活动
我已成功使用Intent进入了第二个活动,但是在第二个活动中单击“提交”按钮后,我遇到了异常。
我尝试了很多方法,但是我无法理解问题。
这是我的日志
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的链接。
由于我的问题似乎很大,而我的班级文件太大,因此我在此处提供了下载链接。
由于我是新用户,因此无法在此处发布屏幕截图。
最佳答案
根据日志,您在第21行的NullPointerException
中有一个ResultActivity.java
...查看您的源代码,这是由于调用getIntent()
引起的,该Activity
是Activity
的成员函数,只能调用onCreate
创建后。因此,请将您的初始化代码移到getIntent().getExtras()
方法中。
将您的成员定义更改为:
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;
进一步,您正在调用
bundle
并将其存储在变量Intent
中。但是,因为您从未在AddBillActivity
的bundle
中放置任何附加字符,所以null
将是bundle.getString(...)
,当您尝试NullPointerException
时,它将抛出另一个android:id
!首先要做的更改是在form.xml文件中为您的
RadioGroup
提供一个radios
属性。将其设置为AddBillActivity
并将此行添加到setUpViews
中的成员变量中:public static RadioGroup radios;
以及
addTask
方法的这一行:radios = (RadioGroup)findViewById(R.id.radios);
然后将
AddBillActivity
中的onCreate
方法更改为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);
现在,您可以在
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));
这应该做;)