我有ViewPager的Activity,在ViewPager Adapter中我为每个位置提供了Fragment。
示例片段是DebugFragment。我在下面写了源代码。
public class DebugFragment extends android.support.v4.app.Fragment {
private OnFragmentInteractionListener mListener;
public interface OnFragmentInteractionListener {
void onFragmentInteraction(int someValue);
}
public static DebugFragment newInstance() {
DebugFragment fragment = new DebugFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
mListener.onFragmentInteraction(0);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LocalBroadcastManager.getInstance(getContext()).registerReceiver(mMessageReceiver,
new IntentFilter("com.android.example.INITIAL_REQUEST"));
}
@Override
public void onDestroy() {
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(mMessageReceiver);
super.onDestroy();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
@Override
public void onResume() {
super.onResume();
getUserData();
}
public void getUserData() {
// Inside Background Thread
if (getActivity() == null) {
return;
}
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
mListener.onFragmentInteraction(0); // This line throws NPE
}
});
}
我的活动实施如下。
public class DebugActivity extends AppCompatActivity implements
DebugFragment.OnFragmentInteractionListener {
// Other Activity Callback
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DebugFragment debugFragment = ((DebugFragment) mViewPagerAdapter.getRegisteredFragment(2));
if (debugFragment != null) {
debugFragment.getUserData();
}
}
}
}
}
我从Frag的onResume,BroadcastReceiver,Activity的OnActivityResult调用我的DebugFragment的getUserData。
有时我在尝试访问FragmentListener时会在getUserData中得到NullPointerException,即mListener。 我想知道为什么?
因为我已经检查了Activity null。这还不够。我是否还必须检查mListener的空值?如果有人会向我解释活动不会为空但我的mListener为空的情况会很棒。我只在纵向模式下保持活动。
修改
我的适配器代码
public abstract class TabPagerAdapter extends FragmentPagerAdapter {
public TabPagerAdapter(FragmentManager fm) {
super(fm);
}
public abstract View getTabView(int position);
}
public class SecondaryPagerAdapter extends TabPagerAdapter {
private static final int NUM_PAGES = 5;
private String tabTitles[] = new String[] { "Today", "New", "Calendar", "In-progress", "Invoices" };
private int[] imageResId = { R.drawable.ic_tab_hired_pro, R.drawable.ic_tab_history,
R.drawable.ic_tab_today, R.drawable.ic_tab_inprogress, R.drawable.ic_tab_invoices };
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public SecondaryPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
case 1:
case 3:
return ServiceRequestFragment.newInstance(tabTitles[position]);
case 2:
return DebugFragment.newInstance();
case 4:
return InvoicesFragment.newInstance();
default:
throw new RuntimeException("No fragment for this position");
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public View getTabView(int position) {
CustomTab customTab = new CustomTab(DashBoardActivity.this);
customTab.bindWith(imageResId[position], tabTitles[position]);
return customTab;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
从我的活动中我称之为
mSecondaryPagerAdapter = new SecondaryPagerAdapter(getSupportFragmentManager());
mSecondaryPager = (ViewPager) findViewById(R.id.dashboard_pager);
mSecondaryPager.setOffscreenPageLimit(4);
mSecondaryPager.setAdapter(mSecondaryPagerAdapter);
答案 0 :(得分:12)
当你调用getActivity().runOnUiThread(new Runnable() {})
时,这会在 UI线程上已经排队的其他内容之后将Runnable在UI线程上运行,这可能包括对{{1}的调用},将onDestroy()
设置为null。这意味着,当广播进入时,您的应用可能会关闭。虽然在正常情况下这不应该是一个问题,因为您在清除mListener
之前取消注册您的接收器,它是可能之后Runnable已经入队,这就是侦听器在执行时为空的原因。
要避免NPE,您应该检查Runnable中的mListener
。但是,这仍然意味着在您的Fragment被销毁之后安排回调,因此,您的Fragment实例被泄露。最好的办法是创建一个Handler并将Runnable发布到它而不是调用mListener == null
。然后,在runOnUiThread()
中,调用基本上清除队列的onDestroy()
,以便根本不会调用Runnable。
答案 1 :(得分:2)
mMessageReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(mListener != null){
mListener.onFragmentInteraction(0);
}
}
};
当app接收广播时,此活动可能已于当时关闭
答案 2 :(得分:1)
@Jschools有最好的答案。但作为替代方案,您还可以使用Eventbus而不是侦听器模式。事件总线只会广播事件,如果活动未激活,则不会发生任何事件,也不会抛出NPE。
答案 3 :(得分:0)
根据文档onAttach在片段首次附加到其上下文时被调用。既然你没有直接在活动中实例化片段,而是通过适配器,你确定正在调用DebugFragment的onAttach(Context context)方法,因为那是你将上下文分配给mListener的地方吗?
有报道称没有调用onAttach(Context context)方法的情况。请参阅https://code.google.com/p/android/issues/detail?id=183358
您可以一起跳过Fragment生命周期方法,并在newInstance()中设置mListener。
public static DebugFragment newInstance(Context context) {
DebugFragment fragment = new DebugFragment();
fragment.setListener(context)
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
private void setListener(Context context){
if (context.instanceof(OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
}
else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}