带有片段接口侦听器的NullPointerException

时间:2012-08-20 08:53:34

标签: android interface nullpointerexception fragment

对不起,我确信这很简单,但我无法看到我在哪里出错。

我有一个Fragment(工具列表)可以在两个窗格设置中启动许多其他片段(工具)。有一项活动位于&当我们按下“关闭按钮”时,我正在使用它来管理工具碎片的移除。然后,如果存在一个工具,则从后台弹出一个工具。

奇怪的是,我在一个片段中工作但是'cut&粘贴'代码到另一个片段不起作用&抛出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)

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我可以看到你在每个片段中定义了2个具有相同名称的接口。

public interface OnFragmentButtonClickedListener {
     public void onFragmentButtonClicked (int button, Fragment fragId);
}

但是活动只能实现一个具有此名称的接口(在这种情况下,我相信你的活动实现了第一个片段的接口),这可能导致第二个片段的错误空指针异常。

//where is OnFragmentButtonClickedListener come from?
public class MyActivity extends FragmentActivity implements FirstFragment OnFragmentButtonClickedListener 

尝试在2个片段之外编写OnFragmentButtonClickedListener(可能在另一个文件中)