gpt4 book ai didi

android - 使用 ConnectivityManager 和 NetworkInfo 避免 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:20:22 25 4
gpt4 key购买 nike

在我的应用程序中,我通过使用 PHP 脚本查询数据库。目前,它会在应用程序出现在屏幕上时立即获取信息。但是,如果您没有互联网连接,它会返回 NullPointerException,因为它没有获取任何数据。从那以后,我用 ConnectivityManager 和 NetworkInfo 更新了我的代码,如果用户没有连接到他们的网络或 WiFi,它会显示提示框。问题是它跳过了这一点,只是像往常一样执行其余代码。

public class Shc_BalloonSat_Activity extends Activity
{
int historyCountFromUser;
httpAPI api;
mapAPI map;
DecimalFormat df = new DecimalFormat("##.#####");
DecimalFormat decimalf = new DecimalFormat("##.######");
SharedPreferences pref;
Editor prefEditor;
String lastpacketsPHP;

// User to determine how many packet the user would like to see.
int userDefinedCount = 5;

/*
* Called when the activity is first created.
*/

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String returned = "";
lastpacketsPHP = "";
pref = getSharedPreferences("shared_prefs", 0);
prefEditor = pref.edit();
prefEditor.putString(lastpacketsPHP, "/* PHP file url goes here */");
prefEditor.commit();

if(!isNetworkConnected(this))
{
Toast.makeText(this, "Please connect to the internet to access the full functionality of this app.", Toast.LENGTH_LONG).show();
}

else
{
api = new httpAPI(this);
map = new mapAPI(this);

try
{
returned = api.getData();
}

catch (Exception e)
{
e.printStackTrace();
}

TextView infoTV = (TextView)this.findViewById(R.id.info);
infoTV.setText(returned);
assignInfoToInfoTextView();
assignInfoToHistoryTextView();
}

}

public boolean isNetworkConnected(Context context)
{
ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectionManager.getActiveNetworkInfo() != null && connectionManager.getActiveNetworkInfo().isAvailable() && connectionManager.getActiveNetworkInfo().isConnected())
{
return true;
}

else
{
return false;
}
}

知道我做错了什么吗?

更新(LogCat 错误信息):

02-19 13:55:19.450: E/log_tag(443): Error in HTTP connection: java.net.UnknownHostException: Host is unresolved: space.uah.edu:80
02-19 13:55:19.450: E/log_tag(443): Error converting result: java.lang.NullPointerException
02-19 13:55:19.450: E/log_tag(443): Error in HTTP connection: java.net.UnknownHostException: Host is unresolved: space.uah.edu:80
02-19 13:55:19.450: E/log_tag(443): Error converting result: java.lang.NullPointerException
02-19 13:55:19.450: E/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
02-19 13:55:19.473: E/AndroidRuntime(443): java.lang.RuntimeException: Unable to start activity ComponentInfo{shc_BalloonSat.namespace/shc_BalloonSat.namespace.Shc_BalloonSat_Activity}: java.lang.NullPointerException: println needs a message
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.os.Handler.dispatchMessage(Handler.java:99)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.os.Looper.loop(Looper.java:123)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-19 13:55:19.473: E/AndroidRuntime(443): at java.lang.reflect.Method.invokeNative(Native Method)
02-19 13:55:19.473: E/AndroidRuntime(443): at java.lang.reflect.Method.invoke(Method.java:521)
02-19 13:55:19.473: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-19 13:55:19.473: E/AndroidRuntime(443): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-19 13:55:19.473: E/AndroidRuntime(443): at dalvik.system.NativeStart.main(Native Method)
02-19 13:55:19.473: E/AndroidRuntime(443): Caused by: java.lang.NullPointerException: println needs a message
02-19 13:55:19.473: E/AndroidRuntime(443): at android.util.Log.println(Native Method)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.util.Log.e(Log.java:208)
02-19 13:55:19.473: E/AndroidRuntime(443): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.assignInfoToInfoTextView(Shc_BalloonSat_Activity.java:173)
02-19 13:55:19.473: E/AndroidRuntime(443): at shc_BalloonSat.namespace.Shc_BalloonSat_Activity.onCreate(Shc_BalloonSat_Activity.java:75)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-19 13:55:19.473: E/AndroidRuntime(443): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-19 13:55 :19.473: E/AndroidRuntime(443): ... 11 more

最佳答案

假设您的连接检查逻辑工作正常,将其设为 if/else。如果未连接显示 toast ,否则继续逻辑。

     if(!isNetworkConnected(this))         {     
Toast.makeText(this, "Please connect to the internet to access the full functionality of this app.", Toast.LENGTH_LONG).show();
}
else
{
TextView infoTV = (TextView)this.findViewById(R.id.info);
infoTV.setText(returned);
assignInfoToInfoTextView();
assignInfoToHistoryTextView();
}

关于android - 使用 ConnectivityManager 和 NetworkInfo 避免 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9351949/

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