使用ListView的片段:setAdapter上的NullPointerException

时间:2014-10-10 17:44:59

标签: android listview android-fragments android-listview

我有问题,当我想在ListView上设置适配器时,我得到一个NullPointExeption。 在我使用ListFragment和一个简单的适配器扩展Fragment之前,问题是,我在这个活动中有3个片段都使用ListViews并且我得到显示错误(在片段中显示错误的列表)。所以我决定在Listview上设置每个Fragment自己的ID,但现在它不起作用。

错误listview.setAdapter(适配器):

  

java.lang.NullPointerException at   de.resper.e2cast.MainFragmentLive.onCreateView(MainFragmentLive.java:46)

片段:

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

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

import de.resper.e2cast.classes.globalBox;
import de.resper.e2cast.helper.getXml;
import de.resper.e2cast.helper.parseXml;

public class MainFragmentLive extends android.support.v4.app.Fragment {

    private List<String> bouquetListString;
    private ArrayAdapter<String> adapter;
    private globalBox activeBox;
    private ListView listview;

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_live, container, false);
        activeBox = ((globalBox) getActivity().getApplicationContext());
        bouquetListString = new ArrayList<String>();
        bouquetListString.add("loading...");
        if(activeBox.isInit()){
            if(activeBox.getBouquets().size() > 0 && activeBox.getBouquets().get(2).size() > 0){
                bouquetListString = activeBox.getBouquets().get(2);
            }else{
                Log.d("Load Bouquet", "XML");
                getBouquetBox();
            }
        }
        listview = (ListView) getActivity().findViewById(R.id.listLive);
        adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, bouquetListString);
        listview.setAdapter(adapter);

        ImageButton reloadBouquet = (ImageButton) view.findViewById(R.id.reloadBouquet);
        reloadBouquet.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View arg0) {
                getBouquetBox();
            }
        });

        setHasOptionsMenu(true);
        return view;
    }


    public void getBouquetBox(){
        getXml.DownloadCompleteListener dcl = new getXml.DownloadCompleteListener() {
            @Override
            public void onDownloadComplete(String result) {
                bouquetListString.clear();
                String [] tags = {"e2servicereference", "e2servicename"};
                List<List<String>> bouquetsList = parseXml.parseXmlByTag(result, tags);
                activeBox.addBouquets(bouquetsList);
                bouquetListString.addAll(activeBox.getBouquets().get(2));
                adapter.notifyDataSetChanged();
            }
        };
        Log.d("MyLogger", "XML Request GET BOUQUET");
        getXml downloader = new getXml(dcl);
        downloader.execute("http://" + activeBox.getIpPort() + "/web/getservices");
    }
}

片段XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="8dp">
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:text="@string/selectBouquet"
            style="@style/header1"/>
        <ImageButton
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:id="@+id/reloadBouquet"
            android:src="@drawable/ic_action_refresh"
            android:contentDescription="@string/search"
            android:layout_weight=".20"
            android:layout_gravity="bottom"/>
    </LinearLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"/>
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listLive" />
</LinearLayout>

1 个答案:

答案 0 :(得分:3)

使用view代替getActivity()来初始化ListView,因为ListView在Fragment布局中而不是Activity:

    listview = (ListView) view.findViewById(R.id.listLive);