2016-07-13 118 views
0

我想添加一个AutoCompleteTextView,将拉的位置通过GoogleApiClient。 我尝试了许多不同的选择,使这项工作,我得到了以下错误:在片段类 - 试图调用虚拟方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'null对象引用

Attempt to invoke virtual method 'void com.google.android.gms.common.api.GoogleApiClient.connect()' on a null object reference

请告诉我,我做错了什么,还是建议如何改进这一点。

下面是XML代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.freightos.priceestimator.MainActivity$ContainerFragment" 
android:id="@+id/tabContainer"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="50dp" 
     android:gravity="center"> 

     <AutoCompleteTextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/autocomplete" 
      android:drawableLeft="@drawable/location_icon_s" 
      android:drawablePadding="5dp" 
      android:hint="Type in your Location" /> 

    </LinearLayout> 

</LinearLayout> 

这里是ContainerFragment类:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.ToggleButton; 
import com.google.android.gms.common.api.PendingResult; 

import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.common.api.ResultCallback; 
import com.google.android.gms.location.places.Place; 
import com.google.android.gms.location.places.PlaceBuffer; 
import com.google.android.gms.location.places.Places; 
import com.google.android.gms.location.places.ui.PlacePicker; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 

import org.w3c.dom.Text; 

/** 
* Created by Leonid on 6/30/2016. 
*/ 
public class ContainerFragment extends Fragment { 

    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
// public Button btnCallAPI2; 

    public ContainerFragment() { 
    } 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    /*** Start Location Parameters *****/ 
    private AutoCompleteTextView autocomplete; 
    private PlacePicker.IntentBuilder builder; 
    private PlacesAutoCompleteAdapter mPlacesAdapter; 
    private static final int PLACE_PICKER_FLAG = 1; 

    private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new LatLngBounds(
      new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362)); 
    protected GoogleApiClient mGoogleApiClient; 

    /*** End Location Parameters *****/ 

    public static ContainerFragment newInstance(int sectionNumber) { 
     ContainerFragment fragment = new ContainerFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.container_main, container, false); 

     /***** Start of Location Finder ****/ 
     autocomplete = (AutoCompleteTextView) rootView.findViewById(R.id.autocomplete); 
     mPlacesAdapter = new PlacesAutoCompleteAdapter(this.getActivity(), android.R.layout.simple_list_item_1,mGoogleApiClient, BOUNDS_GREATER_SYDNEY, null); 
     autocomplete.setOnItemClickListener(mAutocompleteClickListener); 
     autocomplete.setAdapter(mPlacesAdapter); 
     /***** End of Location Finder ****/ 


     return rootView; 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == Activity.RESULT_OK) { 
      switch (requestCode) { 
       case PLACE_PICKER_FLAG: 
        Place place = PlacePicker.getPlace(data, this.getActivity()); 
        autocomplete.setText(place.getName() + ", " + place.getAddress()); 
        break; 
      } 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    public void onStop() { 
     mGoogleApiClient.disconnect(); 
     super.onStop(); 
    } 

    private AdapterView.OnItemClickListener mAutocompleteClickListener 
      = new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      final PlacesAutoCompleteAdapter.PlaceAutocomplete item = mPlacesAdapter.getItem(position); 
      final String placeId = String.valueOf(item.placeId); 
      PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi 
        .getPlaceById(mGoogleApiClient, placeId); 
      placeResult.setResultCallback(mUpdatePlaceDetailsCallback); 
     } 
    }; 
    private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback 
      = new ResultCallback<PlaceBuffer>() { 
     @Override 
     public void onResult(PlaceBuffer places) { 
      if (!places.getStatus().isSuccess()) { 
       Log.e("place", "Place query did not complete. Error: " + 
         places.getStatus().toString()); 
       return; 
      } 
      // Selecting the first object buffer. 
      final Place place = places.get(0); 
     } 
    }; 
} 

回答

0

您还没有初始化mGoogleApiClient对象。请执行下列操作:

protected void onCreate(Bundle savedInstanceState){ 
    ..... 

    // First we need to check availability of play services 
    if (checkPlayServices()) { 

     // Building the GoogleApi client 
     buildGoogleApiClient(); 
    } 

    .... 
} 

checkPlayServices方法:

private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil 
      .isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "This device is not supported.", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 

buildGoogleApiClient方法:

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 
} 

请查看以下链接的完整代码:source

+0

我添加的代码上面,但现在我得到以下错误: 尝试在空对象引用上调用虚方法'void com.google.android.gms.common.api.GoogleApiClient.connect()'参考 可能在片段类中它应该使用super.onStart();或者我错过了什么?有没有使用GoogleAPIClient对象的片段类的例子? – Leonid

+0

您可以确保调用buildGoogleApiClient()方法吗?我怕谷歌播放服务尚未提供。如果您还没有完成,请为您的Android SDK安装Google Play服务库(修订版15或更高版本)。 – whdinata

相关问题