在我的应用程序中,我通过使用PHP脚本查询数据库。目前,只要应用程序出现在屏幕上,它就会获取信息。但是,如果您没有Internet连接,则会返回NullPointerException,因为它没有获取任何数据。我已经使用ConnectivityManager和NetworkInfo更新了我的代码,如果用户未连接到他们的网络或WiFi,则会显示Toast。问题是它正在跳过这个并且正常执行其余的代码。
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
答案 0 :(得分:1)
假设您的连接检查逻辑工作正常,请将其设为if / else。如果没有连接show 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();
}