[英]onActivityResult not called for intent
我嘗試使用基於 googles 示例的 google Places API 使地點自動完成,但我面臨的問題是它不調用 onActivityResult 方法。 我已經閱讀了上一個線程,但它不是很有幫助。 這是我的代碼:
public class AutoCompleteActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_autocomplete);
}
public void findPlace(View view) {
try {
Intent intent =
new PlaceAutocomplete
.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.build(this);
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println("======");
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// retrive the data by using getPlace() method.
Place place = PlaceAutocomplete.getPlace(this, data);
Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());
((TextView) findViewById(R.id.searched_address))
.setText(place.getName()+",\n"+
place.getAddress() +"\n" + place.getPhoneNumber());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
Log.e("Tag", status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
finish();
}
}
}
}
如果需要,這是我的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".AutoCompleteActivity">
<Button
android:id="@+id/findPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Google Places"
android:onClick="findPlace"/>
<TextView
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="25dp"
android:layout_below="@+id/findPlace"
android:id="@+id/searched_address"/>
</RelativeLayout>
我似乎無法獲得有關 google API 的建議 - (不是 GOOGLE API KEY NOT ENABLED 問題),它一直在加載並最終失敗。
您是否將此標簽放在清單文件中...元數據android:name =“ com.google.android.geo.API_KEY” android:value =“ XXXXXXBsWU9seEWmlB-CMc_pxZ358b4-esTNR5I” /> **
確保傳遞給startActivityForResult(...)的數字參數> = 0
或者您可能在清單中有問題。 檢查清單並確保您未使用單個實例:如果是這種情況,那么從活動中刪除android:noHistory =“ true”將解決問題。
機器人:launchMode = “singleInstance”
或如果要將大數據像位圖一樣傳遞到Intent,請考慮將其存儲在SD上的某個位置,在Intent中傳遞路徑,同時在ParentActivity中從SD讀取此位圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.