i have an error in my rootView - android

#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return inflater.inflate(R.layout.fragment_currentmovie, container, false);
}
}
it is displaying rootView cannot be resolved,i need to add edit text et to string str so that using bundle i can pass it to the next fragment

Try this Add inflater then return.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
et=(EditText)rootView.findViewById(R.id.et1);
String str=et.getText().toString();
Fragment frag=new Fragment();
Bundle bundle1=new Bundle();
bundle1.putString("name", str);
frag.setArguments(bundle1);
getFragmentManager().beginTransaction().add(R.id.container_body, frag).commit();
return rootView ;
}
}

You need to inflate it first:
View rootView = inflater.inflate(R.layout.fragment_currentmovie, container, false);
...
return rootview;

Related

Passing Data from acitvity to fragment android studio

--This is my Fragment :--
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
//and This is main acivity that constarin framelayout :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_save, container, false);
savedAlamat = view.findViewById(R.id.saved_alamat);
savedContac = view.findViewById(R.id.saved_kontak);
savedName = view.findViewById(R.id.saved_nama);
String malamat = getArguments().getString("mAlamat");
String mcontac = getArguments().getString("mContac");
String mname = getArguments().getString("mname");
savedAlamat.setText(malamat);
savedContac.setText(mcontac);
savedName.setText(mname);
return view;
}
--I don't understand why I keep get NotNULLobject, i want to make a passing data between that activity and fragment.
In Activity, write this code
Bundle bundle = new Bundle();
bundle.putString("mAlamat", "Data to send"); // set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
In Fragment class, receive data like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("mAlamat");
return inflater.inflate(R.layout.fragment, container, false);
}
Courtesy

How use a button inside a fragment?

I'm trying to use 4 fragments
[F1]
[F2][F3][F4]
F1 will have 3 buttons to change between F2,F3 and F4 but i donĀ“t know how to use the buttons properties like in a normal Activity
That's what i have in my F1 code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_01, container, false);
}
Check this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_01, container, false);
// here you have the reference of your button
Button yourButton = (Button)view.findViewById(R.id.your_button_id);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_01, container, false);
Button firstButton = (Button)view.findViewById(R.id.firstbutton);
Button seconButton = (Button)view.findViewById(R.id.seconbutton);
Button thirdButton = (Button)view.findViewById(R.id.thirdbutton);
return view;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_one, container, false);
Button button = (Button) rootView.findViewById(R.id.buttonSayHi);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
onButtonClicked(v);
}
});
return rootView;
}
public void onButtonClicked(View view)
{
//do your stuff here..
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frameLayoutFragmentContainer, new FragmentTwo(), "NewFragmentTag");
ft.commit();
ft.addToBackStack(null);
}
}
It's a simple way to reference a button or any widget in a fragment.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_fragment_one, container, false);
btnOne = view.findViewById(R.id.btnOne);
return view;
You should get the view first, and then get the button from the view
like this:
View rootView = inflater.inflate(R.layout.fragment_01, container, false);
Button btn = (Button) rootView.findViewById(R.id.btn);
You should do some tutoriasl first, its not that hard.

Passing data from Fragment to Activity?

In my application,i have a Fragment class for Facebook login.I want to get this inflated layout from class in my Activity class so that i can display the facebook data in my SideMenu.How to get the layout in Activity class ?
This is my onCreate method
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.login_notifications, container, false);
login();
return ll;
}
This is the code for showing layout in fragment:-
private static View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.location_layout, container,false);
return rootView;
}

Inflating a layout in a fragment

So I am finally learning Fragments after learning Activities. I am looking at a video tutorial. All of a sudden he changed the way a Fragment inflates a layout. Why does he have two different ways? The first way in FragmentA is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a, container, false);
}
Then the other way in FragmentB is:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
if (savedInstanceState == null)
{
}
else {
savedInstanceState.getString("text");
TextView myText = (TextView) view.findViewById(R.id.textView);
myText.setText(data);
}
return view;
}
So why did it change from return inflater.inflate to View view = inflater.inflate
Sorry for dumb question, but I can't find this answer on Google.
It's basically the same,
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
has to return a View.
And
inflater.inflate(R.layout.fragment_b, container, false)
returns a View as well.
In the second example he assigns the return value of inflater.inflate() to a View object, as he needs to loads some data and set it on this View object... then he returns it.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.lstindicators, container,
false);
And return your view thats all! :D
jejeje... and if you want to change your current fragment for another chechk this:
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();

How to transfer value from one fragment to another fragment

Example
I do not understand this example.
How to apply in my program
I try to write In my program
General use that I can.
But replaced fragment.
I do not know how to use.
Pass the value of the object.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune, container, false);
FragmentManager DS2 = getFragmentManager();
FragmentTransaction DSE2 = DS2.beginTransaction();
Fragment DF2 = DS2.findFragmentById(R.id.frameLayout4);
if (DF2 == null) {
String title = "Fragment A";
templistview2 usermune = new templistview2(title);
DSE2.add(R.id.frameLayout4, usermune);
DSE2.addToBackStack(null);
DSE2.commit();
/////////////////////////////////////////////////////////
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
////////////////////////////////////////////////
}
The pick value the object
templistview2.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.templistmune2, container, false);
////////////////////////////////////////////////////////////////////////////////////
Bundle bundle = this.getArguments();
String myST = bundle.getString("ST", SAS);
///////////////////Error SAS cannot be resolved to a variable /////////////////
return view;
}
You're only adding adding the Bundle when DF2 is null... so your program will either crash with a NullPointerException somewhere along the line or run but not have the Bundle passed.
So place
Bundle bundle = new Bundle();
String SAS="50";
bundle.putString("ST", SAS);
DF2.setArguments(bundle);
outside the if structure.

Categories

Resources