gpt4 book ai didi

android - 用 Handler 覆盖 onBackPressed 会导致 NPE

转载 作者:行者123 更新时间:2023-11-29 21:56:40 27 4
gpt4 key购买 nike

我有一个显示 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)

最佳答案

替换 onCreate 中的以下行

runnable.run();

handler.postDelayed(runnable, 25000);

并删除该行

handler.postDelayed(runnable, 25000);

来自 returnToMenu 方法。

关于android - 用 Handler 覆盖 onBackPressed 会导致 NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13065110/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com