EDIT: THE ISSUE HERE IS NOT THE NULLPOINTER EXPECTION, the issue is that when I'm trying to send an int to a method located inside one of my fragments, it CRASHES.
I have been searching everywhere for a solution, but I have not yet found one, which is why I created an account and posted. Please do not mark this as duplicate (unless you can find a solution to the issue as to why I am not able to call frag.changeText(id); for this situation). It keeps crashing and I still can't figure out why it cannot call this method.
Here is my Main Fragment Code (inside Main Activity):
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 Fragment Code:
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();
}
}
and I keep getting this error (BEFORE I added the try catch method):
10-21 16:04:07.263 25153-25153/com.example.engg6600.fragtest2 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x419a9da0) 10-21 16:04:07.263 25153-25153/com.example.engg6600.fragtest2 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.engg6600.fragtest2, PID: 25153 java.lang.NullPointerException at com.example.engg6600.fragtest2.MainFragment$override.onClickMethod(MainFragment.java:48) at com.example.engg6600.fragtest2.MainFragment$override.access$dispatch(MainFragment.java) at com.example.engg6600.fragtest2.MainFragment.onClickMethod(MainFragment.java:0) at com.example.engg6600.fragtest2.MainFragment$1.onClick(MainFragment.java:33) at android.view.View.performClick(View.java:4654) at android.view.View$PerformClick.run(View.java:19438) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:146) at android.app.ActivityThread.main(ActivityThread.java:5602) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) at dalvik.system.NativeStart.main(Native Method)
My question is: why can't I call the function located in my other fragment? or pass the int value to it?