com.google.android.gms.common.api.GoogleApiClient $ ConnectionCallbacks中的错误

时间:2015-07-02 15:21:57

标签: android google-maps

我是Android新手,目前我正在制作一张非常基本的Google地图。并得到这个错误。 有人可以帮我解决这个问题吗?

这是logcat中的错误

`07-02 10:49:20.882  25215-25215/com.example.prashant.nuhani_go      E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.prashant.nuhani_go, PID: 25215
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.prashant.nuhani_go/com.example.prashant.nuhani_go.seco ndLocationChoosen}:     java.lang.ClassCastException:  com.example.prashant.nuhani_go.secondLocationChoosen cannot be cast to   com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
            at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassCastException:     com.example.prashant.nuhani_go.secondLocationChoosen cannot be cast to com.google.android.gms.common.api.GoogleApiClient$ConnectionCallbacks
        at com.example.prashant.nuhani_go.secondLocationChoosen.onCreate(secondLocationChoosen.java:46)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278).....

我的java文件:

      package com.example.prashant.nuhani_go;
      import android.support.v7.app.ActionBarActivity;
      import android.os.Bundle;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.View;
      import android.widget.AutoCompleteTextView;
      import android.widget.Button;
      import android.widget.EditText;
      import com.google.android.gms.common.ConnectionResult;
      import com.google.android.gms.common.api.GoogleApiClient;
      import    com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
      import com.google.android.gms.location.places.Places;


     public class secondLocationChoosen extends ActionBarActivity {
private GoogleApiClient mGoogleApiClient;
private AutoCompleteTextView mAutocompleteView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second_location_choosen);

  // Checking the destination and starting location, they shouldn't be empty
   final EditText editText3 =(EditText)findViewById(R.id.editText3);
   final EditText editText4 =(EditText)findViewById(R.id.editText4);
        Button button2 = (Button)findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if( editText3.getText().toString().length() == 0 )
                editText3.setError( "This field cannot be empty !" );
            if( editText4.getText().toString().length() == 0 )
                editText4.setError( "This field cannot be empty !" );
        }
    });


    // Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage
    // functionality, which automatically sets up the API client to handle Activity lifecycle
    // events. If your activity does not extend FragmentActivity, make sure to call connect()
    // and disconnect() explicitly.
    mGoogleApiClient = new GoogleApiClient
            .Builder(this)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks((ConnectionCallbacks) this)
            .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
            .build();


    // Retrieve the AutoCompleteTextView that will display Place suggestions.
    //mAutocompleteView = (AutoCompleteTextView)
    //        findViewById(R.id.place);
    // Register a listener that receives callbacks when a suggestion has been selected
    //mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_second_location_choosen, menu);
    return true;
}

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

@Override
protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

我启用了google maps api,并且我还在清单文件中添加了我的密钥。

2 个答案:

答案 0 :(得分:2)

让您的活动实施 GoogleApiClient.ConnectionCallbacks GoogleApiClient.OnConnectionFailedListener

答案 1 :(得分:0)

删除GoogleAPIClient实例中的强制转换,并在您的课程中实现这些方法。

尝试在课堂上实现这些界面,你会看到你缺少的东西。 Android Studio会给你一些“没有实现x方法”的警告

您告诉ApiClient您的类的“此”当前实例实现了这两个接口,但实际上并未实现它们。因此ClassCastException。