java Builder (android.content.Context) 在 builder 中不能应用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30321648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:54:09  来源:igfitidea点击:

Builder (android.content.Context) In builder cannot be appllied to?

javaandroid

提问by Johnathon Starge

I'm trying to implement Google Places API into my app and I'm currently getting this in my error log when trying to run the application.

我正在尝试将 Google Places API 实施到我的应用程序中,并且在尝试运行该应用程序时,我目前在错误日志中收到了此信息。

Error:(23, 28) error: no suitable constructor found for Builder(menu_1_fragment)
constructor Builder.Builder(Context,ConnectionCallbacks,OnConnectionFailedListener) is not applicable
(actual and formal argument lists differ in length)
constructor Builder.Builder(Context) is not applicable
(actual argument menu_1_fragment cannot be converted to Context by method invocation conversion)

Now i'm trying to implement Google maps into a fragment that I have created that will display my app. I also want to include places API, Do I need to create a separate class for google maps or can I include this code like i'm currently doing into my fragment class?

现在我正在尝试将 Google 地图实现到我创建的一个片段中,该片段将显示我的应用程序。我还想包含地点 API,我是否需要为谷歌地图创建一个单独的类,或者我可以像我目前正在做的那样将这段代码包含到我的片段类中吗?

    public class menu_1_fragment  extends Fragment {
    MapView mapView;
    GoogleMap map;

    private GoogleApiClient mGoogleApiClient;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.menu1_layout, container, false);
        //--Snippet
        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 0, this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }
}

The error is occurring on .Builder(this)

错误发生在 .Builder(this)

Edit: Modified code

编辑:修改后的代码

`package com.examples.blahblah.blahblah;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;


public class menu_1_fragment  extends Fragment implements GoogleApiClient.OnConnectionFailedListener {
    MapView mapView;
    GoogleMap map;

    private Context mContext;
    @Override
    public void onAttach(final Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }




    private GoogleApiClient mGoogleApiClient;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.menu1_layout, container, false);
        //--Snippet
        mGoogleApiClient = new GoogleApiClient
                .Builder(mContext )
                .enableAutoManage(this, 0, this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
        if (mGoogleApiClient != null)
            mGoogleApiClient.connect();
    }

    @Override
    public void onStop() {
        if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onStop();
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }
}
`

The new error is now occurring on

新的错误现在发生在

   .enableAutoManage(this, 0, this)

and telling me it's the wrong first argument type?

并告诉我这是错误的第一个参数类型?

回答by Mr. Borad

you need activitycontext. so try with this.

你需要activitycontext。所以试试这个。

in your Fragment class.

在您的 Fragment 类中。

private Context mContext
@Override
public void onAttach(final Activity activity) {
    super.onAttach(activity);
    mContext = activity;
} 
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.menu1_layout, container, false);
    //--Snippet
    mGoogleApiClient = new GoogleApiClient
            .Builder(mContext )
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    return v;
}

Hope this will help you.

希望这会帮助你。

Update :

更新 :

I had to remove .enableAutoManage(this,0,this);

我不得不删除 .enableAutoManage(this,0,this);

for more info, you can visit here

欲了解更多信息,您可以访问这里

回答by Blackbelt

your Fragmentis not implementing the interface needed. With thisas third parameter you are declaring that your Fragmentis an object of a class required by the method itself. The builder is also expecting a Contextobject as first parameter and not an instance of Fragment, which you can retrieve with getActivity()

Fragment没有实现所需的接口。使用this第三个参数,您声明您Fragment是方法本身所需的类的对象。构建器还需要一个Context对象作为第一个参数而不是 的实例Fragment,您可以使用它来检索getActivity()