Application crashes when starting a new fragment - android

I'm new to Android dev, and my app crashes when I try to create a new fragment upon clicking my button that has an "onClick" that is called "EnterNameOnClick()"
I suspect that the following code from MainActivity might be the problem...
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void EnterNameOnClick(){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
EnterName_Fragment enterName_fragment = new EnterName_Fragment();
fragmentTransaction.add(android.R.id.content, enterName_fragment);
fragmentTransaction.commit();
}
}
public class EnterName_Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.new_user_fragment, container, false);
Spinner spinner = (Spinner) view.findViewById(R.id.movie_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this.getActivity(), R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
return view;
}
}
When I am writing the fragmentTransaction.add(android.R.id.content, enterName_fragment), I wonder if the "android.R.id.content" is erroneous? What am I supposed to fill this field with?

Maybe this is caused because using android:onclick in xml. Try onClick in Java using onClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void EnterNameOnClick(View v) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
EnterName_Fragment enterName_fragment = new EnterName_Fragment();
fragmentTransaction.add(android.R.id.content, enterName_fragment);
fragmentTransaction.commit();
}
});
Posting your logcat would help us to provide better solution. Try it and let me know.

Related

replacing fragment with another fragment with OnClick on a Listview

public class Fragment_01 extends ListFragment {
String string[]={"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5"};
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState) {
View myview = inflater.inflate(R.layout.fragment_fragment_01, container, false);
ListView listView= (ListView) myview.findViewById(android.R.id.list);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,string);
listView.setAdapter(array);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2, f3);
ft.commit();
}
});
return myview;
}
}
Do not change a fragment inside another. Have a frame layout n the activity. On click of an item in the list view, send the event to the activity through an interface and in the activity replace the fragment. This is the right way of replacing fragments.
Fragment_03 f3 = new Fragment_03();
FragmentManager fragmentManager = ((FragmentActivity) getContext()).getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new Fragment_03())
.commit();
//I am using this code without listView and this is working fine then why the other is not working ??
public class Fragment_01 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState){
View myview=inflater.inflate(R.layout.activity_fragment_01,container,false);
Button btn = (Button) myview.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm =getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2,f3);
ft.commit();
}
});
return myview;
}
}

Android relationships between fragments

I'm using some fragments programmatically in activity. There is one button in my first fragment and when i click to this button, it replaces to second fragment.My second fragment's background is 90% transparent, and when it starts, i can see button which is situated in first fragment, and it also works. I want to stop or do something, because i dont want to see first fragment features and use it.
First Fragment
public class RegistrationFirstFragment extends Fragment {
RegistrationSecondFragment rf;
ImageButton btnNewUser,btnNewAgent;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_first,container,false);
rf = new RegistrationSecondFragment();
btnNewUser = (ImageButton)v.findViewById(R.id.btnNewUserRegistrationFirstFragment);
btnNewAgent = (ImageButton)v.findViewById(R.id.btnNewAgentRegistrationFirstFragment);
btnNewUser.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "Transaction completed succesfully", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.flRegistrationFirst, rf);
ft.commit();
}
});
return v;
}
}
Second Fragment
public class RegistrationSecondFragment extends Fragment {
RegistrationFirstFragment rtl;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rtl = new RegistrationFirstFragment();
//return super.onCreateView(inflater, container, savedInstanceState);
View v =inflater.inflate(R.layout.fragment_registration_second,container,false);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// ft.replace(R.id.flRegistrationFirst, rf);
ft.remove(rtl);
ft.commit();
return v;
}
}
Main Activity
public class RegistrationActivity extends AppCompatActivity{
RegistrationFirstFragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
fr = new RegistrationFirstFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.flRegistrationFragment,fr);
ft.commit();
}
}
You can put
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
(the parameter fragment would be an instance of your second fragment)
into your onClick(View view){...} method to change the fragment instead of adding it.
Next time code for understanding your problem btw ;)
Add to fragment layout android:clickable="true". Int his way fragment will catch event so the click will not be caught by "main fragment".
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
Give android:clickable="true" for Second Fragment root layout parent, when ever fragment opens It catches the click event of root and ignored previous click events.
Second One: If u use replace Fragment it's better than add fragment.
getActivity().getSupportFragmentManager().beginTransaction().replace
(R.id.YOUR_CONTAINER, 'FragmentObject').addToBackStack("TAG").commitAllowingStateLoss();

Change fragment and navigation drawer state from fragment class

I created an application with navigation drawer using this tutorial: http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
And I have a button on the first fragment. So, I want to change fragment by clicking the button. This code can change fragment but not change navigation draver state (title, selected item):
public class FirstFragment extends Fragment {
public FirstFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_today, container, false);
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
ImageButton addButton = (ImageButton) getView().findViewById(R.id.add_button);
addButton.setOnClickListener(new addNewListener());
super.onActivityCreated(savedInstanceState);
}
private class addNewListener implements View.OnClickListener {
#Override
public void onClick(View v) {
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container, new NewFragment(), "NewFragmentTag");
ft.commit();
}
}
}
How can I fix it?
Thanks a lot!

Trouble refreshing android array adapter in fragment

I'm having trouble refreshing an array adapter. When I'm testing the program on a phone it works well and refreshes perfectly when switching through fragments. The problem comes when I'm using a tablet and both fragments are up at once.
Here's my fragment code:
public class HistoryFragment extends Fragment {
private ArrayList<String> history = new ArrayList<String>();
private ArrayAdapter<String> arrayAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle data = getArguments();
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.history, container, false);
ListView view = (ListView) v.findViewById(R.id.list);
if(data != null){
history = data.getStringArrayList("history");
}
arrayAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1, history);
view.setAdapter(arrayAdapter);
return v;
}
public void setHistory(ArrayList<String> history) {
history.addAll(history);
arrayAdapter.notifyDataSetChanged();
}
}
Here's my activity code:
public class MainActivity extends Activity implements historyClickListener{
public static ArrayList<String> history = new ArrayList<String>();
CalcFragment mCalcFragment = new CalcFragment();
HistoryFragment mHistFragment = new HistoryFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.container) != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, mCalcFragment);
fragmentTransaction.commit();
}
}
#Override
public void sendHistory(ArrayList<String> history){
Fragment hf = getFragmentManager().findFragmentById(R.id.historyfrag);
if(hf != null){
mHistFragment.setHistory(history);
}else{
Bundle data = new Bundle();
data.putStringArrayList("history", history);
mHistFragment.setArguments(data);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container, mHistFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
The sendHistory is an interface method in a calculator that help the calculator send the list of equations that have been done. Then in main activity if I'm using a tablet then all it does is setHistory which is a method in the history fragment. I believe the problem is that I'm not using arrayAdapter.notifyDataSetChanged() in the history fragment correctly. Can someone please help. Thanks!
You are setting the new data to a variable of the fragment not to the adapter who is the one who shows the data.
So you can:
Create a new ArrayAdapter with the new data and set it to the listview.
Extend ArrayAdapter, create a method that pass the new Data to the Adapter and then call notifyDataSetChanged().
this.history.addAll(history)? I see the same variable name for parameter and member variable.

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