用Handler覆盖onBackPressed会导致NPE

时间:2012-10-25 09:02:36

标签: android handler

我有一个显示ListView的Activity。我想在25秒之后调用Activity的onBackPressed()。目前它正在引发NPE。谁能告诉我为什么?谢谢。它也没有显示listView,它会立即回到调用屏幕,然后在指定的时间之后,NPE就会发生。

@Override
protected void onNewIntent(Intent intent) {
    setIntent(intent);
    super.onNewIntent(intent);
}

 Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        public void run() {
            returnToMenu();

        }

        private void returnToMenu() {

            handler.postDelayed(runnable, 25000);
            onBackPressed();
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.getrotalayout);

     setCurrentDateOnView();

    nfcscannerapplication = (NfcScannerApplication) getApplication();

    //set titlebar to carer's name
    Cursor cursorCarerName = nfcscannerapplication.loginValidate.queryAllFromCarer();
    cursorCarerName.moveToLast();
    String carerTitleName = cursorCarerName.getString(cursorCarerName
                    .getColumnIndex(LoginValidate.C_CARER_FIRSTNAME)) + " " + cursorCarerName.getString(cursorCarerName
                            .getColumnIndex(LoginValidate.C_CARER_LASTNAME)) ;
    setTitle(carerTitleName + " is currently logged in");


    listView = (ListView) findViewById(R.id.rotalist);
    textViewDate = (TextView)findViewById(R.id.textviewdate);
    Log.e(TAG, "textview = "+textViewDate);


    listView.setAdapter(arrayAdapter);
    listView.setOnItemClickListener(this);

    runnable.run();

}// end of onCreate

10-25 10:00:25.780: E/AndroidRuntime(20896): FATAL EXCEPTION: main
10-25 10:00:25.780: E/AndroidRuntime(20896): java.lang.NullPointerException
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1359)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:439)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:454)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.app.Activity.onBackPressed(Activity.java:2185)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at com.carefreegroup.GetRota$1.returnToMenu(GetRota.java:77)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at com.carefreegroup.GetRota$1.run(GetRota.java:70)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.os.Handler.handleCallback(Handler.java:608)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.os.Looper.loop(Looper.java:156)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at android.app.ActivityThread.main(ActivityThread.java:5045)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at java.lang.reflect.Method.invokeNative(Native Method)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at java.lang.reflect.Method.invoke(Method.java:511)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-25 10:00:25.780: E/AndroidRuntime(20896):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

onCreate

替换以下行
runnable.run();

handler.postDelayed(runnable, 25000);

并删除该行

handler.postDelayed(runnable, 25000);

来自returnToMenu方法。