错误类放置自动完成

时间:2018-06-06 08:17:58

标签: android android-studio google-places-api googleplacesautocomplete

我正在尝试创建一个用于创建和管理自动完成片段的类。如果我在MainActivity中创建PlaceSelectionListener它可以工作,但如果我在类中创建它,应用程序崩溃。 我在哪里做错了?

这里是代码:

activity_main.xml中

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

MainActivity.java(创建Places对象)

Places places = new Places(this);

Places.java

public class Places extends AppCompatActivity{

    public Places( Context context ) {

    // Retrieve the PlaceAutocompleteFragment.
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    // Register a listener to receive callbacks when a place has been selected or an error has
    // occurred.
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected( Place place ) {
            Log.e("-----", place.toString());
        }

        @Override
        public void onError( Status status ) {
            Log.e("-----", status.toString());
        }
    });
}}

错误:

06-06 10:05:58.360 20562-20562/com.example.aleclock.streetcamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aleclock.streetcamera, PID: 20562
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aleclock.streetcamera/com.example.aleclock.streetcamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)' on a null object reference
    at com.example.aleclock.streetcamera.classes.Places.<init>(Places.java:35)
    at com.example.aleclock.streetcamera.MainActivity.onCreate(MainActivity.java:53)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

0 个答案:

没有答案