警报对话框 - 无法恢复活动



我正在尝试在应用程序启动之前进行网络验证。我只是在 if else 条件下添加 AlertDialog,如果没有网络,它将提示一个对话框,当用户单击确定按钮时,它将重定向到主菜单。我不知道我哪里做错了。请帮忙。谢谢

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);
}

//如果您没有任何方法可做onResume()只需删除它或注释它。

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

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

如果(已连接){

.

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

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

希望这对你有帮助

最新更新