Java 调用接口方法时的NullPointerException(片段通信)
我的应用程序与一个TabLayout(以及一个ViewPager)一起工作 我有一个片段a和一个片段B。我的目标是从片段a的EditText中恢复数据,显然,在片段B中打印并使用它们 为此,我使用了一个名为Java 调用接口方法时的NullPointerException(片段通信),java,android,android-fragments,interface,Java,Android,Android Fragments,Interface,我的应用程序与一个TabLayout(以及一个ViewPager)一起工作 我有一个片段a和一个片段B。我的目标是从片段a的EditText中恢复数据,显然,在片段B中打印并使用它们 为此,我使用了一个名为ButtonCliked的界面,但似乎效果不佳。我忘了什么吗 TabFragment.java(用于管理我的片段) 片段A:EquationsCalculFragment.java public class EquationsCalculFragment extends Fragment {
ButtonCliked
的界面,但似乎效果不佳。我忘了什么吗
TabFragment.java(用于管理我的片段)
片段A:EquationsCalculFragment.java
public class EquationsCalculFragment extends Fragment {
private String aS;
private String bS;
private EquationsCalculFragment.ButtonClicked callEqua2ndRes;
public interface ButtonClicked {
void sendResultats(float a, float b);
}
private ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_equations_calcul, container, false);
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
final EditText champ_a = (EditText) view.findViewById(R.id.equa_2nd_a);
final EditText champ_b = (EditText) view.findViewById(R.id.equa_2nd_b);
Button button = (Button) view.findViewById(R.id.btn_equations_resultats);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bS = champ_b.getText().toString();
aS = champ_a.getText().toString();
callEqua2ndRes.sendResultats(Float.parseFloat(aS), Float.parseFloat(bS));
viewPager.setCurrentItem(1, true);
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
if (context instanceof EquationsCalculFragment.ButtonClicked) {
callEqua2ndRes = (EquationsCalculFragment.ButtonClicked) context;
}
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement ButtonClicked");
}
}
@Override
public void onDetach() {
callEqua2ndRes = null;
super.onDetach();
}
}
public class EquationsResultatFragment extends Fragment {
private TextView results;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_equations_resultat, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
results = (TextView) getView().findViewById(R.id.equa_2nd_resultat);
}
public void getResultat(float a, float b) {
results.setText(String.valueOf((a*a)+(2*a*b)+(b*b)));
}
}
片段B:equalationsResultatFragment.java
public class EquationsCalculFragment extends Fragment {
private String aS;
private String bS;
private EquationsCalculFragment.ButtonClicked callEqua2ndRes;
public interface ButtonClicked {
void sendResultats(float a, float b);
}
private ViewPager viewPager;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_equations_calcul, container, false);
viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
final EditText champ_a = (EditText) view.findViewById(R.id.equa_2nd_a);
final EditText champ_b = (EditText) view.findViewById(R.id.equa_2nd_b);
Button button = (Button) view.findViewById(R.id.btn_equations_resultats);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bS = champ_b.getText().toString();
aS = champ_a.getText().toString();
callEqua2ndRes.sendResultats(Float.parseFloat(aS), Float.parseFloat(bS));
viewPager.setCurrentItem(1, true);
}
});
return view;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
if (context instanceof EquationsCalculFragment.ButtonClicked) {
callEqua2ndRes = (EquationsCalculFragment.ButtonClicked) context;
}
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement ButtonClicked");
}
}
@Override
public void onDetach() {
callEqua2ndRes = null;
super.onDetach();
}
}
public class EquationsResultatFragment extends Fragment {
private TextView results;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_equations_resultat, container, false);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
results = (TextView) getView().findViewById(R.id.equa_2nd_resultat);
}
public void getResultat(float a, float b) {
results.setText(String.valueOf((a*a)+(2*a*b)+(b*b)));
}
}
有什么我做错了吗?问题是您在EquationsCalculFragment中声明了一个接口,但从未初始化它:) 下面是您可以做的:) 在EquationsCalculFragment中,更改
private EquationsCalculFragment.ButtonClicked callEqua2ndRes;
到
在TabFragment的getItem方法案例0中,进行以下更改:)
应该能解决你的问题:)希望我的回答有帮助:)
p.S:上面提供的代码是为了解释这个概念,而不是为了复制粘贴,可能包含语法错误:)如果包含,请修改并使用它:D我认为NPE是因为您试图找到带有id的片段,但您提供了我猜是ViewPager的id?尝试按标记查找,它应该如下所示:
int position;
getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.yourViewPagerIdGoesHere + ":" + position);
位置是目标片段的位置,就像适配器的
public fragment getItem(int position)
method一样,请只添加有效的问题和数据,以便易于理解和回答。不确定,但是我认为您将不正确的位置
参数传递给了片段getItem(int位置)
或getPageTitle()
方法。您在那里返回空值。这不是一个好的做法。如果提供的参数不正确,最好抛出新的IllegalArgumentException()
@Exaqt抱歉,这无助于解决我的问题。@A-DroidTech如果我删除了某些内容,我想这会更难理解understand@slabre:由于get item是MyAdapter的一个方法,而TabFragment实现了接口,而不是MyAdapter:),所以出现了不兼容的错误:),所以只需说calculationFragment.callEqua2ndRes=TabFragment.this;这就是全部,伙计:)请检查我的更新答案:)请在你否决投票时留下评论:)有助于理解我的错误:我已经做出了改变。在我的案例0中,在calculationFragment.callequal2ndres=this行中,我只是有一个类型不兼容代码>。Android Studio告诉我:必需:EquationsCalculFragment.ButtonClicked并找到:TabFragment.MyAdapter。我不明白why@Salbre:那太费事了:让我再看一眼:)给我一分钟:)@Salbre:你能用我更新的答案检查一下吗:)不幸的是,这会产生其他空点异常。类似于fragRes.getResultat(a,b)代码>在TabFragment.java的开头
int position;
getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.yourViewPagerIdGoesHere + ":" + position);