gpt4 book ai didi

java - 从另一个类中获取值(value)

转载 作者:行者123 更新时间:2023-12-01 16:51:31 25 4
gpt4 key购买 nike

我想从另一个类获取值,但是当我在第二类中获取它时,它是空的。我认为这是由于 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,但我必须通过单击 map 上的标记来运行计时器。我无法在此计时器之外设置收件人电子邮件的值,因为 bcs 值不会相同。所以当我在另一个类中调用它时,通常:

MapsActivity mapsActivity = new MapsActivity();

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

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

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) 

你能帮我一下吗?

最佳答案

在这里,您创建一个(可能是)MapsActivity实例:

MapsActivity mapsActivity = new MapsActivity();

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

recipientUsername2 = mapsActivity.getRecipientEmail();

难怪它是空的。 这个 MapsActivity 不是您正在寻找的 MapsActivity...(只是这一次它不是绝地诡计。)

尝试这样理解:当您在 MapsActivity 中设置 recipientEmail 字段时,您将其写在一张纸上。通过 new MapsActivity() 构造函数调用,您从抽屉中取出一张新纸 - 一张不同的纸,现在您尝试从纸上读取一些内容,其中你在另一张上写了...

为了能够获得该信息,您必须将其放在某个地方 - 将其设置为您传递的类的实例,将其设置为您接下来计划使用的类中的字段,等等...

但是,这一次,我认为将 recipientEmail 字符串设置为 SendMail Activity 的额外内容将是正确的方法:

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

当您尝试在 SendMail Activity 中访问它时:

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 的答案我通常在需要处理 Extras 时求助。

另一件事:访问对象成员的推荐方法是将它们设置为私有(private) vland 添加 getter 方法以及适用的 setter 方法。仅仅让变量可访问是有问题的。

关于java - 从另一个类中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39195052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com