gpt4 book ai didi

java - 自定义 ListView Android 的 getView nullPointerException

转载 作者:行者123 更新时间:2023-11-29 21:37:57 24 4
gpt4 key购买 nike

我有 2 个自定义 ListView ,用于显示不同的类别。但是,当创建第二个 ListView 时,我在从 getView 返回时得到此 NullPointerException。我无法找出原因。

代码如下:

自定义列表适配器:

public class CustomListAdapter extends ArrayAdapter<String>{

Context context;
String[] stringList;
TextView medium, small;


public CustomListAdapter (Context con, String[] sL)
{
super(con, R.layout.my_list, R.id.mediumTextInList, sL);
context = con;

stringList = sL;


}

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



LayoutInflater inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View newRowView = inflator.inflate(R.layout.my_list, parent,false);


medium = (TextView) newRowView.findViewById(R.id.mediumTextInList);
small = (TextView) newRowView.findViewById(R.id.smallText);

medium.setText(stringList[position]);

return super.getView(position, newRowView, parent);
}

列表 Activity :

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

Bundle myBasket = new Bundle();

myBasket.putString("name", categoryName);

Intent myIntent = new Intent(myFirstList.this, mySecondList.class);

myIntent.putExtras(myBasket);
startActivity(intent);
}

堆栈跟踪:

07-28 17:04:46.551: E/AndroidRuntime(2290): FATAL EXCEPTION: main 
07-28 17:04:46.551: E/AndroidRuntime(2290): java.lang.NullPointerException
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
07-28 17:04:46.551: E/AndroidRuntime(2290): at com.umer.androiduetproject.CustomListAdapterForDeptAndSemesters.getView(CustomListAdapterForDeptAndSemesters.java:53)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ListView.makeAndAddView(ListView.java:1745)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ListView.fillDown(ListView.java:670)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ListView.fillFromTop(ListView.java:727)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.ListView.layoutChildren(ListView.java:1584)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.view.View.layout(View.java:7175)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.view.View.layout(View.java:7175)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.view.View.layout(View.java:7175)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
07-28 17:04:46.551: E/AndroidRuntime(2290): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)

问候

最佳答案

您不应该调用 super.getView,只需返回 newRowView。

关于java - 自定义 ListView Android 的 getView nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17908193/

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