Google上的Java空指针异常放置片段 [英] Java null pointer exception on google places fragment

查看:81
本文介绍了Google上的Java空指针异常放置片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用自动完成位置功能时,出现了空指针异常.我在文档中添加了该片段,并需要帮助来解决这一问题.谢谢.

I'm getting a null pointer exception when I tried to experiment with the autocomplete places feature. I added the fragment like in the docs and require help in resolving this one. Thank you.

MainActivity

package com.example.srinivas.mapautocompletetest1;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private TextView autocom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        autocom = (TextView)findViewById(R.id.autotv);

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                //    Log.i(TAG, "Place: " + place.getName());
                autocom.setText(""+place.getName()+"");
            }

            @Override
            public void onError(Status status) {
//                // TODO: Handle the error.
//                Log.i(TAG, "An error occurred: " + status);
                autocom.setText(""+status+"");
            }
        });



    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}

autocom.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        card_view:cardCornerRadius="4dp">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/place_autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.CardView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/autotv"/>
</LinearLayout>

activity_maps.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.srinivas.mapautocompletetest1.MapsActivity" />

堆栈跟踪

03-02 13:10:45.665 14169-14169/com.example.srinivas.mapautocompletetest1 E/dalvikvm: Could not find class 'com.google.android.chimera.Activity', referenced from method mu.b
03-02 13:10:45.908 14169-14169/com.example.srinivas.mapautocompletetest1 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                           Process: com.example.srinivas.mapautocompletetest1, PID: 14169
                                                                                           java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.srinivas.mapautocompletetest1/com.example.srinivas.mapautocompletetest1.MapsActivity}: java.lang.NullPointerException
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
                                                                                               at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                                               at android.os.Looper.loop(Looper.java:193)
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5292)
                                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                               at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                                            Caused by: java.lang.NullPointerException
                                                                                               at com.example.srinivas.mapautocompletetest1.MapsActivity.onCreate(MapsActivity.java:37)
                                                                                               at android.app.Activity.performCreate(Activity.java:5264)
                                                                                               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
                                                                                               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390) 
                                                                                               at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
                                                                                               at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                                               at android.os.Looper.loop(Looper.java:193) 
                                                                                               at android.app.ActivityThread.main(ActivityThread.java:5292) 
                                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                               at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                                               at dalvik.system.NativeStart.main(Native Method) 

推荐答案

我通过在默认 google maps 布局文件和相同的Java文件中包含 autocomplete片段来解决了此问题像以前一样归档.我已经在下面发布了最终代码.

I solved this by including the autocomplete fragment inside the default google maps layout file and the same java file as before. I have posted the final code below.

activity_maps.xml

<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android" >

<fragment
    android:id="@+id/place_autocomplete_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />

<fragment
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.srinivas.democomplete.MapsActivity>
</LinearLayout>

这篇关于Google上的Java空指针异常放置片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆