无法从静态上下文Android引用

时间:2016-12-01 12:03:15

标签: android

我是Android的初学者,我正在创建一个电影应用程序,用户可以浏览电影并搜索评分最高或最受欢迎的电影。 我正在使用DetailsFragment来显示电影细节​​,我也使用可扩展的列表视图来显示电影的预告片和评论

我的问题是我在这一行上收到错误 listAdapter = new ExpandableListAdapter(this,listDataHeader,listDataChild);

说:这不能从静态上下文引用,我无法通过使用getActivity()或DetailsFragment来解决它。这个

很抱歉,如果我非常无能,但我搜索了,我无法理解如何修复它。感谢。

这里是DetailsFragment.java

package com.example.android.movies;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.example.android.movies.R.id.favoriteButton;
import static com.example.android.movies.R.id.iv_poster;


/**
 * A placeholder fragment containing a simple view.
 */
public class DetailsFragment extends Fragment{


    static Long id;
    String title, overview, poster, date;
    String vote;
    ImageView posterIV;
    TextView titleTV, dateTV, overviewTV, voteTV;
    static Context mContext;

    static ExpandableListAdapter listAdapter;
    static ExpandableListView expListView;
    static List<String> listDataHeader;
    static HashMap<String, List<Trailers>> listDataChild;

    static ArrayList<Trailers> arrTrailers;
    static ArrayList<Trailers> arrReviews;

    ToggleButton favorite;
    private MoviesDB db;

    Trailers t;


    public static DetailsFragment newInstance() {
        return new DetailsFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_details, container, false);

        // get the listview
        expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

        // receiving intent

        Intent gIntent = getActivity().getIntent();
        Movies movie = null;
        if (gIntent != null && gIntent.hasExtra("movie")) {
            movie = gIntent.getParcelableExtra("movie");


            id = movie.getMposter_id();

            title = movie.getMtitle();
            titleTV = (TextView) rootView.findViewById(R.id.tv_title);
            titleTV.setText(title);

            date=movie.getMdate();
            dateTV = (TextView) rootView.findViewById(R.id.tv_date);
            dateTV.setText(date);

            vote=String.valueOf(movie.getMvote());
            voteTV= (TextView) rootView.findViewById(R.id.tv_vote);
            voteTV.setText(vote);


            overview=movie.getMoverview();
            overviewTV = (TextView) rootView.findViewById(R.id.tv_overview);
            overviewTV.setText(overview);

            poster = movie.getMposter_path();
            posterIV = (ImageView) rootView.findViewById(iv_poster);
            Picasso.with(getActivity())
                    .load("http://image.tmdb.org/t/p/w185".concat(poster)).into(posterIV);

            favorite= (ToggleButton) rootView.findViewById(favoriteButton);

        }
        favorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                db.insert(id,title,poster,date, Double.parseDouble(vote),overview,t.getContent1());

            }
        });

        return rootView;
    }

    private static void settingAdapter() {
        // preparing list data
        prepareListData();

        listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

        // setting list adapter
        expListView.setAdapter(listAdapter);
    }

    /*
     * Preparing the list data
     */
    private static void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<Trailers>>();


        // Adding child data
        listDataHeader.add("Reviews");
        listDataHeader.add("Trailers");

        listDataChild.put(listDataHeader.get(0), arrReviews); // Header, Child data
        listDataChild.put(listDataHeader.get(1), arrTrailers);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mContext=getActivity();

        new TrailersFetcher(getActivity(), id).execute();
    }

    public static void updateTrailers(ArrayList<Trailers> trailers){

        arrTrailers = new ArrayList<>();
        for (int i = 0; i <trailers.size() ; i++) {
            arrTrailers.add(trailers.get(i));

        }
        new ReviewsFetcher(mContext, id).execute();
    }

    public static void updateReviews(ArrayList<Trailers> reviews){
        arrReviews = new ArrayList<>();

        for (int i = 0; i <reviews.size() ; i++) {
            arrReviews.add(reviews.get(i));

        }

        settingAdapter();
    }
}

2 个答案:

答案 0 :(得分:0)

Simlple OOP规则 - 静态上下文(方法,类等)中无法引用非静态变量/成员/对象。

<强> 为什么?

静态引用不需要初始化来调用,因此可能存在(大多数情况下)未初始化普通(非静态)成员/对象并且对该(静态)方法进行调用的情况。这将始终抛出NullPointerException(对于引用类型)。 因此,它在编译时检查非静态引用不是在静态上下文中进行的。

<强> 解..

创建static ExpandableList对象或

static删除private static void settingAdapter()

答案 1 :(得分:0)

我认为你不需要那么多静态声明。 修饰符'static'仅在常量变量声明中允许。

无法从静态上下文引用非静态变量

private static void settingAdapter() {
    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);
}

只需删除'静态':

  1. 来自方法:
    settingAdapter
    prepareListData
    更新预告片
    updateReviews

  2. 来自变量:
    上下文mContext
    ExpandableListAdapter listAdapter
    ExpandableListView expListView
    列表listDataHeader
    的HashMap&GT; listDataChild
    ArrayList arrTrailers
    ArrayList arrReviews