android - Fragment with ListView: NullPointerException on setAdapter -


i have problem, nullpointexeption when want set adapter on listview. before had fragment extended listfragment , simple adapter, works problem was, have 3 fragments in activity listviews , got display errors (shows wrong list in fragment). decided set every fragment own ids on listview doesnt work.

error listview.setadapter(adapter):

java.lang.nullpointerexception @ 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 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> 

use view instead of getactivity() initializing listview because listview inside fragment layout instead of activity :

    listview = (listview) view.findviewbyid(r.id.listlive); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -