对不起,我相信这会很简单,但我只是看不出我在哪里出错了。
我有一个片段(工具列表),可以在两个窗格中启动许多其他片段(工具)。上面有一个活动,我正在使用它来管理按下“关闭按钮”时工具碎片的移除。然后它会从后台堆栈中弹出一个工具(如果存在)。
奇怪的是,我在一个片段中进行了此操作,但是将“剪切并粘贴”到另一个片段中的代码不起作用并引发了 NPE。任何想法为什么非常感谢。
活动片段:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
public class MyActivity extends FragmentActivity implements OnFragmentButtonClickedListener {
private final static int CLOSE_BUTTON = 0;
...
public void onFragmentButtonClicked(int button, Fragment fragId) {
Log.i(TAG, "onFragmentButton");
// process closing of fragment
if (button == CLOSE_BUTTON) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.remove(fragId);
ft.commit();
}
// then restore most recent fragment from backstack
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
}
}
}
此片段有效:
import android.support.v4.app.Fragment;
public class FirstFragment extends Fragment implements OnClickListener{
OnFragmentButtonClickedListener fragmentButtonListener;
private final static int CLOSE_BUTTON = 0;
// empty constructor
public FirstFragment() {};
// Activity must implement this interface
public interface OnFragmentButtonClickedListener {
public void onFragmentButtonClicked (int button, Fragment fragId);
}
@Override
public void onAttach(Activity activity) {
Log.i(TAG, "onAttach");
super.onAttach(activity);
try {
fragmentButtonListener = (OnFragmentButtonClickedListener) activity;
} catch (ClassCastException e) {
Log.w(TAG, (activity.toString() + " must implement OnFragmentButtonClickedListener"));
}
}
...
public void onClick(View v) {
Log.i(TAG, "onClick");
switch (v.getId()) {
case R.id.btn_st_close:
//close activity
eraseData(prefs);
Log.i(TAG, "Frag Id = " + String.valueOf(getFragmentManager().findFragmentById(getId())));
fragmentButtonListener.onFragmentButtonClicked(CLOSE_BUTTON, getFragmentManager().findFragmentById(getId()));
break;
}
}
但是,当在 fragmentButtonListener 上调用 onFragmentButtonClicked() 方法时,这个会在 onClick 方法中抛出 NPE。
import android.support.v4.app.Fragment;
public class SecondFragment extends Fragment implements OnClickListener {
OnFragmentButtonClickedListener fragmentButtonListener;
private final static int CLOSE_BUTTON = 0;
// empty constructor
public SecondFragment() {};
// Activity must implement this interface
public interface OnFragmentButtonClickedListener {
public void onFragmentButtonClicked (int button, Fragment fragId);
}
@Override
public void onAttach(Activity activity) {
Log.i(TAG, "onAttach");
super.onAttach(activity);
try {
fragmentButtonListener = (OnFragmentButtonClickedListener) activity;
} catch (ClassCastException e) {
Log.w(TAG, (activity.toString() + " must implement OnFragmentButtonClickedListener"));
}
}
...
public void onClick(View v) {
Log.i(TAG, "OnClick");
switch (v.getId()) {
case R.id.btn_wc_close:
//close activity
eraseData(prefs);
Log.i(TAG, "Frag Id = " + String.valueOf(getFragmentManager().findFragmentById(getId())));
try {
fragmentButtonListener.onFragmentButtonClicked(CLOSE_BUTTON, getFragmentManager().findFragmentById(getId()));
}
catch (Exception e) {
Log.i(TAG, "Exception: " + e);
}
break;
}
}
从 logcat 中,我可以看到片段 Id 不为空,所以我很困惑。
08-20 09:13:49.096: I/Second Fragment(1691): onCreate
08-20 09:13:49.096: I/Second Fragment(1691): Frag Id = SecondFragment{41355d68 #1 id=0x7f090093}
08-20 09:13:49.106: I/Second Fragment(1691): onCreateView
08-20 09:13:49.406: I/Second Fragment(1691): onActivityCreated
08-20 09:13:49.560: I/Second Fragment(1691): onClick
0 09:13:51.766: I/Second Fragment(1691): Frag Id = SecondFragment{41355d68 #1 id=0x7f090093}
08-20 09:13:51.766: W/dalvikvm(1691): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-20 09:13:51.826: E/AndroidRuntime(1691): FATAL EXCEPTION: main
08-20 09:13:51.826: E/AndroidRuntime(1691): java.lang.NullPointerException
08-20 09:13:51.826: E/AndroidRuntime(1691): at SecondFragment.onClick(IceTurbFragment.java:136)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.view.View.performClick(View.java:4084)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.view.View$PerformClick.run(View.java:16966)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.os.Handler.handleCallback(Handler.java:615)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.os.Handler.dispatchMessage(Handler.java:92)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.os.Looper.loop(Looper.java:137)
08-20 09:13:51.826: E/AndroidRuntime(1691): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-20 09:13:51.826: E/AndroidRuntime(1691): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 09:13:51.826: E/AndroidRuntime(1691): at java.lang.reflect.Method.invoke(Method.java:511)
08-20 09:13:51.826: E/AndroidRuntime(1691): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-20 09:13:51.826: E/AndroidRuntime(1691): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-20 09:13:51.826: E/AndroidRuntime(1691): at dalvik.system.NativeStart.main(Native Method)
谢谢你的帮助。