我試圖在後退按鈕被觸摸時在我的片段中執行一些代碼。我在我的活動中創建一個接口並在我的片段中實現它。但是當我回頭時,它會崩潰。我做過的事情有什麼問題?如何通知一個活動,後退按鈕已被按下片段?
import android.support.v7.app.ActionBarActivity;
public class myActionBarActivity extends ActionBarActivity{
public OnBackPressedListener onBackPressedListener;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbaractivity_layout); //loads fragment
@Override
public void onBackPressed(){
onBackPressedListener.onActivityBackPressed();
}
public interface OnBackPressedListener{
public void onActivityBackPressed();
}
}
片段:
import android.support.v4.app.Fragment;
public class myFragment extends Fragment implements myActionBarActivity.OnBackPressedListener{
...
...
@Override
public void onActivityBackPressed() {
Toast.makeText(getActivity(), "Back pressed", Toast.LENGTH_SHORT).show();
}
}
logcat的:
06-21 18:04:30.351: E/InputEventSender(6530): Exception dispatching finished signal.
06-21 18:04:30.351: E/MessageQueue-JNI(6530): Exception in MessageQueue callback: handleReceiveCallback
06-21 18:04:30.351: E/MessageQueue-JNI(6530): java.lang.NullPointerException
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at mypackage.myActionBarActivity.onBackPressed(myActionBarActivity.java:23)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.app.Activity.onKeyUp(Activity.java:2222)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.KeyEvent.dispatch(KeyEvent.java:2917)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.app.Activity.dispatchKeyEvent(Activity.java:2456)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.dispatchKeyEvent(ActionBarActivityDelegateICS.java:258)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2211)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4583)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4559)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4268)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4325)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4191)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4157)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4214)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4183)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4301)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4451)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2223)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1895)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1886)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2200)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.os.MessageQueue.nativePollOnce(Native Method)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.os.MessageQueue.next(MessageQueue.java:132)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.os.Looper.loop(Looper.java:124)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at android.app.ActivityThread.main(ActivityThread.java:5493)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at java.lang.reflect.Method.invoke(Method.java:525)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
06-21 18:04:30.351: E/MessageQueue-JNI(6530): at dalvik.system.NativeStart.main(Native Method)
做到這一點已經在這裏https://stackoverflow.com/a/19133522/2105986解釋正確的方法。但我真的不明白。我正在尋找一種更簡單的方式來使其工作。
您必須設置您的偵聽器,因爲它當前始終爲null。按照我在尋找的5 – WonderCsabo
@WonderCsabo的指南中的步驟。謝謝 – faizal
@Narayansoni謝謝,但我寧願不強迫我的碎片來實現接口。因此給片段選擇「設置」監聽器是一個更靈活的選擇。 – faizal