Android ArrayAdapter NullPointerException(但不为null)

时间:2017-03-16 19:11:28

标签: android listview arraylist nullpointerexception android-arrayadapter

我正在尝试创建与firebase的聊天,尝试将添加的用户添加到朋友ListView,但是当我放置.setAdapter()时,它表示我在.setAdapter行上有一个java.lang.NullPointerException。

package studio.brunocasamassa.superchat.fragments;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.google.firebase.database.DatabaseReference;

import java.util.ArrayList;

import studio.brunocasamassa.superchat.R;

import static android.R.layout.activity_list_item;
import static android.R.layout.simple_list_item_2;

/**
 * A simple {@link Fragment} subclass.
 */


public class ContatosFragment extends Fragment {

    private ListView listview_nomes;
    private ArrayList<String> arraylist_nomes;
    private ArrayAdapter<String> adapter_nomes = null;
    private DatabaseReference firebaseDatabase;


    public ContatosFragment() {
        // Required empty public constructor
    }


    public String insertContact(String nomeContato) {
        arraylist_nomes = new ArrayList<String>();
        arraylist_nomes.add(nomeContato);
        adapter_nomes = new ArrayAdapter<String>(getContext(),
                android.R.layout.simple_list_item_2,
                android.R.id.text2,
                arraylist_nomes);
        return nomeContato;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


        if (adapter_nomes == null) {
            insertContact("teste");
        }

        listview_nomes = (ListView) getActivity().findViewById(R.id.ListContatos);
        System.out.println(adapter_nomes);
> the problem:
        listview_nomes.setAdapter(adapter_nomes);


        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_contatos, container, false);

    }


}

我在另一个类中调用insertContact方法,如下所示:

      Contato contato = new Contato();
                                        contato.setidUser(idContact);
                                        contato.setEmail(emailContact);
                                        contato.setNome(usuarioContato.getNome());
            ContatosFragment contact = new ContatosFragment();
                                            contact.insertContact(usuarioContato.getNome());
System.out.println("nome usuario: "+usuarioContato.getNome());

如果有人知道,我不知道如何修复它 感谢

  
    

已更新

  

我刚刚更改了我的insertContact方法(因为问题看起来像是其中的ArrayAdapter变量):

public String insertContact(String nomeContato) {
    arraylist_nomes = new ArrayList<String>();
    arraylist_nomes.add(nomeContato);
    System.out.println("nomes_array: " + arraylist_nomes);

    adapter_nomes = new ArrayAdapter<String>(
            getContext().getApplicationContext(),
            android.R.layout.simple_list_item_2,
            android.R.id.text2,
            arraylist_nomes);

    return nomeContato;
}

2 个答案:

答案 0 :(得分:1)

您的onCreateView()应如下:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_contatos, container, false);
        if (adapter_nomes == null) {
            insertContact("teste");
        }

        listview_nomes = (ListView) v.findViewById(R.id.ListContatos);

        System.out.println(adapter_nomes);
        listview_nomes.setAdapter(adapter_nomes);
        return v;
}

您必须从当前布局视图中获取ListView,而不是从提供空引用的getActivity()获取

 listview_nomes = (ListView) v.findViewById(R.id.ListContatos);

答案 1 :(得分:0)

在onCreateVIew()

中执行此操作
View root = inflater.inflate(R.layout.fragment_contatos, container, false);
listview_nomes = (ListView) root.findViewById(R.id.ListContatos);
listview_nomes.setAdapter(adapter_nomes);


return root;

问题是

listview_nomes = (ListView) getActivity().findViewById(R.id.ListContatos);

尝试在acitivity布局中找到listview而不是片段布局。因此你得到一个空指针。您的列表视图引用为空。