1

我上了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可以在这里工作(我得到我的尺寸)。

现在我尝试从 item 类中获取它:

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");在项目类中显示了前一个。

为什么它会给出一个 NullPointer?我错过了什么?

日志猫:

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)
4

1 回答 1

5

实际上,

ArrayList<String> arrList;null

只需声明它ArrayList<String> arrList = new ArrayList<String>();

更新:

实际上,MainItem都有不同的全局对象,所以它不能引用相同arrList的。

我最好补充一下:

public static ArrayList<String> arrList = new ArrayList<String>();

Global class像这样使用它:

Global.arrList.size();
于 2013-01-24T10:12:39.317 回答