I have the Problem, that I get a NullPointExeption when I want to set a adapter on my ListView.
Before I had the Fragment extended with ListFragment and a simple Adapter, that works but the problem was, that I have 3 Fragments in this activity all with ListViews and I got display errors (shows the wrong list in a fragment). So I decided to set for every Fragment own ids on the Listview but now it doesnt work.
Error listview.setAdapter(adapter):
java.lang.NullPointerException at
de.resper.e2cast.MainFragmentLive.onCreateView(MainFragmentLive.java:46)
Fragment:
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");
}
}
Fragment 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>
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…