2014-02-15 72 views
3

我目前有3個片段,但我無法讓我的適配器工作。 當我調用lv.setAdapter(lbadapter)時,它返回一個空指針異常。 可能是什麼問題?Listview設置適配器片段nullpointerexception

在此先感謝

這裏是我的片段的一部分:

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



    profielen = new ArrayList<Profile>(); 
    new getLeaderboards().execute(url); 

    lv = (ListView) this.getActivity().findViewById(android.R.id.list); 
    lbadapter = new LeaderBoardAdapter(this.getActivity(), R.layout.item_layout, profielen); 


    return rootView; 
} 

public static class getLeaderboards extends AsyncTask<String, Void, JsonObject> { 


    @Override 
    protected JsonObject doInBackground(String... urls) { 

     JsonObject x = parseURL.GetJSON(urls[0]); 

     return x; 
    } 


    @Override 
    protected void onPostExecute(JsonObject obj) { 
     Gson gson = new Gson(); 
     JsonArray arr = obj.get("rows").getAsJsonArray(); 

     for (JsonElement el : arr) { 
      Profile pat = gson.fromJson(el , Profile.class); 
      profielen.add(pat); 
     } 

     lv.setAdapter(lbadapter); 


    } 

} 

這是我的leadlist.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_centerInParent="true" 
    android:divider="@null" 
    android:dividerHeight="20dp" > 
</ListView> 


</RelativeLayout> 

我的適配器:

public class LeaderBoardAdapter extends ArrayAdapter<Profile>{ 

LayoutInflater vi; 
List<Profile> profielen; 
Context mContext; 
int res; 
public LeaderBoardAdapter(Context context, int resource, 
     List<Profile> objects) { 
    super(context, resource, objects); 
    this.res = resource; 
    this.profielen = objects; 
    mContext = context; 
    vi = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 


    ProfileHolder holder = null; 
    if(convertView == null) 
    { 
     LayoutInflater inflater = ((Activity)mContext).getLayoutInflater(); 
     convertView = vi.inflate(R.layout.item_layout, parent, false); 
     convertView = inflater.inflate(res, parent, false); 

     holder = new ProfileHolder(); 
     holder.name = (TextView)convertView.findViewById(R.id.nameChar); 
     holder.pic = (ImageView)convertView.findViewById(R.id.imageChar); 
     holder.rank = (TextView)convertView.findViewById(R.id.ratingNumber); 

     convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (ProfileHolder)convertView.getTag(); 
    } 

    Profile p = profielen.get(position); 
    holder.name.setText(p.getName()); 
    holder.rank.setText(p.getRanking()); 

    return convertView; 

} 

public static class ProfileHolder{ 
    TextView name; 
    ImageView pic; 
    TextView rank; 
} 





} 
+0

嘗試初始化ListView後啓動AsyncTask –

+0

可以任何幫助http://stackoverflow.com/questions/28148618/listview-not-refreshing-after-click-on-button – 2015-01-27 13:23:39

回答

8
lv = (ListView) this.getActivity().findViewById(android.R.id.list); 

反而這樣做。由於列表視圖與您的Activity沒有直接關係。這是片段內封閉的視圖,其持有的ListView

lv = (ListView) rootView.findViewById(android.R.id.list); 
+0

感謝它的工作原理。順便說一下另一個問題,你是否需要爲每個片段分別設計一個佈局,還是一個佈局足夠了? – David

+0

這是基於你需要在片段內顯示的內容。 – Triode

+0

如果你需要在每個片段上使用相同的用戶界面,那麼你就可以正常工作。 –

1

可能是你需要做這樣的

lv = (ListView) rootView.findViewById(android.R.id.list); 

,而不是

lv = (ListView) this.getActivity().findViewById(android.R.id.list); 

假設列表視圖中片段。

+1

該答案已由我發佈。 – Triode

+0

@RajeshCP是的,可能我們在哪裏做同一時間... –

+0

如果你看一看時差,你可以縮小這個範圍。 – Triode

0

問題在於你初始化ListView的方式。如果您發現它屬於您的leadlist.xml文件。但是在初始化你打電話,

lv = (ListView) this.getActivity().findViewById(android.R.id.list); // this means your listView lv belongs to your MainActivity which actually doesn't. 

更改爲,

lv = (ListView) rootView.findViewById(android.R.id.list); 

,它應該工作。讓我知道它是怎麼回事。快樂編碼:)

相關問題