gpt4 book ai didi

android - 应用程序抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-29 16:24:03 27 4
gpt4 key购买 nike

我创建了 3 个 Activity 。

  1. 电子账单 Activity

  2. AddBillActivity

  3. 结果 Activity

我使用 Intent 成功进入了第二个 Activity,但是当我在第二个 Activity 中单击“提交”按钮时,我收到了一个异常。

我尝试了很多方法,但我无法理解问题。

这是我的日志

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 行的 ResultActivity.java 中遇到了一个 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 中。但是由于您从未在 AddBillActivity 中的 Intent 中添加任何额外内容,因此 bundle 将为 null 并且当您尝试 bundle.getString(...) 它将抛出另一个 NullPointerException!

首先要做的更改是在 form.xml 文件中为您的 RadioGroup 提供一个 android:id 属性。将其设置为 radios 并将此行添加到 AddBillActivity 中的成员变量:

public static RadioGroup radios;

以及 setUpViews 方法的这一行:

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

然后将AddBillActivity中的addTask方法修改为

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);

现在您可以在 ResultActivityonCreate 方法中获得所有这些额外功能:

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));

应该这样做 ;)

关于android - 应用程序抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5579293/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com