[英]Null Pointer Exception when using CustomList Adapter
I am trying to create a list view inside a fragment. 我试图在一个片段中创建一个列表视图。 The array which is causing a null pointer exception is being taken from a separate class called "getData". 导致空指针异常的数组是从称为“ getData”的单独类中获取的。
To create the list view i am using the Custom List Adapter. 要创建列表视图,我正在使用“自定义列表适配器”。 It is only when I put the array into the custom list adapter that i get the error. 仅当我将数组放入自定义列表适配器中时,我才收到错误。
Fragment where error evolves: 错误发生的片段:
package com.example.testapp;
import android.os.Bundle;
public class FragmentA extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.fragment_a, container, false);
ListView listView = (ListView)V.findViewById(R.id.list);
Integer[] imageId = {
R.drawable.ic_launcher,
R.drawable.ic_launcher,
};
getData data = getData.getMyData();
CustomList adapter = new
CustomList(getActivity(), data.myArray, imageId); //This is where i put the array.
listView.setAdapter(adapter);
return V;
}
}
getData class (where the array is taken from): getData类(从中获取数组的类):
package com.example.testapp;
import java.io.BufferedReader;
public class getData{
private static getData _instance;
public String myArray[]; //Array set up
public static getData getMyData() //This is what the fragment calls to get the array.
{
if(_instance == null)
_instance = new getData();
return _instance;
}
public void runData(){
getData data = getData.getMyData();
data.myArray[0] = "test"; //Array given value
}
}
You are not setting up array you are just declaring it. 您不是在设置数组而是在声明它。
public String myArray[];
The below line will give NP because the array object is not initialized yet. 下一行将给出NP,因为尚未初始化数组对象。
data.myArray[0] = "test";
you can create array object like this. 您可以像这样创建数组对象。
public String myArray[] = new String[10];
Here is updated getData class, this is not 100% encapsulated and singleton but it will work. 这是更新的getData类,它不是100%封装和单例的,但可以使用。 learn more about encapsulating data and singleton. 了解有关封装数据和单例的更多信息。
package com.example.testapp;
import java.io.BufferedReader;
public class getData{
private static getData _instance;
public String myArray[] = new String[10]; //Array set up
public static getData getMyData() //This is what the fragment calls to get the array.
{
if(_instance == null)
_instance = new getData();
_instance.runData();
return _instance;
}
private void runData(){
this.myArray[0] = "test"; //Array given value
}
}
Change your GetData
to 将您的GetData
更改为
package com.example.testapp;
import java.io.BufferedReader;
public class GetData
{
private static GetData _instance;
public List<String> myArray = null;
public static GetData getMyData() //This is what the fragment calls to get the array.
{
if(_instance == null) _instance = new GetData();
return _instance;
}
private GetData()
{
myArray = new LinkedList<String>();
}
public void runData()
{
GetData data = GetData.getMyData();
myArray.add(0, "test"); //Array given value
}
}
Your array was not correctly initialized. 您的阵列未正确初始化。 If you want to use a String[]
array, you must specify a length, using new String[5]
for example. 如果要使用String[]
数组,则必须指定长度,例如使用new String[5]
。 But if you don't know the final length, I suggest you to use my above code. 但是,如果您不知道最终长度,建议您使用上面的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.