Java 通过多种意图恰当地传递价值

Java 通过多种意图恰当地传递价值,java,android,Java,Android,我已经尝试了所有好的ole方式,通过诸如getExtra和putExtra之类的意图发送数据。然而,这条特定的信息不会通过意图传播。变量在此处初始化(driverId): 单击此处的按钮时,我会发送driverId的信息: Intent intent = new Intent(Home.this, CallDriver.class); intent.putExtra("driverId",marker.getSnippet()); intent.putExtra("lat",mLastL

我已经尝试了所有好的ole方式,通过诸如getExtra和putExtra之类的意图发送数据。然而,这条特定的信息不会通过意图传播。变量在此处初始化(driverId):

单击此处的按钮时,我会发送driverId的信息:

 Intent intent = new Intent(Home.this, CallDriver.class);

 intent.putExtra("driverId",marker.getSnippet());
 intent.putExtra("lat",mLastLocation.getLatitude());
 intent.putExtra("lng",mLastLocation.getLongitude());
 startActivity(intent);
然后我调用文本中的信息,如下所示:

 if (driverId != null && !driverId.isEmpty())
                if (getIntent() !=null) {
                    driverId = getIntent().getStringExtra("driverId");
                }
                sendRequestToDriver(driverId, mService, getBaseContext(), mLastLocation);
        }
我向驱动程序发送请求的方法是:

 public static void sendRequestToDriver(String driverId,final IFCMService mService,final Context context,final Location currentLocation) {

        DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Common.token_tbl);

        tokens.orderByKey().equalTo(driverId)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                        {
                            Token token = postSnapShot.getValue(Token.class);

                            //String json_lat_lng = new Gson().toJson(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
                            String riderToken = FirebaseInstanceId.getInstance().getToken();



                            Map<String,String> content = new HashMap<>();
                            content.put("customer", riderToken);
                            content.put("driverId",driverId);
                            content.put("lat",String.valueOf(currentLocation.getLatitude()));
                            content.put("lng",String.valueOf(currentLocation.getLongitude()));
                            DataMessage dataMessage = new DataMessage(token.getToken(),content);
                            Log.d(String.valueOf(dataMessage), "here big boy"+dataMessage);

                            mService.sendMessage(dataMessage).enqueue(new Callback<FCMResponse>() {
                                @Override
                                public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                                    if(response.body().success == 1)
                                        Toast.makeText(context, "Request Sent!", Toast.LENGTH_SHORT).show();
                                    else
                                        Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onFailure(Call<FCMResponse> call, Throwable t) {
                                    Log.e("Error", t.getMessage());
                                }
                            });

                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

    }
public static void sendRequestToDriver(字符串driverId、最终IFCMService mService、最终上下文上下文、最终位置currentLocation){
DatabaseReference tokens=FirebaseDatabase.getInstance().getReference(Common.token\u tbl);
tokens.orderByKey().equalTo(driverId)
.addListenerForSingleValueEvent(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot postSnapShot:DataSnapshot.getChildren())
{
Token-Token=postsnashot.getValue(Token.class);
//字符串json_lat_lng=new Gson().toJson(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude());
字符串riderToken=FirebaseInstanceId.getInstance().getToken();
映射内容=新的HashMap();
内容。放置(“客户”,riderToken);
content.put(“driverId”,driverId);
put(“lat”,String.valueOf(currentLocation.getLatitude());
content.put(“lng”,String.valueOf(currentLocation.getLongitude());
DataMessage DataMessage=新的DataMessage(token.getToken(),content);
Log.d(String.valueOf(dataMessage),“here big boy”+dataMessage);
mService.sendMessage(dataMessage).enqueue(新的回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.body().success==1)
Toast.makeText(上下文,“请求已发送!”,Toast.LENGTH_SHORT.show();
其他的
Toast.makeText(上下文“失败”,Toast.LENGTH_SHORT).show();
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
Log.e(“Error”,t.getMessage());
}
});
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}

现在,当我尝试接收有关intent RateActivity的信息时,我得到一个空值。如何确保字符串driverId已正确初始化,或者数据在这些意图中始终保持不变?

尝试此
intent.putExtra(“driverId”,marker.getSnippet().toString());
intent.putExtra(“lat”,mLastLocation.getLatitude().toString());

intent.putExtra(“lng”,mLastLocation.getLongitude().toString())

试试这个
intent.putExtra(“driverId”,marker.getSnippet().toString());
intent.putExtra(“lat”,mLastLocation.getLatitude().toString());

intent.putExtra(“lng”,mLastLocation.getLongitude().toString())

尝试将此id保存在SharedReferences中,如果您不再需要它,请记住将其删除。您的意图将发送到
CallDriver.class
,因此我不确定您的RateActivity是哪一个。如果两者相同,请检查
marker.getSnippet()
是否在“driverId”中实际有任何字符串值要发送。请尝试将此id保存在SharedReferences中,如果不再需要它,请记住将其删除。您的意图发送到
CallDriver.class
,因此我不确定您的RateActivity是哪个。如果两者相同,请检查
marker.getSnippet()
是否确实有任何字符串值要在“driverId”中发送
 public static void sendRequestToDriver(String driverId,final IFCMService mService,final Context context,final Location currentLocation) {

        DatabaseReference tokens = FirebaseDatabase.getInstance().getReference(Common.token_tbl);

        tokens.orderByKey().equalTo(driverId)
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for(DataSnapshot postSnapShot:dataSnapshot.getChildren())
                        {
                            Token token = postSnapShot.getValue(Token.class);

                            //String json_lat_lng = new Gson().toJson(new LatLng(mLastLocation.getLatitude(),mLastLocation.getLongitude()));
                            String riderToken = FirebaseInstanceId.getInstance().getToken();



                            Map<String,String> content = new HashMap<>();
                            content.put("customer", riderToken);
                            content.put("driverId",driverId);
                            content.put("lat",String.valueOf(currentLocation.getLatitude()));
                            content.put("lng",String.valueOf(currentLocation.getLongitude()));
                            DataMessage dataMessage = new DataMessage(token.getToken(),content);
                            Log.d(String.valueOf(dataMessage), "here big boy"+dataMessage);

                            mService.sendMessage(dataMessage).enqueue(new Callback<FCMResponse>() {
                                @Override
                                public void onResponse(Call<FCMResponse> call, Response<FCMResponse> response) {
                                    if(response.body().success == 1)
                                        Toast.makeText(context, "Request Sent!", Toast.LENGTH_SHORT).show();
                                    else
                                        Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onFailure(Call<FCMResponse> call, Throwable t) {
                                    Log.e("Error", t.getMessage());
                                }
                            });

                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

    }