Android Google Places自动完成API未检索位置建议
我在应用程序中使用Google Places Autocomplete API。我遵循了本教程,但当我在搜索框中输入地名时,它无法检索建议,当我按下Go按钮时,我的应用程序崩溃。请帮忙 日志文件Android Google Places自动完成API未检索位置建议,android,autocomplete,google-places-api,google-maps-api-2,Android,Autocomplete,Google Places Api,Google Maps Api 2,我在应用程序中使用Google Places Autocomplete API。我遵循了本教程,但当我在搜索框中输入地名时,它无法检索建议,当我按下Go按钮时,我的应用程序崩溃。请帮忙 日志文件 03-10 16:51:56.383: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider 03-10 16:51:58.798: E/ActivityThread
03-10 16:51:56.383: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:51:58.798: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:51:59.594: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:52:00.399: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:52:01.805: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:52:06.298: E/ActivityThread(25238): Failed to find provider info for com.appscourt.earth.map.PlaceProvider
03-10 16:52:06.313: D/AndroidRuntime(25238): Shutting down VM
03-10 16:52:06.313: W/dalvikvm(25238): threadid=1: thread exiting with uncaught exception (group=0x40d072a0)
03-10 16:52:06.344: E/AndroidRuntime(25238): FATAL EXCEPTION: main
03-10 16:52:06.344: E/AndroidRuntime(25238): java.lang.NullPointerException
03-10 16:52:06.344: E/AndroidRuntime(25238): at com.appscourt.earth.map.location.MainActivity.showLocations(MainActivity.java:129)
03-10 16:52:06.344: E/AndroidRuntime(25238): at com.appscourt.earth.map.location.MainActivity.onLoadFinished(MainActivity.java:116)
03-10 16:52:06.344: E/AndroidRuntime(25238): at com.appscourt.earth.map.location.MainActivity.onLoadFinished(MainActivity.java:1)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.app.LoaderManagerImpl$LoaderInfo.onLoadComplete(LoaderManager.java:395)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.Loader.deliverResult(Loader.java:104)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:73)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.CursorLoader.deliverResult(CursorLoader.java:35)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:223)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:61)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:461)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.ModernAsyncTask.access$500(ModernAsyncTask.java:47)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:474)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.os.Looper.loop(Looper.java:137)
03-10 16:52:06.344: E/AndroidRuntime(25238): at android.app.ActivityThread.main(ActivityThread.java:5059)
03-10 16:52:06.344: E/AndroidRuntime(25238): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 16:52:06.344: E/AndroidRuntime(25238): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 16:52:06.344: E/AndroidRuntime(25238): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-10 16:52:06.344: E/AndroidRuntime(25238): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-10 16:52:06.344: E/AndroidRuntime(25238): at dalvik.system.NativeStart.main(Native Method)
MainActivity.Java
public class MainActivity extends FragmentActivity implements
LoaderCallbacks<Cursor> {
GoogleMap gMap;
ImageButton search;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeMap();
handleIntent(getIntent());
search = (ImageButton)findViewById(R.id.search);
search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
onSearchRequested();
}
});
}
private void handleIntent(Intent intent) {
if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
doSearch(intent.getStringExtra(SearchManager.QUERY));
} else if (intent.getAction().equals(Intent.ACTION_VIEW)) {
getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
handleIntent(intent);
}
private void doSearch(String query) {
Bundle data = new Bundle();
data.putString("query", query);
getSupportLoaderManager().restartLoader(0, data, this);
}
private void getPlace(String query) {
Bundle data = new Bundle();
data.putString("query", query);
getSupportLoaderManager().restartLoader(1, data, this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.action_search:
onSearchRequested();
break;
default:
break;
}
return super.onMenuItemSelected(featureId, item);
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle query) {
CursorLoader cLoader = null;
if (arg0 == 0)
cLoader = new CursorLoader(getBaseContext(),
PlaceProvider.SEARCH_URI, null, null,
new String[] { query.getString("query") }, null);
else if (arg0 == 1)
cLoader = new CursorLoader(getBaseContext(),
PlaceProvider.DETAILS_URI, null, null,
new String[] { query.getString("query") }, null);
return cLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
// TODO Auto-generated method stub
showLocations(c);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
// TODO Auto-generated method stub
}
private void showLocations(Cursor c) {
MarkerOptions markerOptions = null;
LatLng position = null;
gMap.clear();
while (c.moveToNext()) {
markerOptions = new MarkerOptions();
position = new LatLng(Double.parseDouble(c.getString(1)),
Double.parseDouble(c.getString(2)));
markerOptions.position(position);
markerOptions.title(c.getString(0));
gMap.addMarker(markerOptions);
}
if (position != null) {
CameraUpdate cameraPosition = CameraUpdateFactory
.newLatLng(position);
gMap.animateCamera(cameraPosition);
}
}
@SuppressLint("NewApi")
private void initializeMap() {
if (gMap == null) {
gMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (gMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
public类MainActivity扩展了FragmentActivity实现
装载机回扣{
谷歌地图;
图像按钮搜索;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
初始化映射();
handleIntent(getIntent());
search=(ImageButton)findViewById(R.id.search);
search.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
onSearchRequested();
}
});
}
私人无效手册内容(意图){
if(intent.getAction().equals(intent.ACTION_SEARCH)){
doSearch(intent.getStringExtra(SearchManager.QUERY));
}else if(intent.getAction().equals(intent.ACTION_视图)){
getPlace(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
}
}
@凌驾
受保护的void onNewIntent(意图){
super.onNewIntent(意向);
设定意图(intent);
手册内容(意图);
}
私有void doSearch(字符串查询){
Bundle data=新Bundle();
data.putString(“查询”,query);
getSupportLoaderManager().restartLoader(0,数据,此);
}
私有void getPlace(字符串查询){
Bundle data=新Bundle();
data.putString(“查询”,query);
getSupportLoaderManager().restartLoader(1,数据,此);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onMenuItemSelected(int-featureId,MenuItem项){
//TODO自动生成的方法存根
开关(item.getItemId()){
案例R.id.行动搜索:
onSearchRequested();
打破
违约:
打破
}
返回super.onMenuItemSelected(featureId,item);
}
@凌驾
公共加载器onCreateLoader(int arg0,Bundle查询){
游标加载程序cLoader=null;
如果(arg0==0)
cLoader=new CursorLoader(getBaseContext(),
PlaceProvider.SEARCH_URI,null,null,
新字符串[]{query.getString(“query”)},null);
else if(arg0==1)
cLoader=new CursorLoader(getBaseContext(),
PlaceProvider.DETAILS\u URI,null,null,
新字符串[]{query.getString(“query”)},null);
回程加载器;
}
@凌驾
public void onLoadFinished(加载程序arg0,光标c){
//TODO自动生成的方法存根
展览地点(c);
}
@凌驾
公共void onLoaderReset(加载程序arg0){
//TODO自动生成的方法存根
}
专用void显示位置(光标c){
MarkerOptions MarkerOptions=null;
车床位置=零;
gMap.clear();
while(c.moveToNext()){
markerOptions=新的markerOptions();
位置=新车床(Double.parseDouble(c.getString(1)),
parseDouble(c.getString(2));
标记选项。位置(位置);
markerOptions.title(c.getString(0));
gMap.addMarker(标记选项);
}
如果(位置!=null){
CameraUpdate cameraPosition=CameraUpdateFactory
.newLatLng(职位);
gMap.animateCamera(摄像机定位);
}
}
@SuppressLint(“新API”)
私有无效初始值映射(){
如果(gMap==null){
gMap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
//检查是否成功创建映射
如果(gMap==null){
Toast.makeText(getApplicationContext(),
“抱歉!无法创建地图”,Toast.LENGTH\u SHORT)
.show();
}
}
}
}
public void onLoadFinished(加载程序arg0,光标c){
//TODO自动生成的方法存根
展览地点(c);
}
我猜你的光标是空的。您检查过了吗?Google Place现在已经被弃用,因此您必须为Place使用新的Place SDK。 您可以按照这个答案轻松地实现new Place SDK
发布您的java代码我也发布了。请检查旧的位置选择器现在已被弃用。您必须使用新的位置SDK参考此链接
public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
// TODO Auto-generated method stub
showLocations(c);
}