错误上下文:android.content.Context

时间:2017-04-23 08:35:32

标签: java android android-studio

以下是代码:

    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.support.design.widget.Snackbar;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.app.Fragment;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.GridLayoutManager;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.android.volley.Request;
    import com.android.volley.Response;
    import com.android.volley.VolleyError;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Map;

    import com.facebook.ads.*;

    import static com.facebook.FacebookSdk.getApplicationContext;

    public class NearbyFragment extends Fragment implements Constants, SwipeRefreshLayout.OnRefreshListener {

        private static final String STATE_LIST = "State Adapter Data";
        private static final String STATE_LIST_2 = "State Adapter Data 2";

        Menu MainMenu;

        RecyclerView mRecyclerView;
        TextView mMessage, mDetails;
        ImageView mSplash;

        SwipeRefreshLayout mItemsContainer;

        LinearLayout mSpotLight, mPermissionSpotlight;

        Button mGrantPermission;

        private ArrayList<Item> itemsList;
        private ItemListAdapter itemsAdapter;

        private ArrayList<Profile> peopleList;
        private SearchListAdapter peopleAdapter;

        private AdView adView;

        private int itemId = 0;
        private int arrayLength = 0;
        private Boolean loadingMore = false;
        private Boolean viewMore = false;
        private Boolean restore = false;

        int pastVisiblesItems = 0, visibleItemCount = 0, totalItemCount = 0;

        private int distance = NEARBY_DIALOG_OPTION_1;      // im miles
        private int mode = 0;                               // view mode

        public NearbyFragment() {
            // Required empty public constructor
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

    //        setRetainInstance(true);

            setHasOptionsMenu(true);

            loadBannerAd();

        }

        public void loadBannerAd() {
            // Instantiate an AdView view
            adView = new AdView(this, "111111111111111_111111111111111", AdSize.BANNER_HEIGHT_50);

            // Find the Ad Container
            LinearLayout adContainer = (LinearLayout) adView.findViewById(R.id.banner_container1);

            // Add the ad view to your activity layout
            adContainer.addView(adView);

            adView.setAdListener(new com.facebook.ads.AdListener() {

                @Override
                public void onError(Ad ad, AdError error) {
                    // Ad failed to load.
                    // Add code to hide the ad's view
                }

                @Override
                public void onAdLoaded(Ad ad) {
                    // Ad was loaded
                    // Add code to show the ad's view
                }

                @Override
                public void onAdClicked(Ad ad) {
                    // Use this function to detect when an ad was clicked.
                }

                @Override
                public void onLoggingImpression(Ad ad) {

                }

            });

            // Request an ad
            adView.loadAd();
        }

enter image description here

在线上获得错误&#34;这,&#34; xxxxxxxxxxxxxxx_xxxxxxxxxxxxxxx&#34;,AdSize.BANNER_HEIGHT_50&#34;。 错误如图所示。头痛这个错误。想知道如何解决它。感谢。

1 个答案:

答案 0 :(得分:0)

你必须编写getActivity()。getApplicationContext()而不是这个