1

我有一个显示 ListView 的活动。我想在 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)
4

1 回答 1

2

替换以下行onCreate

runnable.run();

handler.postDelayed(runnable, 25000);

并删除该行

handler.postDelayed(runnable, 25000);

returnToMenu方法上。

于 2012-10-25T09:09:20.790 回答