简体   繁体   English

调用setVisibility类时出错

[英]Error when the setVisibility class is called

I try to execute a method automatically from my class MyFirebaseMessaging.java that receives by firebase message by cloud, and when it arrives all this code is activated, executing the method that is in Home. 我尝试从我的类MyFirebaseMessaging.java中自动执行一个方法,该方法通过云通过Firebase消息接收消息,并在到达时激活所有这些代码,并执行Home中的方法。

The error is that when I still declare onCreate and globally declare the variable the CardView cardUDatos, it tells me that: 错误是,当我仍然声明onCreate并全局声明变量CardView cardUDatos时,它告诉我:

E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setVisibility(int)' on a null object reference E / UncaughtException:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.support.v7.widget.CardView.setVisibility(int)”

MyFirebaseMessaging.java MyFirebaseMessaging.java

public class MyFirebaseMessaging extends FirebaseMessagingService {
@Override
public void onMessageReceived(final RemoteMessage remoteMessage) {
if(remoteMessage.getData() != null) {

        Map<String,String> data = remoteMessage.getData();
        String title = data.get("title");
        final String message = data.get("message");
   if (title.equals("Aceptar")) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
                Home SDI = new Home();
                SDI.MostrarDatos();

            }
        });
    }
     }
   }
    }

Clase Home.java donde esta el layout Clase Home.java目录的布局

 CardView cardUDatos;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
{
 .
 .
 .
 cardUDatos = (CardView) findViewById(R.id.cardview_user);
 .
 .
 .
}

 public void MostrarDatos(){ DatabaseReference UserInformation = FirebaseDatabase.getInstance().getReference(Common.User);
  UserInformation.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        if(dataSnapshot.exists()){
            List<Object> map = (List<Object>) dataSnapshot.getValue();

       String name

       name = String.valueOf(map.get(2).toString());

   }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // ...
    }
});


cardUDatos.setVisibility(View.VISIBLE);
}

The problem is following line: 问题如下:

Home SDI = new Home();

You shouldn't try to access Activity in this way. 您不应该尝试以这种方式访问​​活动。 This will only create the instance of Activity. 这只会创建Activity的实例。 This instance won't have any relation with the actual Activity (if running in foreground). 该实例与实际的Activity没有任何关系(如果在前台运行)。 The life-cycle methods won't be called as a consequence your cardUDatos won't be initialized. 由于不会初始化cardUDatos因此不会调用生命周期方法。

One way is to implement the LocalBroadcastReceiver . 一种方法是实现LocalBroadcastReceiver Refer to this SO for implementation details 有关实现的详细信息,请参阅此SO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM