Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ListView item set error Android

I have a listview that have 5 item in it if am trying to set error on the 4 th and 5th item .Then it is throwing a null pointer exception. Exception

08-22 13:34:49.523: E/AndroidRuntime(16952): FATAL EXCEPTION: main
08-22 13:34:49.523: E/AndroidRuntime(16952): java.lang.NullPointerException
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.example.iweenflightbookingpage.MainActivity$1.onClick(MainActivity.java:116)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.view.View.performClick(View.java:4091)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.view.View$PerformClick.run(View.java:17072)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Handler.handleCallback(Handler.java:615)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.os.Looper.loop(Looper.java:153)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at android.app.ActivityThread.main(ActivityThread.java:4987)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at java.lang.reflect.Method.invokeNative(Native Method)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at java.lang.reflect.Method.invoke(Method.java:511)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-22 13:34:49.523: E/AndroidRuntime(16952):    at dalvik.system.NativeStart.main(Native Method)

Code To set Error

click.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
   allValues = myAdapter.getAllValues();
                ArrayList<Integer> errorList = new ArrayList<Integer>();
                for(int i = 0; i < allValues.length; i++){
                   for(int j=0;j<staticPasengerList.length;j++){
                       if(allValues[i] == staticPasengerList[j]){
                            Log.d("Lop Count", ""+allValues[i]+"="+staticPasengerList[j]);

                            // Add position to errorList
                            errorList.add(i);
                        }
                   }

                }
                 myAdapter.setErrorList(errorList);
                 myAdapter.notifyDataSetChanged();
          }

        }
    }

BaseAdapter Class

class data extends BaseAdapter {
    String[] Title;
    Activity activity;

    public data (MainActivity mainActivity, String[] text) {
        Title = text;
        activity = mainActivity;
    }

    public String[] getAllValues() {
        return Title;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View row ;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        title.setText(Title[position]);
        return (row);
    }

This exception is occuring in case of 4 and 5 will be the value of i. Please help me to resolve the issue


1 Answers

You should consider that getChildAt(4) may return null .

you can setError() in your adapter's method getView() where position will be 4 . . .

EDITED:

class data extends BaseAdapter {
    String [] passengersList;
    String[] Title;
    Activity activity;

    public data (MainActivity mainActivity, String[] text, String[] passengersList) {
        Title = text;
        activity = mainActivity;
        this.passengersList = passengersList;
    }

    public String[] getAllValues() {
        return Title;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        return Title.length;
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = activity.getLayoutInflater();
        View row ;
        row = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        TextView title;
        title = (TextView) row.findViewById(android.R.id.text1);
        if(position == 4){
           checkAndSetError(title);
        }
        title.setText(Title[position]);
        return (row);
    }

    private void checkAndSetError(TextView title, int position){
        allValues = this.Title;
        Log.d("this is my array", "arr: " + Arrays.toString(allValues));
        for(int i=0;i<allValues.length;i++){
           if(allValues[position] == this.passengerList[i]){
               Log.d("Lop Count", ""+allValues[i]+"="+i);
               title.setError("Please change the data");
          }
        }
    }
}

It Should look something like this.

like image 121
Jilberta Avatar answered Nov 28 '25 15:11

Jilberta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!