I am building an app that has an option when user click on push notifcation, he goes to the fragment on MainActivity(which is called Fragment1). Here is my entire code and i don't know how to write code for switching to the Fragment1. Could someone help me?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.dugme1);
btn2=(Button)findViewById(R.id.dugme2);
btn3=(Button)findViewById(R.id.dugme3);
dugme=(Button)findViewById(R.id.subscribe);
dugme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FirebaseMessaging.getInstance().subscribeToTopic("developeri");
Log.d(TAG, "Subscribed to developeri topic");
}
});
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
StartFragment startFragment=new StartFragment();
ft.add(R.id.myFragment, startFragment);
ft.commit();
btn1.setOnClickListener(btnOnClickListener);
btn2.setOnClickListener(btnOnClickListener);
btn3.setOnClickListener(btnOnClickListener);
Bundle extras=getIntent().getExtras();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if (extras.getString("action").contains("goToFragment1")){
//code for switching fragments
}
}
Button.OnClickListener btnOnClickListener= new Button.OnClickListener() {
@Override
public void onClick(View view) {
Fragment newFragment;
if (view==btn1){
newFragment =new Fragment1();
}else if(view==btn2){
newFragment =new Fragment2();
}else if (view==btn3){
newFragment =new Fragment3();
}else{
newFragment=new StartFragment();
}
FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.myFragment, newFragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_NONE);
transaction.commit();
}
};
EDIT: This "myFragment" is the first fragment that is on MainActivity and Fragment1 is the fragment to which i want to go when click on notification
EDIT1: This is activity_main XML file.
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"
android:orientation="horizontal"
tools:context="com.example.dev3.fragmenti.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dugme1"
android:text="Fragment1"
android:textColor="#000000"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dugme2"
android:text="Fragment2"
android:textColor="#000000"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/dugme3"
android:text="Fragment3"
android:textColor="#000000"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/subscribe"
android:text="Subscribe"
android:textColor="#000000"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
android:id="@+id/myFragment"
android:orientation="vertical"
>
</LinearLayout>
My logcat error:
06-06 13:37:21.498 16764-16764/com.example.dev3.fragmenti E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dev3.fragmenti/com.example.dev3.fragmenti.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$700(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.dev3.fragmenti.MainActivity.onCreate(MainActivity.java:63)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$700(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at dalvik.system.NativeStart.main(Native Method)
In order to get rid of the NullPointerException, you should Change
if (extras.getString("action").contains("goToFragment1"))
to this:
if (extras != null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1"))
That way, it should not cause NullPointerException
if the extras
is null
or does not contain key " action
". So your code should look like this:
...
Bundle extras=getIntent().getExtras();
if (extras != null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1")){
//code for switching fragments
//try this
getSupportFragmentManager().beginTransaction().replace(R.id.myFragment,new Fragment1()).commit();
}
By the way, I am not sure why you have myFragment as a LinearLayout
- this should be a FrameLayout
instead. In any case, please give this a try and let me know if you get any errors.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.