2016-04-01 12 views
0

私は非常に新しいアンドロイドプログラミングです。フラグメントを作成しましたが、ListViewAdapter内にmainactivityコンテキストを配置しようとしていますが、エラーが表示されます:android.app.activity can not can android.content.contextに適用されます なぜこれがListViewAdapterで起こっているのか必ずしも確かではありませんが、InputStreamの下では問題がないようです。フラグメント内のMainActivityContextを取得する際に問題が発生しました

import android.app.Fragment; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ListView; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 

import Model.Statistics; 
import util.DatabaseHelper; 

import static com.example.sheyda.scorestats.Constants.FIFTH_COLUMN; 
import static com.example.sheyda.scorestats.Constants.FIRST_COLUMN; 
import static com.example.sheyda.scorestats.Constants.FOURTH_COLUMN; 
import static com.example.sheyda.scorestats.Constants.SECOND_COLUMN; 
import static com.example.sheyda.scorestats.Constants.SIXTH_COLUMN; 
import static com.example.sheyda.scorestats.Constants.THIRD_COLUMN; 

public class TopFragment extends Fragment { 
private InputStreamReader fileReader; 
private BufferedReader buffReader; 
private ArrayList<HashMap<String, String>> list; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View v = inflater.inflate(R.layout.layout_f1, container, false); 
    ListView listview = (ListView) v.findViewById(R.id.listView_SID); 
    try { 
     populateLV("stats.txt"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    ListViewAdapter adapter = new ListViewAdapter(getActivity().getBaseContext(), list); 
    listview.setAdapter(adapter); 
    return v; 
} 


public void populateLV(String fileName) throws IOException { 

    list = new ArrayList<HashMap<String, String>>(); 
    Statistics stat = new Statistics(); 


    InputStream descriptor = getActivity().getBaseContext().getAssets().open(fileName); 
    fileReader = new InputStreamReader(descriptor); 
    buffReader = new BufferedReader(fileReader); 
    String line; 
    // Counter stops at 40 because maximum number of students allowed is 40 
    int count = 0; 
    while ((line = buffReader.readLine()) != null && count <= 40) { 
     String[] l = line.split("~"); 
     HashMap<String, String> temp = new HashMap<String, String>(); 
     temp.put(FIRST_COLUMN, l[0]); 
     temp.put(SECOND_COLUMN, l[1]); 
     temp.put(THIRD_COLUMN, l[2]); 
     temp.put(FOURTH_COLUMN, l[3]); 
     temp.put(FIFTH_COLUMN, l[4]); 
     temp.put(SIXTH_COLUMN, l[5]); 
     list.add(temp); 
     count++; 
    } 
} 
} 
+1

アクティビティはコンテキストです –

答えて

1

利用getActivity()の代わりに、どこでもgetActivity().getBaseContext()

+0

ありがとうございます!その問題を解決しました。 – Ghazal

関連する問題