java.lang.IllegalStateException 添加 google 将 API 放入 android

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

我想通过使用 Google Places API 来检索当前位置,但是在添加它时应用程序崩溃了。 这是 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
    }
}

}

这是android清单

<?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>

这是日志猫

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.onResult(ChooseLanguage.java:104)
                                                                          at com.urjapawar.jagrukbharat.ChooseLanguage.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)

它指向的行是:

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

也许 likelyPlaces 没有条目,这可以解释为什么调用 likelyPlaces.get( 0 ) 会导致抛出异常。

抛出

IllegalArgumentException 表示方法已传递非法或不适当的参数。

尝试实现void setPriority (int newPriority),首先不带参数调用该线程的checkAccess方法。这可能会导致抛出 SecurityException。否则,此线程的优先级设置为指定的 newPriority 和线程的线程组的最大允许优先级中较小的一个。

这是一张遇到 IllegalException 的相关工单:Android Google Maps V2 giving me IllegalStateException