[英]Attempt to refresh fragment gives me NullPointerException; I've made sure compatibility isn't an issue, and my fragments shouldn't be null
I am currently trying to implement code in my android project where my fragments will refresh after a button is pressed, recreating the view and displaying different items. 我目前正在尝试在我的android项目中实现代码,在该项目中,按下按钮后片段会刷新,重新创建视图并显示不同的项目。 I made sure there were no compatibility issues between android.app and android.support.v4.app, and am positive that I have my fragments' id's labelled correctly.
我确保android.app和android.support.v4.app之间没有兼容性问题,并且肯定我的片段ID已正确标记。
Here is the exact error I received when running my app on an emulator: 这是在模拟器上运行应用程序时收到的确切错误:
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:722)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
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:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
And here is the java code that is causing the issue in its class: 这是导致此类问题的Java代码:
package edu.sbu.cs.android.NMR.core;
import edu.sbu.cs.android.NMR.core.ImageAdapter;
import edu.sbu.cs.android.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class HomeFragment extends Fragment {
public interface OnSelectedListener {
public void onSelected();
}
OnSelectedListener mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnArticleSelectedListener");
}
}
GridView gridview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
gridview = (GridView)rootView.findViewById(R.id.gridView1);
gridview.setAdapter(new ImageAdapter(gridview.getContext()));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
((MainActivity) getActivity()).setProblem(position);
switch (position) {
case 0:
SpectraFragment.w.loadUrl("file:///android_asset/nmr0.html");
getArguments().putString("problem", "0");
FragmentManager fragMan;
fragMan = getFragmentManager();
Fragment frg= fragMan.findFragmentById(R.layout.fragment_spectra);
Fragment frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
final FragmentTransaction ft = fragMan.beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.detach(frg2);
ft.attach(frg2);
ft.commit();
case 1:
SpectraFragment.w.loadUrl("file:///android_asset/nmr1.html");
getArguments().putString("problem", "1");
fragMan = getFragmentManager();
frg = fragMan.findFragmentById(R.layout.fragment_spectra);
frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
final FragmentTransaction ft2 = fragMan.beginTransaction();
ft2.detach(frg);
ft2.attach(frg);
ft2.detach(frg2);
ft2.attach(frg2);
ft2.commit();
case 2:
SpectraFragment.w.loadUrl("file:///android_asset/nmr2.html");
getArguments().putString("problem", "2");
fragMan = getFragmentManager();
frg = fragMan.findFragmentById(R.layout.fragment_spectra);
frg2 = fragMan.findFragmentById(R.layout.fragment_questions);
final FragmentTransaction ft3 = fragMan.beginTransaction();
ft3.detach(frg);
ft3.attach(frg);
ft3.detach(frg2);
ft3.attach(frg2);
ft3.commit();
default:
SpectraFragment.w.loadUrl("file:///android_asset/nmr0.html");
}
Toast.makeText(getActivity(), " " + position, Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Has anyone every had this issue before where the problem wasn't due to compatibility between android.support.v4.app and android.app, or am I missing a point in my code where I am making a null object reference? 在不是由于android.support.v4.app和android.app之间的兼容性而导致的问题之前,是否每个人都有这个问题,或者我在代码中缺少要进行空对象引用的地方吗? I've searched these forums for weeks and have tried every solution I can think of.
我在这些论坛上搜索了数周,并尝试了我能想到的所有解决方案。 I've even switched out ActionBar tabs in my MainActivity for a material design SlidingTabLayout to get rid of the compatibility issue, but it has done nothing.
我什至在MainActivity中将ActionBar选项卡切换为材质设计SlidingTabLayout来摆脱兼容性问题,但是它什么也没做。 I'm frustrated as to why my code won't, and although I've been working with android for a year, there are still things that I don't quite understand although I've made a different app that works fine.
我为为什么我的代码无法使用而感到沮丧,尽管我已经使用android一年了,但是尽管我开发了一款可以正常工作的应用程序,但仍然有些事情我不太了解。 This one is a bit more complex and was passed to me to work on, so I am still figuring out how some of it works.
这一点比较复杂,已传递给我进行工作,因此我仍在弄清楚其中的一些工作原理。 I'm sorry to have gone off on a rant, but I've been stuck on this issue for a while and seem to be at a dead end.
对不起,我感到很抱歉,但是我在这个问题上停留了一段时间,似乎陷入了死胡同。 Anyone have any solutions?
有人有解决方案吗?
Also, I'm using android studio to develop my app if that makes any difference in solving the issue, but I had been previously using eclipse and had moved the project over. 另外,如果这对解决问题没有任何帮助,那么我正在使用android studio开发我的应用程序,但是我以前一直在使用eclipse并将项目移开。
The problem is that you are using android.support.v4.app.FragmentTransaction
on Fragments that are likely extending android.app.Fragment
问题是,你正在使用
android.support.v4.app.FragmentTransaction
上有可能延长片段android.app.Fragment
You have to use android.app.FragmentTransaction
with android.app.Fragment
and you have to use android.support.v4.app.FragmentTransaction
to make transactions involving android.app.v4.app.Fragment
您必须将
android.app.FragmentTransaction
与android.app.Fragment
一起使用,并且必须使用android.support.v4.app.FragmentTransaction
进行涉及android.app.v4.app.Fragment
交易
You can't intermix the two. 您不能将两者混在一起。 The way to fix it would simply be to change the offending Fragments to extend
android.support.v4.app.Fragment
instead. 修复它的方法只是更改有问题的Fragments来扩展
android.support.v4.app.Fragment
。
Update: In the switch
statement, you are missing the break;
更新:在
switch
语句中,您缺少break;
statements on your cases. 您案件的陈述。
A glance at line 722 of the source code for BackStackRecord
suggests you called FragmentTransaction.detach()
with a null fragment. BackStackRecord
看一下BackStackRecord
的源代码第722行,建议您使用空片段调用FragmentTransaction.detach()
。 Probably one of your calls to findFragmentById()
is returning null. 您对
findFragmentById()
调用之一可能返回null。 To find out which one, check the result of each call and log a debug message if it is null. 要找出哪一个,请检查每个调用的结果并记录调试消息(如果为空)。
该错误清楚地告诉您它是对空对象引用起作用的,这意味着该片段管理器中没有具有相应ID的片段。请尝试对我有用的getChildFragmentManager()或getParentFragment()。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.