我上了3节课。 主类,globalVariable类和项类。
我尝试在主类中设置一个ArrayList。我想在项目课中读出来。
这就是我试过的:
主要:
for (int i = data.size()-1; i >= 0; i--){
if (str_tv.contains(data.get(i).getCaption())){
//TODO data.remove();
String str_i = String.valueOf(i);
checkedArray.add(str_i);
}
}
System.out.println("MainActivity");
global.setArrList(checkedArray);
System.out.println("size = "+global.getArrList().size());
global.setArrlList
在这里工作。另外global.getArrList
在这里工作(我得到我的尺寸)。
现在我尝试从项目类中获取它:
System.out.println("Item Class");
System.out.println("size = "+global.getArrList().size()); <--gives NullPointer
但是在这里,它不起作用,然后它给出了一个NullPointer。
这是我的全球课程:
ArrayList<String> arrList;
public ArrayList<String> getArrList() {
return arrList;
}
public void setArrList(ArrayList<String> arrList) {
this.arrList = arrList;
}
我确实检查过他是否可能先尝试ArrayList
而不是设置它。但是他在项目类中的System.out.println("MainActivity");
之前显示了01-24 11:02:04.755: E/AndroidRuntime(30097): FATAL EXCEPTION: main
01-24 11:02:04.755: E/AndroidRuntime(30097): java.lang.NullPointerException
01-24 11:02:04.755: E/AndroidRuntime(30097): at com.dlv.GewasBescherming3.ItemListAdapter.getView(ItemListAdapter.java:47)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.AbsListView.obtainView(AbsListView.java:2035)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.ListView.makeAndAddView(ListView.java:1772)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.ListView.fillDown(ListView.java:672)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.ListView.fillFromTop(ListView.java:732)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.ListView.layoutChildren(ListView.java:1625)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.AbsListView.onLayout(AbsListView.java:1865)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.View.layout(View.java:11278)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.View.layout(View.java:11278)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.View.layout(View.java:11278)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.View.layout(View.java:11278)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.View.layout(View.java:11278)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewGroup.layout(ViewGroup.java:4224)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.os.Looper.loop(Looper.java:137)
01-24 11:02:04.755: E/AndroidRuntime(30097): at android.app.ActivityThread.main(ActivityThread.java:4575)
01-24 11:02:04.755: E/AndroidRuntime(30097): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 11:02:04.755: E/AndroidRuntime(30097): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 11:02:04.755: E/AndroidRuntime(30097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-24 11:02:04.755: E/AndroidRuntime(30097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-24 11:02:04.755: E/AndroidRuntime(30097): at dalvik.system.NativeStart.main(Native Method)
。
为什么它会给出NullPointer?我错过了什么?
logcat的:
{{1}}
答案 0 :(得分:5)
实际上,
ArrayList<String> arrList;
是null
。
只需将其声明为ArrayList<String> arrList = new ArrayList<String>();
<强>更新强>
实际上,Main
和Item
类具有不同的全局对象,因此无法引用相同的arrList
。
我最好添加:
public static ArrayList<String> arrList = new ArrayList<String>();
Global class
中的并按照以下方式使用它:
Global.arrList.size();