gpt4 book ai didi

java - Android Studio - 我无法弄清楚是什么导致我的 StringArray 返回 NullPointerException 因为它记录为已初始化和填充

转载 作者:行者123 更新时间:2023-12-01 23:20:44 24 4
gpt4 key购买 nike

尝试使用适配器,但它抛出空指针错误。

我不明白为什么,因为记录字符串数组的内容显示它已经初始化并且不为空。

MainActivity.java

    public class MainActivity extends AppCompatActivity {

ListView lvMyListView;
String[] items;

///tag for log
private static final String tag = "**IS4447**";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Resources res = getResources();

//init list view
lvMyListView = (ListView) findViewById(R.id.lvMyListView);

items = res.getStringArray(R.array.items);

Log.d(tag, items.toString());

ItemAdapter itemAdapter = new ItemAdapter(this, items);
lvMyListView.setAdapter(itemAdapter);




}
}

ItemAdapter.java

    public class ItemAdapter extends BaseAdapter {

String[] items;
LayoutInflater mInflater;

public ItemAdapter(Context c, String[] i){
i = items;

mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return items.length;
}

@Override
<resources>
public Object getItem(int i) {
return items[i];
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View convertView, ViewGroup parent) {

View v = mInflater.inflate(R.layout.my_list_view_detail, null);
TextView tvName = v.findViewById(R.id.tvName);

String itemName = items[i];

tvName.setText(itemName);

return null;
}
}

字符串.xml

    <resources>
<string name="app_name">My List View App</string>

<!-- My List View Data-->

<string-array name="items">
<item>Apple</item>
<item>Orange</item>
<item>Banana</item>
</string-array>

</resources>

最后是 logcat 输出。第一行显示字符串数组的 toString 输出不为 null。

    D/**IS4447**: [Ljava.lang.String;@c543c7b
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ie.aa.bis.is4447.mylistviewapp, PID: 12011
java.lang.RuntimeException: Unable to start activity ComponentInfo{ie.aa.bis.is4447.mylistviewapp/ie.aa.bis.is4447.mylistviewapp.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to get length of null array
at ie.aa.bis.is4447.mylistviewapp.ItemAdapter.getCount(ItemAdapter.java:25)
at android.widget.ListView.setAdapter(ListView.java:575)
at ie.aa.bis.is4447.mylistviewapp.MainActivity.onCreate(MainActivity.java:36)

最佳答案

只需在构造函数中将变量初始化从 i = items 切换为 items = i:

public ItemAdapter(Context c, String[] i) {
items = i;

mInflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

此外,请确保在 getView() 方法中返回 View,而不是 null

关于java - Android Studio - 我无法弄清楚是什么导致我的 StringArray 返回 NullPointerException 因为它记录为已初始化和填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58328743/

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