当前位置: 首页 > 知识库问答 >
问题:

为什么我的捆绑附加物不存在?

束研
2023-03-14

我正在构建一个应用程序,我正在使用FCM通知。当用户点击notification时,我想将他发送到Main Activity上的Fragment1(这是片段的名称)。我使用intent.putExtra完成了这一操作,但在主活动中,我将Bundle extras=getIntent().getExtras(),我有空指针异常。为什么我没有Putextra的东西?

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.d("OnMessage", "Received");
    super.onMessageReceived(remoteMessage);
    Log.d(TAG, "From " + remoteMessage.getFrom());
    Log.d(TAG, "Body " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage);

}




private void sendNotification(RemoteMessage remoteMessage) {
    Intent intent=new Intent(this, MainActivity.class);
    intent.putExtra("action", "goToFragment1");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri defaultSoundUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder=new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentText(remoteMessage.getNotification().getBody())
            .setContentTitle("Asp")
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());


}
@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("Bravo", "Subscribed to developeri!");
        }
    });


    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);
    Intent intent=getIntent();
    Bundle extras=getIntent().getExtras();
    Log.d("Bundle", "Bundle is here");
    Log.d("Extras", extras.getString("action"));
    if (extras!=null && extras.containsKey("action") && extras.getString("action").contains("goToFragment1")){
        getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
        Log.d("MainActivity", "IF IS HERE");

    }

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


     }
 };

}

我的logcat

共有1个答案

巫马泓
2023-03-14

On create仅在启动activity时调用。它主要保持“活动”,同时收到您的通知。因此,您应该让您的代码处理额外的内容:

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        if (intent.hasExtra("action")) {
            String action =  intent.getExtras().getString("action");
            // Do what ever is needed
            if (action.equalsIgnoreCase("gotToFragment1")) { 
                getSupportFragmentManager().beginTransaction().replace(R.id.myFragment, new Fragment1()).commit();
            }
        }
    }

onNewIntent()方法添加到MainActivity(接收新意图的activity)中。如果您的活动已经启动,将调用此命令。

 类似资料:
  • 设置: 具有一个片段的活动,该片段通过单击按钮进行实例化。在fragment的构造函数中,使用了Bundle。在Bundle和ArrayList中 问题:分离片段时,字符串(姓氏)会按预期销毁,但数组列表会持续存在。因此,在调用片段的新实例时,会出现前一个ArrayList条目。回调不是问题所在。该行为也会在没有回调的情况下出现。 我已经检查了变量(和)和在点片段构造函数(),片段在方法()和片段

  • 问题内容: 我正在尝试使用webpack开发angular2应用,但最终在浏览器控制台中显示错误:Uncaught ReferenceError:未定义系统。 当我查看捆绑的js时,发现它正在使用System.register,如下所示: 我的webpack.config.js非常简单,如下所示: 谁能为我修复?谢谢。 问题答案: 正如@Ron建议的 如果您使用Webpack捆绑应用程序,则必须将

  • 当我运行react native项目时,我得到一个错误,但我不知道我会犯什么错误,我非常困惑。

  • 问题内容: 当我使用编译类时,收到此错误消息? 包不存在 我可以通过将其包含在编译类路径中来进行修复。 为什么不在默认的jdk类路径上? 是在类路径上,并包括其他软件包,但jce似乎很特殊? 问题答案: 好的,这是我的错误。我用来编译代码的Ant文件在任务中具有以下属性: h 您可以像这样添加JCE jar:

  • 把一个例子从尼尔·巴特利特OSGi的书” ^我把它放在一个文件叫做HelloUpdaterActivator。 我成功地编译java文件教程智慧 然后我把所有生成的类文件 最后,我做了一个通用的样品清单。MF文件 现在jar已经准备好了,我在终端中打开了OSGi,它有自己的小控制台,就像这样: OSGi 控制台如下所示: 我在OSGi控制台中输入: 但是即使它说我创建的jar文件是活动的,“Hel

  • 将W,S和I,k绑定到乒乓球的左右桨。根据Eclipse,代码进入类p1_upaction扩展AbstractAction,但不运行公共void actionPerform(ActionEvent e)。我不知道该怎么办。键输入称为操作未注册。