简体   繁体   中英

java.lang.IllegalStateException on adding google places API in android

I want to retrieve current place by using Google Places API but on adding it the app crashes. Here is the code of Myactivity

ChooseLanguage.java

public class ChooseLanguage extends AppCompatActivity {
ViewFlipper vf;
GoogleApiClient mGoogleapiCient;
private RadioGroup language;
private RadioButton rb;
private ProgressBar pb;
private ChooseLanguage cl;
private boolean isLanguageselected;
private String lang;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_language);
    cl=this;
    isLanguageselected = false;
    tv=(TextView)findViewById(R.id.tv);
    language=(RadioGroup)findViewById(R.id.radioGroup);
    pb=(ProgressBar)findViewById(R.id.progressBar);
    vf=(ViewFlipper)findViewById(R.id.viewFlipper);
    vf.setInAnimation(this,R.anim.fade_in);
    vf.setOutAnimation(this,R.anim.fade_out);
    mGoogleapiCient =  new GoogleApiClient
            .Builder( this )
            .enableAutoManage(this, 0, new GoogleApiClient.OnConnectionFailedListener() {
                @Override
                public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

                }
            })
            .addApi( Places.GEO_DATA_API )
            .addApi( Places.PLACE_DETECTION_API )
            .build();
}
@Override
protected void onStart() {
    super.onStart();
    if( mGoogleapiCient != null )
        mGoogleapiCient.connect();
}

@Override
protected void onStop() {
    if (mGoogleapiCient != null && mGoogleapiCient.isConnected()) {
        mGoogleapiCient.disconnect();
    }
    super.onStop();
}
public void LangChosen(View view)
{
    int slctd=language.getCheckedRadioButtonId();
    rb=(RadioButton)findViewById(slctd);


    cl.lang=rb.getText().toString();
    if(cl.lang.contains("English"))
    {
        Toast.makeText(this,"Eng Chosen", Toast.LENGTH_LONG).show();
        MainActivity.getInstance().update(true);
    }
    else
    {
        Toast.makeText(this,"Hindi", Toast.LENGTH_LONG).show();
        MainActivity.getInstance().update(false);
    }
    Toast.makeText(this,"Reached", Toast.LENGTH_LONG).show();
    pb.setVisibility(View.VISIBLE);
    guessCurrentPlace();


}
private void guessCurrentPlace() {
    try {
        // location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace( mGoogleapiCient, null );
        result.setResultCallback( new ResultCallback<PlaceLikelihoodBuffer>() {
            @Override
            public void onResult( PlaceLikelihoodBuffer  likelyPlaces ) {

                PlaceLikelihood placeLikelihood = likelyPlaces.get( 0 );
                String content = "";
                if( placeLikelihood != null && placeLikelihood.getPlace() != null && !TextUtils.isEmpty( placeLikelihood.getPlace().getName() ) )
                    content = "Most likely place: " + placeLikelihood.getPlace().getName() + "\n";
                if( placeLikelihood != null )
                    content += "Percent change of being there: " + (int) ( placeLikelihood.getLikelihood() * 100 ) + "%";
                tv.setText( content );

                pb.setVisibility(View.GONE);
                likelyPlaces.release();

                startActivity(new Intent(ChooseLanguage.this, MainActivity.class));
                finish();
            }
        });}

    catch (SecurityException e) {
        // lets the user know there is a problem with the gps
    }
}

}

Here is android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.urjapawar.jagrukbharat">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="AIzaSyBsAr7DXxdEV0vTDkToPeHsz0FI-xbf4go"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
    <activity android:name=".ChooseLanguage">

    </activity>
</application>

</manifest>

Here is the log cat

08-13 19:55:44.644 2176-2176/com.urjapawar.jagrukbharat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.urjapawar.jagrukbharat, PID: 2176
                                                                      java.lang.IllegalStateException
                                                                          at com.google.android.gms.common.internal.zzaa.zzai(Unknown Source)
                                                                          at com.google.android.gms.common.data.zzc.zzbN(Unknown Source)
                                                                          at com.google.android.gms.common.data.zzc.<init>(Unknown Source)
                                                                          at com.google.android.gms.location.places.internal.zzt.<init>(Unknown Source)
                                                                          at com.google.android.gms.location.places.internal.zzn.<init>(Unknown Source)
                                                                          at com.google.android.gms.location.places.PlaceLikelihoodBuffer.get(Unknown Source)
                                                                          at com.urjapawar.jagrukbharat.ChooseLanguage$2.onResult(ChooseLanguage.java:104)
                                                                          at com.urjapawar.jagrukbharat.ChooseLanguage$2.onResult(ChooseLanguage.java:100)
                                                                          at com.google.android.gms.internal.zznv$zza.zzb(Unknown Source)
                                                                          at com.google.android.gms.internal.zznv$zza.handleMessage(Unknown Source)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                          at android.os.Looper.loop(Looper.java:194)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5706)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

The lines at which it is pointing are:

  PlaceLikelihood placeLikelihood = likelyPlaces.get( 0 );
result.setResultCallback( new ResultCallback<PlaceLikelihoodBuffer>() {

可能likelyPlaces没有条目,这可以解释为什么调用likelyPlaces.get( 0 )会引发异常的原因。

IllegalArgumentException was thrown to indicate that a method has been passed an illegal or inappropriate argument.

Try to implement void setPriority (int newPriority) , First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException. Otherwise, the priority of this thread is set to the smaller of the specified newPriority and the maximum permitted priority of the thread's thread group.

Here's a related ticket experiencing IllegalException: Android Google Maps V2 giving me IllegalStateException

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM