切换到特定片段会产生奇怪的 java.lang.NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28014781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switching to a specific fragment gives strange java.lang.NullPointerException
提问by Antoine Sauray
Here is the problem i am currently facing. I have switched from Eclipse with ADT plugin to Android Studio recently and an error i never encountered on Eclipse appeared with Android studio.
这是我目前面临的问题。我最近从带有 ADT 插件的 Eclipse 切换到 Android Studio,我在 Eclipse 上从未遇到过的错误出现在 Android Studio 中。
When I switch to a specific fragment called "LineFragment" I get the following error :
当我切换到名为“LineFragment”的特定片段时,出现以下错误:
java.lang.NullPointerException: Attempt to write to field 'int android.support.v4.app.Fragment.mNextAnim' on a null object reference
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:708)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
It seems to have something to do with the fragment being null or so. I have search on the internet but only very few people have encoutered something like that.
这似乎与片段为 null 左右有关。我在互联网上搜索过,但只有极少数人遇到过这样的事情。
Here is the code of my fragment (it extends android.support.v4.app.Fragment like all the 3 fragments)
这是我的片段的代码(它像所有 3 个片段一样扩展了 android.support.v4.app.Fragment)
public class LineFragment extends Fragment{
private View v;
private ListView list;
private LineList listAdapter;
private Home home; // Current activity
public static LineFragment newInstance(String chaine) {
LineFragment fragment = new LineFragment ();
Bundle args = new Bundle();
args.putString("LIGNE", chaine);
fragment.setArguments(args);
return fragment;
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
registerForContextMenu(list);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_ligne, container, false);
home = (Home) this.getActivity();
initInterface();
attachReactions();
setHasOptionsMenu(true);
return v;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.line_menu, menu);
MenuItem searchMenuItem = menu.findItem(R.id.action_search);
SearchManager searchManager = (SearchManager) getActivity().getSystemService( Context.SEARCH_SERVICE );
SearchView search = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
SearchViewCompat.setInputType(search, InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_POSTAL_ADDRESS);
search.setSearchableInfo(searchManager.getSearchableInfo(home.getComponentName()));
//search.setSubmitButtonEnabled(true);
int id = search.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView textView = (TextView) search.findViewById(id);
textView.setTextColor(Color.WHITE);
textView.setHintTextColor(Color.WHITE);
search.setOnQueryTextListener(new GreenOnQueryTextListener(list));
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public void onResume() {
super.onResume();
Log.d("OnResume()", "Ligne");
}
@Override
public void setUserVisibleHint(boolean visible){
super.setUserVisibleHint(visible);
if (visible && isResumed()){
//Only manually call onResume if fragment is already visible
//Otherwise allow natural fragment lifecycle to call onResume
onResume();
}
}
/**
* Initializes the graphical interface of the fragment.
*/
private void initInterface(){
list = (ListView) v.findViewById(R.id.list);
list.setTextFilterEnabled(true);
}
/**
* Sets the reactions of the control elements
*/
private void attachReactions(){
ArrayList<Ligne> lignes = new ArrayList<Ligne>(Globale.engine.getReseau().getLignes().values());
listAdapter = new LineList(getActivity(), lignes);
list.setAdapter(listAdapter);
list.setOnItemClickListener(new LineClickListener(home));
}
Here is my PagerAdapter :
这是我的 PagerAdapter :
public class KiceoFragmentPagerAdapter extends FragmentPagerAdapter{
private final int PAGE_COUNT = 3;
public KiceoFragmentPagerAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
// Top Rated fragment activity
return new LineFragment();
case 1:
// Games fragment activity
return new StopFragment();
case 2:
// Movies fragment activity
return new CustomMapFragment();
}
return new LineFragment();
}
}
Does anyone have a clue where this is coming from ? Does it really have something to do with Android Studio ? Thanx
有没有人知道这是从哪里来的?它真的与 Android Studio 有关系吗?谢谢
I tried switching to support V13 fragments but It didn't change anything.
我尝试切换到支持 V13 片段,但它没有改变任何东西。
回答by Antoine Sauray
I managed to solve the problem by overrinding the onDestroy method in my map fragment.
我通过覆盖地图片段中的 onDestroy 方法设法解决了这个问题。
@Override
public void onDestroyView() {
super.onDestroyView();
if (this.mapFrag != null
&& getFragmentManager().findFragmentById(
this.mapFrag.getId()) != null) {
getFragmentManager().beginTransaction().remove(this.mapFrag)
.commit();
this.mapFrag = null;
}
}
回答by ArtKorchagin
I found another decision, try to use Fragments extending android.support.v4.app.Fragment
instead of android.app.Fragment
and use the android.app.FragmentTransaction
instead of android.support.v4.app.FragmentTransaction
我找到了另一个决定,尝试使用 Fragments 扩展android.support.v4.app.Fragment
代替android.app.Fragment
并使用android.app.FragmentTransaction
代替android.support.v4.app.FragmentTransaction
I found a solution in this post:
我在这篇文章中找到了解决方案:
Trying to remove fragment from view gives me NullPointerException on mNextAnim
回答by DexterLabs
just check whether getItem()
was returning null
for a Fragment
. if so set a default Fragment
!
只需检查是否getItem()
正在返回null
a Fragment
。如果是这样设置默认值Fragment
!