简体   繁体   中英

How do I setup a customListView in android?

I have been following a tutorial but I think it is out of date, I have setup everything I think I need for the listView to work but when I run it, it says -

 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)

Here's my 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"/>

Here is my main code:

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

Here is my customListViewSetup class:

 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;
}
 }

thank you in advance!

Replace the lines

 TextView CMDlist = (TextView) convertView.findViewById(R.id.CMDnameItem);
ImageView CMDLaunchArrow = (ImageView) convertView.findViewById(R.id.LaunchArrow);

by

 TextView CMDlist = (TextView) coustomView.findViewById(R.id.CMDnameItem);
    ImageView CMDLaunchArrow = (ImageView) coustomView.findViewById(R.id.LaunchArrow);
LayoutInflater CMDlistInflater = LayoutInflater.from(getContext());
View coustomView = CMDlistInflater.inflate(R.layout.coustom_list_item, parent, false);

remove this line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM