Google地方信息自动填充API不会检索地点建议

时间:2014-03-10 11:53:42

标签: android autocomplete google-places-api google-maps-api-2

我在我的应用程序中使用Google Places Autocomplete API。我遵循了这个教程http://wptrafficanalyzer.in/blog/adding-google-places-autocomplete-api-as-custom-suggestions-in-android-search-dialog/但是当我在搜索框中写下地名时,它没有检索到建议,当我按下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(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();
            }
        }
    }   
}

2 个答案:

答案 0 :(得分:0)

public void onLoadFinished(Loader<Cursor> arg0, Cursor c) {
    // TODO Auto-generated method stub
    showLocations(c);
}

我的猜测是你的Cursor为null。你检查过了吗?

答案 1 :(得分:0)

Google Place已被弃用,因此您必须使用新的Place SDK。 您可以按照此答案轻松实现新的Place SDK。

https://stackoverflow.com/a/56477478/5514839