2017-03-02 25 views
0

當我嘗試使用自動完成位置功能進行實驗時,出現空指針異常。我在文檔中添加了片段,並需要幫助解決此問題。謝謝。谷歌地方片段上的Java空指針異常

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" /> 

堆棧traace

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)  
+2

發佈錯誤的logcat .. – rafsanahmad007

+0

沒有人可以在沒有堆棧跟蹤幫助。 –

+0

已發佈@ rafsanahmad007 – Nobody

回答

0

SupportMapFragment未在XML佈局文件中添加,但你是在Java代碼中使用它。

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
+0

這是Google地圖活動附帶的內置代碼。我沒有發佈,認爲它沒有必要的xml – Nobody

0
在XML

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

在代碼:使用它:

SupportPlaceAutocompleteFragment autocompleteFragment = (SupportPlaceAutocompleteFragment) getSupportFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 
+0

它說不能解決方法getChildFragmentManager – Nobody

+0

不可轉換類型;無法將'android.support.v4.app.Fragment'轉換爲'com.google.android.gms.location.places.ui.PlaceAutocompleteFragment ..現在就是它顯示的內容 – Nobody

+0

我編輯了答案... – rafsanahmad007

0

我通過包括裏面默認的自動完成的片段解決了這個谷歌地圖佈局文件和以前的相同的java文件。我已經發布了下面的最終代碼。

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>