从推送通知中击中片段

时间:2016-06-06 07:28:25

标签: android android-fragments push-notification

我正在构建一个app,当用户点击push notifcation时有一个选项,他会转到MainActivity上的片段(称为Fragment1)。这是我的整个代码,我不知道如何编写代码切换到Fragment1。有人能帮助我吗?

@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();


     }
 };

编辑:这个“myFragment”是MainActivity上的第一个片段,Fragment1是点击通知时我想去的片段

EDIT1:这是activity_main XML文件。

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>

我的logcat错误:

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) 

1 个答案:

答案 0 :(得分:0)

为了摆脱NullPointerException,你应该改变

if (extras.getString("action").contains("goToFragment1")) 

到此:

if (extras != null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1"))

这样,如果NullPointerExceptionextras或不包含密钥“null”,则不应导致action。所以你的代码应该是这样的:

...
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();
}

顺便说一句,我不确定为什么你将myFragment作为LinearLayout - 这应该是FrameLayout。无论如何,请试一试,如果您有任何错误,请告诉我。