从另一个班级获得价值

时间:2016-08-28 19:35:06

标签: java android firebase firebase-realtime-database

我想从另一个类中获取值,但是当我在第二个类中获取它时它是null。我认为这是因为在此String中添加了代码无法运行的内容时会导致bcs。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

很多不重要的代码.......................................

FirebaseDatabase database5 = FirebaseDatabase.getInstance();
    DatabaseReference myRef5 = database5.getReference("userdata");

    myRef5.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.

很多不重要的代码.......................................... ...

mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
                    @Override
                    public boolean onMarkerClick(final Marker marker) {

很多不重要的代码......................................... ..

new CountDownTimer(2000, 1000) {

           public void onTick(long millisUntilFinished) {

           }
           public void onFinish() {

               progressBar.setVisibility(View.GONE);
               textViewPleaseWait.setVisibility(View.INVISIBLE);

               recipientEmail = marker.getTitle();
               Log.i("omg", recipientEmail);

               dialogBuilder.setMessage("Username: " + usernameAlert + "\n" + "Gender: " + genderAlert
                                       + "\n" + "Age: " + ageAlert + "\n" + marker.getSnippet() + "\n" + marker.getTitle());

               dialogBuilder.setPositiveButton("Send message", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
                       Intent intent = new Intent(getApplicationContext(), SendMessage.class);
                       startActivity(intent);

                       recipientEmail = marker.getTitle();
                   }
               });

               dialogBuilder.setNegativeButton("Close", null);
               dialogBuilder.show();


           }
       }.start();

}

这里是....我在计时器完成时将值设置为recipientEmail但我必须通过点击地图上的标记来运行计时器。我无法在此计时器之外设置值recipientEmail,bcs值将不相同。因此,当我在另一个班级中调用它时,通常是:

MapsActivity mapsActivity = new MapsActivity();

    recipientUsername2 = mapsActivity.getRecipientEmail();
    Log.i("values2", mapsActivity.recipientEmail);

运行第二个活动应用程序崩溃后。

08-28 21:34:25.943 4609-4609/com.samo.facedatefb E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.samo.facedatefb, PID: 4609
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.samo.facedatefb/com.samo.facedatefb.SendMessage}: java.lang.NullPointerException: println needs a message
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526)
    at android.app.ActivityThread.access$800(ActivityThread.java:169)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5549)
    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:964)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
 Caused by: java.lang.NullPointerException: println needs a message
    at android.util.Log.println_native(Native Method)
    at android.util.Log.i(Log.java:160)
    at com.samo.facedatefb.SendMessage.onCreate(SendMessage.java:79)
    at android.app.Activity.performCreate(Activity.java:5975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2526) 
    at android.app.ActivityThread.access$800(ActivityThread.java:169) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1421) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:194) 
    at android.app.ActivityThread.main(ActivityThread.java:5549) 
    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:964) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 

你能帮我吗?

1 个答案:

答案 0 :(得分:4)

在这里,您可以创建,(可能是MapsActivity实例:

MapsActivity mapsActivity = new MapsActivity();

下面两行,你试着从中读取:

recipientUsername2 = mapsActivity.getRecipientEmail();

难怪它是空的。 此MapsActivity不是您正在寻找的MapsActivity ... (这次它不是Jedi技巧。)

尝试以这种方式掌握:在MapsActivity中设置recipientEmail字段时,将其写在一张纸上。通过new MapsActivity()构造函数调用,您可以从抽屉中获取一张新纸 - 一个不同的纸,现在您尝试读取该纸上的内容,而您在另一个上面写了一些内容。 ...

为了能够拥有这些信息,你必须把它放在某处 - 将它设置为你传递的类的实例,将它设置为你打算在下一个使用的类中的字段,等等...... / p>

但是,这一次,我认为将recipientEmail字符串设置为SendMail活动的额外内容是可行的方法:

Intent intent = new Intent(getApplicationContext(), SendMessage.class);
intent.putExtra("RECIPIENT_EMAIL", marker.getTitle());
startActivity(intent); //this has to be after the putExtra call

当您尝试在SendMail活动中访问它时

String recipientEmail = null;
Bundle extras = getIntent().getExtras();
if(extras == null) {
    throw new IllegalStateException("No email address found at SendMail activity!");
} else {
    recipientEmail = extras.getString("RECIPIENT_EMAIL");
}

此外,阅读this question的答案,我通常会在必须处理额外内容时转向。

另一件事:访问对象成员的推荐方法是将它们设置为private vland add getter 方法,并在适用的情况下设置 setter 方法。只是让变量可以访问是有问题的。