我创建了3个活动。
ElecBillActivity
AddBillActivity
ResultActivity
我使用Intent成功进入第二个Activity,但是当我点击第二个Activity中的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的链接。
由于我的问题似乎很大而我的班级文件太大,我在这里提供了下载链接 由于我是新用户,因此无法在此处发布屏幕截图。
答案 0 :(得分:4)
根据日志,您在第21行的NullPointerException
中获得ResultActivity.java
...查看您的源代码,这是由调用getIntent()
成员函数引起的Activity
的{{1}}只能在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
中。但是,由于您从未在Intent
AddBillActivity
bundle
中添加任何额外内容,null
将为bundle.getString(...)
,当您尝试NullPointerException
时,它会抛出另一个android:id
RadioGroup
}!
要做的第一个更改是在form.xml文件中为radios
提供AddBillActivity
属性。将其设置为public static RadioGroup radios;
,并将此行添加到setUpViews
中的成员变量:
radios = (RadioGroup)findViewById(R.id.radios);
以及addTask
方法的这一行:
AddBillActivity
然后将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
方法更改为
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));
中的{{1}}方法获取所有额外内容:
{{1}}
这应该这样做;)