编辑: 这里的问题不是NULLPOINTER EXPECTION,问题是当我尝试将int发送到位于我的一个片段内的方法时,它会崩溃。
我一直在寻找解决方案,但我还没找到一个,这就是为什么我创建了一个帐户并发布了。请不要将其标记为重复(除非您可以找到问题的解决方案,以解释为什么我无法调用frag.changeText(id);对于这种情况)。它一直在崩溃,我仍然无法弄清楚为什么它不能调用这种方法。
这是我的主要片段代码(在主要活动中):
public class MainFragment extends Fragment {
private static final int REQUEST_CODE_DETAILS_ACTIVITY = 1234;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
//return inflater.inflate(R.layout.fragment_screen2, container, false);
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
Button b1 = (Button) getActivity().findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onClickMethod(R.id.button1);
}
});
// Button b2 = (Button) getActivity().findViewById((R.id.button2);
//b2.setOnClickListener(new View.);
}
public void onClickMethod(int id) {
// int id = R.id.button1;
Toast.makeText(getActivity(), "does the id get passed?" +id , Toast.LENGTH_SHORT).show();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
int testint = 323;
FragScreen2 frag = (com.example.engg6600.fragtest2.FragScreen2) getFragmentManager().findFragmentById(R.id.fragment_screen2);
//frag.changeText(id);
try {
((FragScreen2)getFragmentManager().findFragmentById(R.id.fs2)).testMethod(testint);
} catch (Exception e) {
Toast.makeText(getActivity(), "breaks" + testint, Toast.LENGTH_SHORT).show();
}
//Intent goS2 = new Intent(getActivity(), Screen2.class);
//goS2.putExtra("buttonNumber", id);
// FragScreen2 frag = (FragScreen2) getFragmentManager().findFragmentById(R.id.fragment_screen2);
// Toast.makeText(getActivity(), "before to frag.changetext(id)", Toast.LENGTH_LONG).show();
// frag.changeText(id);
// Toast.makeText(getActivity(), "sent to frag.changetext(id)", Toast.LENGTH_LONG).show();
} else{
// String toastString = Integer.toString(id);
Toast.makeText(getActivity(), "portrait mode id" +id, Toast.LENGTH_LONG).show();
Intent goS2 = new Intent(getActivity(), Screen2.class);
goS2.putExtra("buttonNumber", id);
startActivityForResult(goS2, REQUEST_CODE_DETAILS_ACTIVITY);
}
}
}
FragScreen2片段代码:
public class FragScreen2 extends Fragment {
private static final String[] SCREENTEXT ={
"You have selected button 1",
"You have selected button 2",
"You have selected button 3",
"You have selected button 4",
"No selection"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_screen2, container, false);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// pull out the country ID to show from the activity's intent
Intent intent = getActivity().getIntent();
int id = intent.getIntExtra("buttonNumber", 234324);
Toast.makeText(getActivity(), "fragscreen 2 id=" +id, Toast.LENGTH_LONG).show();
changeText(id);
}
public void changeText(int id) {
int buttonNumber=4;
Toast.makeText(getActivity(), "button1 of R.id" + Integer.toString(R.id.button1), Toast.LENGTH_SHORT).show();
if (id==R.id.button1) {
buttonNumber =0;
} else if (id==R.id.button2) {
buttonNumber =1;
} else if (id==R.id.button3) {
buttonNumber =2;
} else if (id==R.id.button4) {
buttonNumber =3;
}
String text = SCREENTEXT[buttonNumber];
TextView tv = (TextView)getActivity().findViewById(R.id.textscreen2);
tv.setText(text);
}
public void testMethod (int integerVar) {
Toast.makeText(getActivity(), "testmethod int=" + integerVar, Toast.LENGTH_SHORT).show();
}
}
并且我一直收到此错误(在我添加try catch方法之前):
10-21 16:04:07.263 25153-25153 / com.example.engg6600.fragtest2 W / dalvikvm:threadid = 1:线程退出时未捕获异常(group = 0x419a9da0) 10-21 16:04:07.263 25153-25153 / com.example.engg6600.fragtest2 E / AndroidRuntime:FATAL EXCEPTION:main 处理:com.example.engg6600.fragtest2,PID:25153 显示java.lang.NullPointerException 在com.example.engg6600.fragtest2.MainFragment $ override.onClickMethod(MainFragment.java:48) at com.example.engg6600.fragtest2.MainFragment $ override.access $ dispatch(MainFragment.java) 在com.example.engg6600.fragtest2.MainFragment.onClickMethod(MainFragment.java:0) at com.example.engg6600.fragtest2.MainFragment $ 1.onClick(MainFragment.java:33) 在android.view.View.performClick(View.java:4654) 在android.view.View $ PerformClick.run(View.java:19438) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:146) 在android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1283) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 在dalvik.system.NativeStart.main(本地方法)
我的问题是:为什么我不能调用位于我的其他片段中的函数?或者将int值传递给它?