AlertDialog - 无法恢复活动

时间:2012-09-21 11:23:32

标签: android android-alertdialog

我正在尝试在应用开始之前进行网络验证。我只是在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();            
    }
}

logcat的

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

的onResume

@Override
public void onResume() {
    super.onResume();
    //ValidateLogin();
    mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters,
            mTechLists);

}

2 个答案:

答案 0 :(得分:0)

根据您新编辑的代码,您正在引用madapter参考而不将其作为对象。但实际上您将其作为对象在

如果(已连接) {

........ mAdapter = NfcAdapter.getDefaultAdapter(this);

} 在使用该引用之前替换该行代码

希望这会对你有所帮助

答案 1 :(得分:0)

//如果您在onResume()中没有任何方法可以删除或评论它。

或者只是阻止onresume方法中的行ProductPreview.java:176,该行在那里变为空。