我一直在关注一个教程,但我认为它已经过时了,我已经设置了我认为listView可以使用的所有内容,但是当我运行它时,它会说 -
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at youknow.smscontrol.jason.testexpandiblelistview.CoustmListViewSettup.getView(CoustmListViewSettup.java:29)
这是我的XML:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:textColor="@android:color/white"
android:id="@+id/CMDnameItem"
android:padding="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/item_launch"
android:id="@+id/LaunchArrow"
android:layout_alignBottom="@+id/CMDnameItem"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/CMDnameItem"/>
这是我的主要代码:
public void SettupIds(){
listView = (ListView) findViewById(R.id.listView);
}
public void SetupAndDisplayListView(){
String[] CMDlist = {"Launch App Settings", "Alarm Settings", "Lock Screen Settings", "Display Message Settings", "Take Picture Settings"};
ListAdapter CMDlistAdapter = new CustomListViewSetup(getApplicationContext(), CMDlist);
listView.setAdapter(CMDlistAdapter);
}
这是我的customListViewSetup类:
class CustomListViewSetup extends ArrayAdapter<String>{
public CustomListViewSetup(Context context, String[] CMDList){
super(context,R.layout.coustom_list_item ,CMDList);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater CMDlistInflater = LayoutInflater.from(getContext());
View coustomView = CMDlistInflater.inflate(R.layout.coustom_list_item, parent, false);
String CurrentCMDlistitem = getItem(position);
LINE 29>> TextView CMDlist = (TextView) convertView.findViewById(R.id.CMDnameItem);
ImageView CMDLaunchArrow = (ImageView) convertView.findViewById(R.id.LaunchArrow);
CMDlist.setText(CurrentCMDlistitem);
CMDLaunchArrow.setBackgroundResource(R.drawable.item_launch);
return coustomView;
}
}
提前谢谢你!
答案 0 :(得分:1)
替换
行 TextView CMDlist = (TextView) convertView.findViewById(R.id.CMDnameItem);
ImageView CMDLaunchArrow = (ImageView) convertView.findViewById(R.id.LaunchArrow);
通过
TextView CMDlist = (TextView) coustomView.findViewById(R.id.CMDnameItem);
ImageView CMDLaunchArrow = (ImageView) coustomView.findViewById(R.id.LaunchArrow);
答案 1 :(得分:0)
LayoutInflater CMDlistInflater = LayoutInflater.from(getContext());
View coustomView = CMDlistInflater.inflate(R.layout.coustom_list_item, parent, false);
删除此行