gpt4 book ai didi

java - android变量空指针

转载 作者:行者123 更新时间:2023-11-29 07:51:27 26 4
gpt4 key购买 nike

我认为我正在检查的变量为空。我的 on resume 方法是这样做的:

此方法检查网络上的电子邮件,如果出现问题将返回 -1。我认为这会起作用。现在,当我运行它 10 次时,它开始工作了。所以我把它给了一个 friend 。他说这有时会让他异常(exception)。

///   Ok found it it was in a try catch in the OnCreate() 

tvEmail = (TextView) findViewById(R.id.itemz_email);
tvEmail.setTypeface(TitleFont);

/// and occasionally it didn't even get initialized.
/// I guess ill just init it again in the onResume() just in case.
/// Thanks!


@Override
protected void onResume() {
super.onResume();
tvEmail = (TextView) findViewById(R.id.itemz_email);
tvEmail.setTypeface(TitleFont);

if (EMAIL != null) {
tvEmail.setText("E-mail: " + EMAIL);
} else {
tvEmail.setText("E-mail: No Email Available"); //<----==LINE 77
}
}

这是我的用户遇到的错误:

 java.lang.RuntimeException: Unable to resume activity {com.codalata.craigslistchecker/com.codalata.craigslistchecker.ItemView}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2578)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2606)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
at android.app.ActivityThread.access$600(ActivityThread.java:133)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1198)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4777)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.codalata.craigslistchecker.ItemView.onResume(ItemView.java:77)<------/**/error/**/
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2568)

我应该这样检查并设置它吗?

if(EMAIL == null){
EMAIL = "E-mail: No Email Available"
tvEmail.setText(EMAIL);
}else {
tvEmail.setText("E-mail: " + EMAIL);
}

最佳答案

您在包含 tvEmail.setText(...) 的行上收到了一个 NullReferenceException;交换条件 block 以检查 EMAIL 是否为 null 与否不会改变事情,因为 EMAIL 没有参与导致错误的行。

您需要查看 tvEmail,因为 that 在您的错误发生时是 null

关于java - android变量空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20807669/

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