带有 ListView 的片段:setAdapter 上的 NullPointerException [英] Fragment with ListView: NullPointerException on setAdapter

查看:16
本文介绍了带有 ListView 的片段:setAdapter 上的 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

错误 listview.setAdapter(adapter):

Error listview.setAdapter(adapter):

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

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>

推荐答案

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

Use view instead of getActivity() to initializing ListView because ListView is inside Fragment layout instead of Activity :

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

这篇关于带有 ListView 的片段:setAdapter 上的 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆