我正在尝试在我的应用程序启动之前进行网络验证。我只是在 if else 条件下添加 AlertDialog,如果没有网络,它将提示一个对话框,当用户单击 OK 按钮时,它将重定向到主菜单。我不知道我哪里做错了。请帮忙。谢谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
MODE_WORLD_READABLE);
String username = myPrefs.getString("MEM1", "");
if (username.equalsIgnoreCase("") || username.length() == 0) {
toast("Please register to scan product. Only registered user able to scan the product");
startActivity(new Intent(ProductPreview.this, MainMenu.class));
finish();
}
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext() .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
boolean connected = networkInfo != null && networkInfo.isAvailable()
&& networkInfo.isConnected();
if (connected) {
try {
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(
this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// Setup an intent filter for all MIME based dispatches
IntentFilter ndef = new IntentFilter(
NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
mFilters = new IntentFilter[] { ndef, };
// Setup a tech list for all NfcF tags
mTechLists = new String[][] { new String[] { MifareClassic.class
.getName() } };
Intent intent = getIntent();
resolveIntent(intent);
} catch (Exception ex) {
ex.toString();
}
}
else {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Attention");
dialog.setMessage("No Internet Connection. Please enable the wifi."
); dialog.setPositiveButton("OK", new
DialogInterface.OnClickListener() { public void
onClick(DialogInterface dialog, int whichButton) {
startActivity(new Intent(ProductPreview.this, MainMenu.class));
finish();
}
});
dialog.show();
}
}
日志猫
09-21 19:19:05.825: E/AndroidRuntime(8413): FATAL EXCEPTION: main
09-21 19:19:05.825: E/AndroidRuntime(8413): java.lang.RuntimeException: Unable to resume activity {com.smartag.mdec.dev/com.smartag.mdec.dev.ProductPreview}: java.lang.NullPointerException
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.os.Looper.loop(Looper.java:137)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-21 19:19:05.825: E/AndroidRuntime(8413): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 19:19:05.825: E/AndroidRuntime(8413): at java.lang.reflect.Method.invoke(Method.java:511)
09-21 19:19:05.825: E/AndroidRuntime(8413): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-21 19:19:05.825: E/AndroidRuntime(8413): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 19:19:05.825: E/AndroidRuntime(8413): at dalvik.system.NativeStart.main(Native Method)
09-21 19:19:05.825: E/AndroidRuntime(8413): Caused by: java.lang.NullPointerException
09-21 19:19:05.825: E/AndroidRuntime(8413): at com.smartag.mdec.dev.ProductPreview.onResume(ProductPreview.java:176)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.Activity.performResume(Activity.java:5082)
09-21 19:19:05.825: E/AndroidRuntime(8413): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
09-21 19:19:05.825: E/AndroidRuntime(8413): ... 12 more
恢复
@Override
public void onResume() {
super.onResume();
//ValidateLogin();
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
mTechLists);
}