gpt4 book ai didi

java - 难以从使用谷歌地图地点选择器 API 获得的位置传递数据并将其发送到 textView 以供用户查看位置

转载 作者:行者123 更新时间:2023-11-30 00:27:11 26 4
gpt4 key购买 nike

我正在开发一个简单的应用程序,它使用 google place picker API 检索地点的位置,但我无法检索数据并将其设置为 textView。检索数据时其他一切都分开工作。当覆盖方法时总是使用@Overide 注释以确保您重写您打算使用的方法以避免以下错误

下面是我的代码示例:

public class MainActivity extends AppCompatActivity {
private EditText name;
private EditText email;
private TextView date;
private TextView location;


private Button Submit,Select;
private String NAME; // stores name value
private String EMAIL; // stores email value
private String DATE; // stores date value

private DatePickerDialog.OnDateSetListener dateSetListener;

int PLACE_PICKER_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

name = (EditText)findViewById(R.id.edtNameID);
email = (EditText)findViewById(R.id.edtEmailID);

NAME = name.getText().toString();
EMAIL = email.getText().toString();

location = (TextView)findViewById(R.id.locationtxt);

date = (TextView)findViewById(R.id.txtSelectDate);
date.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {

java.util.Calendar calendar = java.util.Calendar.getInstance();
int month = calendar.get(java.util.Calendar.MONTH);
int year = calendar.get(java.util.Calendar.YEAR);
int day = calendar.get(java.util.Calendar.DAY_OF_MONTH);

month = month+1;

DatePickerDialog dialog = new DatePickerDialog(MainActivity.this,R.style.Theme_AppCompat_DayNight, dateSetListener,year,month,day);
dialog.show();
}
});
dateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
String result = day +"/" +month +"/"+year;
date.setText(result);
}
};
DATE = date.getText().toString();


Select = (Button)findViewById(R.id.SelectLocationbtn);
Select.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

try {

Intent intent = builder.build(MainActivity.this);
startActivityForResult(intent,PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();

} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}

}
});


Submit = (Button)findViewById(R.id.Submitbtn);
Submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

}
});

}
protected void OnActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);

if(requestCode==PLACE_PICKER_REQUEST){

if(resultCode== RESULT_FIRST_USER){
Place place = PlacePicker.getPlace(data, MainActivity.this);
String name = String.format("Place: %s",place.getName());
location.setText(name);

}

}


}
}

最佳答案

问题出在 OnActivityResult() 中的大写 O;

使用@Override 注释是个好主意,以确保您重写您打算重写的方法:

@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);

if(requestCode==PLACE_PICKER_REQUEST){

if(resultCode== RESULT_FIRST_USER){
Place place = PlacePicker.getPlace(data, MainActivity.this);
String name = String.format("Place: %s",place.getName());
location.setText(name);

}

}
}

关于java - 难以从使用谷歌地图地点选择器 API 获得的位置传递数据并将其发送到 textView 以供用户查看位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45132620/

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