2016-12-01 12 views
0

我是Android上的初學者,我正在創建一個電影應用程序,用戶可以在其中瀏覽電影並搜索最受歡迎或最受歡迎的電影。 我使用DetailsFragment顯示電影的細節,還我使用的可擴展列表視圖中顯示影片的預告片和評論無法從靜態上下文中引用Android

我的問題是,我在這條線得到一個錯誤 listAdapter =新ExpandableListAdapter(這一點,listDataHeader ,listDataChild);

,說:這不能從靜態上下文引用,我不能使用getActivity()或DetailsFragment.this

很抱歉,如果我是極端無能,但我搜索,我沒能解決它瞭解如何解決它。謝謝。

這裏是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(); 
    } 
} 
+1

你不能用靜態的背景下,因爲這會導致MemoryLeakException在你的應用程序 –

+0

爲什麼它是靜態的第一位? – yanivtwin

+0

其次,你已經創建了自己的ExpandableListAdapter ..即創建一個新的類,並使用BaseExpandableListAdapter擴展它,然後使用它..看看它http://androidexample.com/Custom_Expandable_ListView_Tutorial_-_Android_Example/index.php?view=article_discription&aid = 107 –

回答

0

Simlple OOP規則 - 非靜態變量/成員/對象不能在靜態上下文中引用(方法,類等)。

爲什麼?

靜態引用不需要初始化呼叫,因此可能存在如下情況,正常的(非靜態)部件/對象未初始化,呼叫到(靜態)方法制成的(大部分)。這將始終拋出NullPointerException(用於參考類型)。 因此,它在編譯時檢查,非靜態引用不是在靜態上下文中進行的。

解..

要麼使一個static對象的ExpandableList,或

private static void settingAdapter()除去static

0

我認爲你不需要太多的靜態聲明。 修飾符「靜態」只允許在常量變量聲明中使用。

非靜態變量這個不能從靜態上下文中引用。

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

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

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

只是刪除 '靜態':

  1. 從方法:
    settingAdapter
    prepareListData
    updateTrailers
    updateReviews

  2. 自變量:
    語境mContext
    ExpandableListAdapter listAdapter
    ExpandableListView expListView
    列表listDataHeader
    的HashMap> listDataChild
    的ArrayList arrTrailers
    的ArrayList arrReviews

相關問題