I'm trying to open and populate a navigation drawer with a separate fragment by clicking on a button. The drawers themselves are only empty RelativeLayouts declared in activity_main.xml.
activity_main.xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Main View-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="xxx.MainActivity">
</RelativeLayout>
<!--Left drawer-->
<RelativeLayout android:id="#+id/drawer_left"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="start">
</RelativeLayout>
<!--Right drawer-->
<RelativeLayout android:id="#+id/drawer_right"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end">
</RelativeLayout>
The main_view gets populated with a fragment in MainActivity, so it will look like this:
MainActivity class:
public class MainActivity extends AppCompatActivity {
android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
//Set fragment
Frag1 frag1 = new Frag1();
fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_container,frag1).commit();
}
Frag1 class:
public class Frag1 extends Fragment {
ImageButton imageButtonOpenDrawer;
DrawerLayout mDrawerLayout;
RelativeLayout mDrawer_left;
android.app.FragmentManager fragmentManager;
android.app.FragmentTransaction fragmentTransaction;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.frag1, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View v = getView();
imageButtonOpenDrawer = (ImageButton) v.findViewById(R.id.imageButtonOpenDrawer);
fragmentManager = getFragmentManager();
mDrawerLayout = (DrawerLayout) v.findViewById(R.id.drawer_layout);
mDrawer_left = (RelativeLayout) v.findViewById(R.id.drawer_left);
imageButtonOpenDrawer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DrawerFragment drawerFragment = new DrawerFragment();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragment_container,drawerFragment).commit();
}
});
}
The same principle works when done from MainActivity class. But when I try to open the drawer from the fragments class, this null pointer error gets thrown:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference
Try to check returns of getView().
I think that frag1's root view is in R.layout.frag1 and it doesn't have drawer_layout and drawer_left.
Related
Theoretically, lets say i have two classes with corresponding XML files - Activity and Frag
Activity.java
public class Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment fragment = new Frag();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragmentArea, fragment);
ft.commit();
}
}
Frag.java
public class Frag extends Fragment {
private TextView txtView;
public Frag() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_, container, false);
txtView = view.findViewById(R.id.textView);
return view;
}
public void setTextView(String str) {
txtView.setText(str);
}
}
activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eksamen.chris.eksempelfragment.Activity">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/fragmentArea">
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Fragment xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.eksamen.chris.eksempelfragment.Frag">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Fragment"
android:id="#+id/textView"/>
</FrameLayout>
As you can see, i have the method setTextView() in my Frag class. Obviously enough, what i would like to do is to call that method from my Activity class.
I have tried grasping this for some hours now, and i really cannot figure out a way to do this. Every example/tutorial out there makes my app crash.
Could someone explain to me how this works, taken the example we have in hand into consideration? Or maybe there is a better way to provide the fragment with data from my activity?
I'd be grateful!
Edit: awful indentation, had some issues, trying to fix.
In order to pass data from Activity to Fragment , you have to use :
Bundle
which is a KEY VALUE data structure for passing values between fragments and activity so at your onCreate() in Activity class will be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str = "your_string_here";
// 1 -
Bundle bundle = new Bundle();
// 2 -
bundle.putString("KEY", str);
Fragment fragment = new Frag();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
// 3 -
fragment.setArguments(bundle);
ft.replace(R.id.fragmentArea, fragment);
ft.commit(); }
and at your fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_, container, false);
txtView = view.findViewById(R.id.textView);
// 4 - NOTE : Should hould the same key at the Activity class :
String str = getArguments().getString("KEY");
setTextView(str);
return view;
}
In my Android Studio project I want to implement into the MainActivity a NavigationDrawer with fragments and on every fragments a bottom navigation view.
This is the code of one fragment:
public class U16 extends Fragment{
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("TITOLO U16");
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
switch (item.getItemId()) {
case R.id.navigation_home:
fragmentTransaction.replace(R.id.content, new Calendario()).commit();
return true;
case R.id.navigation_dashboard:
fragmentTransaction.replace(R.id.content, new Palestre()).commit();
return true;
case R.id.navigation_notifications:
fragmentTransaction.replace(R.id.content, new Squadra()).commit();
return true;
}
return false;
}
};
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
BottomNavigationView navigation = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, new Calendario()).commit();
return inflater.inflate(R.layout.u16_layout, container, false);
}
}
I get this error:
FATAL EXCEPTION: main
Process: app.navigationdrawerfragment, PID: 21499
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.BottomNavigationView.setOnNavigationItemSelectedListener(android.support.design.widget.BottomNavigationView$OnNavigationItemSelectedListener)' on a null object reference at app.navigationdrawerfragment.U16.onCreateView(U16.java:52)
I know that this error in into the procedure "OnCreateView" doing " navigation.setOnNavigationItemSelectedListener..."
This is the u16_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="simone.biliato.navigationdrawerfragment.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
</LinearLayout>
So, anyone tried to do the same?
You are trying do set someone that is isn't already created.
So move the code into the event onViewCreated, like this:
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getActivity().setTitle("Titolo");
BottomNavigationView navigation = (BottomNavigationView) getActivity().findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content, new Calendario()).commit();
return inflater.inflate(R.layout.u16_layout, container, false);
}
This will work :)
I want add 2 fragment into activity, and when click on button switch between this fragments!
I want show Fragment_1 for by default in activity (my mean is : when running activity, show fragment_1) and when click on button switch between fragment_2 and fragment_1 !
I write below codes, and when running activity show fragment_1 but when click on button for switch between fragments, show me Force Close error.
FC error :
08-27 14:15:41.363 14224-14224/com.mohammad.nouri.diaryday E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mohammad.nouri.diaryday, PID: 14224
java.lang.IllegalStateException: commit already called
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:641)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:621)
at com.mohammad.nouri.diaryday.Activities.LoginActivity$1.onClick(LoginActivity.java:58)
at android.view.View.performClick(View.java:4764)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5349)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Activity codes:
public class LoginActivity extends AppCompatActivity {
private Context context;
private boolean status = false;
FragmentTransaction fT;
FragmentManager fM;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context = this;
Glide.with(this).load(R.drawable.login_header_image)
.bitmapTransform(new BlurTransformation(context, 20))
.into((ImageView) findViewById(R.id.login_background));
fM = getSupportFragmentManager();
fT = fM.beginTransaction();
if (!status) {
LoginFragment f1 = new LoginFragment();
fT.add(R.id.login_cardView, f1);
fT.commit();
status = true;
}
FloatingActionButton f = (FloatingActionButton) findViewById(R.id.login_registerButton);
f.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!status) {
RegisterFragment f1 = new RegisterFragment();
fT.add(R.id.login_cardView, f1);
fT.commit();
status = true;
}else {
LoginFragment f2 = new LoginFragment();
fT.add(R.id.login_cardView, f2);
fT.commit();
status = false;
}
}
});
}
}
How can I fix it ? Thanks all <3
You only call commit() once in a FragmentTransaction which you already do in the onCreate(). So, just create a new FragmentTransaction within your fab:
FloatingActionButton f = (FloatingActionButton) findViewById(R.id.login_registerButton);
f.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!status) {
RegisterFragment f1 = new RegisterFragment();
fM.beginTransaction().add(R.id.login_cardView, f1)
.commit();
status = true;
}else {
LoginFragment f2 = new LoginFragment();
fM.beginTransaction().add(R.id.login_cardView, f2)
.commit();
status = false;
}
}
});
Instead of adding the fragments try replacing because you have already added once above.
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnFragment;
LinearLayout fragmentContainer;
boolean toggleFlag = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentContainer = (LinearLayout) findViewById(R.id.fragment_container);
FragmentA fragmentA = new FragmentA();
final FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragmentA);
fragmentTransaction.commit();
btnFragment = (Button) findViewById(R.id.btn_fragment);
btnFragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(toggleFlag) {
FragmentA fragmentA = new FragmentA();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragmentA);
fragmentTransaction.commit();
} else {
FragmentB fragmentB = new FragmentB();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragmentB);
fragmentTransaction.commit();
}
toggleFlag = !toggleFlag;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/btn_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Toggle Fragment" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
FragmentA.java
public class FragmentA extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.layout_fragment_a, null);
return rootview;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
layout_fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/picture"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#mipmap/ic_launcher"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo Fragment A"
android:textColor="#android:color/black"/>
</LinearLayout>
FragmentB.java
public class FragmentB extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.layout_fragment_b, null);
return rootview;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
layout_fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/picture"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#mipmap/ic_launcher"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Demo Fragment B"
android:textColor="#android:color/black"/>
</LinearLayout>
use this method to call fragment
rplaceFragment(Fragment fragment , int id){
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(id, fragment);
fragmentTransaction.commit();
}
I am trying to show different fragments depending on whether GPS is enabled or not. But I am getting this error :-
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f0e00a1
Please advise me how to do this:
Here is my MainActivity class:
public class MainActivity extends Activity {
Fragment fr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
fr = new FragmentTwo();
getFragment();
} else {
fr = new FragmentOne();
getFragment();
}
}
public void getFragment() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
FragmentOne:-
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one, container, false);
}
}
FragmentTwo:-
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_two, container, false);
}
}
My activity_main:-
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
And the fragment_one.xml and fragment_two.xml are same just the different text:-
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="GPS IS ENABLE"/>
</RelativeLayout>
Thanks in advance.
Replace
<fragment
android:id="#+id/fragment_place"
android:name="com.test.FragmentTwo"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
With
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
As static Fragment can not be replace at run time. But you can add or replace fragment in FrameLayout.
You can remove this line
android:name="com.test.FragmentTwo"
<fragment
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
In a class i am adding a Fragment programatically.
This Fragment contains a Button, which i want to have an onClick implementation.
Here is the class i have:
public class ShareProduct extends SherlockFragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share_product);
FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
ProductFragment1 fragment = new ProductFragment1();
fragmentTransaction.add(R.id.share_product_parent, fragment);
fragmentTransaction.commit();
Button done_button = (Button) fragment.getView().findViewById(
R.id.done_product_1);
done_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
ProductFragment2 fragment = new ProductFragment2();
fragmentTransaction
.replace(R.id.share_product_parent, fragment);
fragmentTransaction.commit();
}
});
}
}
and my ProductFragment1 class is:
public class ProductFragment1 extends SherlockFragment {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater
.inflate(R.layout.share_product_1, container, false);
return view;
}
}
and the layout my main class uses is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/share_product_parent"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right|center_vertical"
android:orientation="horizontal" >
</RelativeLayout>
and the layout my ProductFragment1 uses is:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp" />
<EditText
android:id="#+id/et1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv1"
android:textSize="25dp" />
<Button
android:id="#+id/done_product_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/et1"
android:text="Done" />
</RelativeLayout>
The problem is:
I am getting a NullPointerException at this line in my main class:
Button done_button = (Button) fragment.getView().findViewById(R.id.done_product_1);
done_button.setOnClickListener ....
when i searched for what getView() on a fragment returns. I found out that it returns the view from onCreateView();
I checked it doesn't return null.
Thank You
You are on the wrong track here. You get the NPE because the Fragments views is not created yet. Fragments also have a lifecycle just like Activities.
What you should be doing is assigning the OnClickListener inside your Fragments onViewCreated() method. And from there... you should create a callback and notify your hosting Activity of the event.