无法从字符串数组填充列表视图片段

问题描述 投票:0回答:0

我不确定我哪里错了。我之前使用数组来填充列表视图并且没有任何问题。现在我正在使用一个片段,我收到无法修复的错误。我登录以查找错误所在,应用程序仅在运行最后一条注释掉的行时崩溃。

我的错误消息是

Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
但是当我在字符串数组上执行 to string 时怎么会这样(我相信我之前没有片段就做过)。如果适配器有问题,请告知我应该在那里。

ListView 片段 public class ListviewFragment extends Fragment implements AdapterView.OnItemClickListener, View.OnClickListener { 私有链表链表; 私人 ListView listView;

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


    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.frag_listviewfragment, container, false);
    MainActivity mainActivity = (MainActivity) getActivity();

    LinkedList<ItemClass> items = mainActivity.alist;

    int size = items.size();
    int j = 0;
    String itemArray[ ] = new String[size];

    if(items == null){
        Log.d("UI thread", "SHITS EMPTY FAM " );
    }
    else{
        for(ItemClass i : items){

            Log.d("UI thread", "I am the UI thread " + i.getdescription());
            //Log.d("UI thread", "The size of items is: " + size);
            itemArray[j] = i.getTitle();
            Log.d("UI thread", "following was added to array: " + i.getTitle());
            Log.d("UI thread", "The size of the array is: " + itemArray.length);
        }
    }

    Log.d("UI thread", "The size of the array is STILL: " + itemArray.length);
    ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), R.layout.activity_listview, itemArray);
    if (adapter == null)
    {
        Log.e("MyTag","Adapter error");
    }
    listView = (ListView) v.findViewById(R.id.list);
    if(listView == null){
        Log.e("MyTag","listview error");
    }

    //listView.setAdapter(adapter);




    return v;
}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState)
{


    super.onViewCreated(view, savedInstanceState);

// ListView listView = (ListView) view.findViewById(R.id.list); // listView.setOnItemClickListener((AdapterView.OnItemClickListener) 这个);

    //ArrayList<ItemClass> newList = null;


}


@Override
public void onClick(View view) {

}

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

}

} // 列表视图片段结束

java android android-fragments android-adapter
© www.soinside.com 2019 - 2024. All rights reserved.