gpt4 book ai didi

android - 尝试获取全局 ArrayList 时获取 NullPointer

转载 作者:行者123 更新时间:2023-11-29 15:24:22 25 4
gpt4 key购买 nike

我上了 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");

为什么会给出一个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)

最佳答案

其实,

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();

关于android - 尝试获取全局 ArrayList 时获取 NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14498696/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com