Crashes fragement when passing arguments to previous fragment - android

I have three fragments FragA, FragB, FragC. Am passing an argument through bundle "discount" from FragA to FragB. Then from FragB am sending two arguments "discount" and "address" to FragC. Till here all are working properly.
Then i have to go back to FragB from FragC. I used a button 'backButton' for this purpose. But when the button clicks, my app crashes due to NPE. I have send argument to FragB from FragC, but no arguments will get in FragB.
Am attaching the code i have used below..
Fragement A
class FragA extends Fragment
{
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragfirst, container, false);
nextButton = (Button) v.findViewById(R.id.buttonnext);
nextButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
fr = new FragB();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
Fragement B
class FragB extends Fragment
{
Context context;
String discount = "";
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragsec, container, false);
String addressid = "something";
nextButton = (Button) v.findViewById(R.id.buttonnext);
Bundle b = getArguments();
discount = b.getString("discount");
nextButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
fr = new FragC();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
bundle.putString("address", addressid);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
Fragement C
class FragC extends Fragment
{
Context context;
String discount = "",addressid="";
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
{
context = getActivity();
View v = inflater.inflate(R.layout.layout_fragthird, container, false);
backButton = (Button) v.findViewById(R.id.buttonnext);
Bundle b = getArguments();
discount = b.getString("discount");
//backButton to go to Fragment B(FragB)
backButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
fr = new FragB();
Bundle bundle=new Bundle();
bundle.putString("discount", discount);
fr.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
}
LogCat
java.lang.NullPointerException
at com.abc.fragments.FragA.onCreateView(FragA.java:114)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
at android.app.BackStackRecord.run(BackStackRecord.java:635)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Please Suggest me a solution...

the problem might be NullPointerException....
as you are extracting arguments in onCreateView but when you are going back getArguments must be giving you null a there is no argument passed from FragC to FragB... it better you put null check around bundle
Bundle b = getArguments();
if( b != null )
discount = b.getString("discount");
when you press back button you are restoring your pervious fragment... but in you code you are going Frag A >Frag B >Frag C >Frag B ....you are implementing it wrong way... just dont add Frag B to stack of fragment

Related

How to transfer from one fragment to another in Android

I want to transfer some data like "editText1Double" and "sum" to another fragment (SecondFragment) that will use those data. How would I do that?
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_fragment, container, false);
editText1 = (EditText) v.findViewById(R.id.editText1);
editText2 = (EditText) v.findViewById(R.id.editText2);
editText3 = (EditText) v.findViewById(R.id.editText3);
textView = (TextView) v.findViewById(R.id.textView);
calculateButton = (Button) v.findViewById(R.id.calculateButton);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1Double = Double.parseDouble(editText1.getText().toString());
editText2Double = Double.parseDouble(editText2.getText().toString());
editText3Double = Double.parseDouble(editText3.getText().toString());
sum = editText1Double + editText2Double + editText3Double;
}
});
1) Using Bundle :
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
and Receive as
Bundle bundle = getArguments();
Double sum = bundle.getDouble("key");
2)Using Construcor:
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B(sum);//receiving fragment
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
and Receive as
public class Fragment_B extends Fragment{
private double sum;
public Fragment_B (double sum) {
this.sum= sum;
}
public Fragment_B () {
}}
Pass data by using
fragment = new HomeFragment();
Bundle bundle = new Bundle();
bundle.putDouble("double", 1.1);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment, "HomeFragment").addToBackStack(null).commit();
And get data in another Fragment using
Bundle bundle = getArguments();
double d = bundle.getDouble("double")
Hope it works fine for you.
Sending data from fragment_A
FragmentManager fM = getActivity().getSupportFragmentManager();
FragmentTransaction fT = fM.beginTransaction();
Fragment_B fragment = new Fragment_B();//receiving fragment
Bundle bundle = new Bundle();
bundle.putDouble("sum_key", sum);
fragment.setArguments(bundle);
fT.add(R.id.container, fragment);
fT.addToBackStack(null);
fT.commit();
Receiving data at fragment_B
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
Double sum = bundle.getDouble("sum_key");
}

Passing data by using bundle between tablayout

I am using Bundle to passing data from my First TabFragment but it prompts out with the NullPointerException. Error is occur when getArguments() in list_fragments2 in second tab
MainActivity Fragment
list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
Toast.makeText(this, "" + b, Toast.LENGTH_SHORT).show();
SecondActivity Fragment
public class list_fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View h = inflater.inflate(R.layout.tab2, container, false);
TextView textView = (TextView) h.findViewById(R.id.textView);
Bundle bundle=getArguments();
//your string
if(bundle != null) {
String test = bundle.getString("test");
textView.setText(test);
}
return h;
}
}
Do you actually load your second fragment?
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
list_fragment2 fragment = new list_fragment2();
Bundle b = new Bundle();
b.putString("test","text");
fragment.setArguments(b);
fragmentTransaction.replace(placeholder, fragment);
fragmentTransaction.commit();
I think you can create your instance of list_fragment2 code within list_fragment2. Maybe your codes recreated list_fragment2 many times.
public static list_fragment2 createInstance() {
list_fragment2 fragment = new list_fragment2();
Bundle bundle = new Bundle();
bundle .putString("test","text");
fragment.setArguments(bundle);
return fragment;
}

Getting NullPointerException while passing string between two Fragments [duplicate]

This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 6 years ago.
I have Two Dynamic Fragments associated with one activity, I am trying to pass one Text from First Fragment to Second Fragment using Bundle, but I am getting Null Pointer Exception. Is it the right way to pass String between two fragments? Below is my code :
First Fragment
public class FirstFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_main, container, false);
String text = "GetThisStringInSecondFragment";
TextView txtView = null;
txtView = (TextView) view.findViewById(R.id.firstfragmenttext);
txtView.setText(text);
Bundle bundle = new Bundle();
bundle.putString("HI", text);
return view;
}
}
Second Fragment
public class SecondFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.content_secondmain,
container, false);
TextView txtView = null;
txtView = (TextView) view.findViewById(R.id.secondfragmenttext);
Bundle bundle = this.getArguments();
String myInt = bundle.getString("HI");
txtView.setText(myInt);
return view;
}
}
Activity
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnLoad = (Button) findViewById(R.id.btn_load);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FirstFragment hello = new FirstFragment();
fragmentTransaction.add(R.id.fragment_container, hello, "HELLO");
fragmentTransaction.commit();
FragmentManager fragmentManager2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
SecondFragment hello2 = new SecondFragment();
fragmentTransaction2.add(R.id.fragment_container, hello2, "HELLO");
fragmentTransaction2.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
You are doing getArguments in second fragment but where are you sending those arguments from first fragment. You can use Interface here and define it in activity. From activity, you can use setArgument() method for second fragment.
check the following link for more detailed information and steps to do that.
https://developer.android.com/training/basics/fragments/communicating.html

How to Do Transaction of framents from one to another

I want to transact from one fragment to another... below given is my snippet...
I want transaction on button click...
public class Main extends Fragment {
// final View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
Button camera=(Button)rootView.findViewById(R.id.button1);
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.layout.main, new CameraActivity());
ft.commit();
}
});
return rootView;
}
so please Assist me in solving this...
[1]<--->[2]<--->[3]<--->[4]<--->[5]
where []=fragments...
[1]- has buttons a,b,c,d,e
How to move from 1 to 3 OnClick of c ...
Here is the Camera Activity
public class CameraActivity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.camera, container, false);
return rootView;
}
}
first of all this cant be activity
ft.replace(R.layout.main, new CameraActivity());
it has to be fragment
put something like this in your onclick listener in FirstFragment:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
SecondFragment fragment = new SecondFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
than in your SecondFragment onclick listener:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
ThirdFragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
and so on ...
method addToBackStack(null) is providing you back as simple as that. You could also pass some tag in that method if you need it, but I think from question you want be needing it.
Also if you need to start Activity than dont use this method, use Intent instead.
hope it helps

Android Fragments Compatibility Issue

I have main activity of fragment
public class MainActivity extends Activity implements OnClickListener {
Fragment fragment;
Button btn1, btn2,btn3;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn2 = (Button)findViewById(R.id.button2);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MyFragment myfragment = new MyFragment();
ft.add(R.id.myFragment, myfragment);
ft.commit();
}
#Override
public void onClick(View v){
Fragment fragment = null;
if(v == btn1){
fragment = new Fragment1();
}else if(v == btn2){
fragment = new Fragment2();
}
FragmentTransaction transcation = getFragmentManager().beginTransaction();
transcation.replace(R.id.myFragment,fragment);
transcation.addToBackStack(null);
transcation.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
transcation.commit();
}
}
and three simple fragments similar like this
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate( R.layout.fragment2, container, false);
}
}
Its working fine with HC and ICS but if i try to run on older version 2.3. It crashes.... I have added supported library also but does not work. and one more question... Kindly suggest me good tutorial or video which helps to move from normal development to fragments.

Categories

Resources