-2

I'm going to put some (testing) listview on my fragment but the app was crashed instantly. This is the code.

public class ListPassword extends Fragment {

private String accountID , masterUsername;
private ListView lv;
private TextView masterUsernameTXT;
private String[][] showingData;
String[] title = { "a" , "b" , "c" , "d" , "e" };
String[] description = { "aaa" , "bbb" , "ccc" , "ddd" , "eee" };
String[] updateDate = { "000" , "111" , "222" , "333" , "444" };
int recordRow = 0;

public static ListPassword newInstance() {
    ListPassword fragment = new ListPassword();
    return fragment;
}

public ListPassword() { }

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_listpassword, container, false);

    String[] title = { "a" , "b" , "c" , "d" , "e" };
    String[] description = { "aaa" , "bbb" , "ccc" , "ddd" , "eee" };
    String[] updateDate = { "000" , "111" , "222" , "333" , "444" };

    lv = (ListView)rootView.findViewById(R.id.listview_password);
    SpinnerAdapt adapter = new SpinnerAdapt(getActivity(),title, description, updateDate);
    lv.setAdapter(adapter);

    return rootView;
}

I tried to solve it by reading the logcat and found out the following error.

FATAL EXCEPTION: main Process: com.example.new_ang_ja_kai_la.masterpassword, PID: 21591 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at com.example.new_ang_ja_kai_la.masterpassword.SpinnerAdapt.getCustomView(SpinnerAdapt.java:63)

and the SpinnerAdapt is as following (I coppied this from my friend)

public class SpinnerAdapt extends BaseAdapter {

    private Activity activity;
    private String[] title;
    private String[] description;
    private String[] updateDate;
    //
    public SpinnerAdapt(Activity activity, String[] title, String[] description, String[] updateDate) {
        this.activity = activity;
        this.title = title;
        this.description = description;
        this.updateDate = updateDate;
    }

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

    @Override
    public Object getItem(int i) {
        return i;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        return getCustomView(i, view, viewGroup);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        holder holder;
        if (convertView == null) {
//            LayoutInflater inflater = (activity).getLayoutInflater();
            LayoutInflater inflater = (LayoutInflater)activity.getSystemService
                    (Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_listview_storedpassword,null);
            holder = new holder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (holder) convertView.getTag();
        }
        holder.rowTitle.setText(title[position]);
        holder.rowDescription.setText(description[position]);
        holder.rowUpdateDate.setText(updateDate[position]);
        return convertView;
    }
    public class holder{
        private TextView rowTitle;
        private TextView rowDescription;
        private TextView rowUpdateDate;
        public holder(View v){
            TextView rowTitle= (TextView) v.findViewById(R.id.passwordTitle);
            TextView rowDescription = (TextView) v.findViewById(R.id.passwordDescription);
            TextView rowUpdateDate = (TextView) v.findViewById(R.id.passwordDescription);
        }
    }

}

When I have debugged this. It says that...

The error occured at the line "lv = (ListView)rootView.findViewById(R.id.listview_password);"

It showed that the value of lv is null when debugging completed.

I don't know how to solve it since I have already checked that R.id.listview_password is the right reference (By ctrl+click it) so, I could had put the pointer already.

I have checked out for solutions but nothing can help me.

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

You can see in your error message that your app is crashing on the line 63 of your SpinnerAdapter class. So please post the code of this class so we can help you.

Edit: Try it without the holder:

if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(activity);
    convertView = inflater.inflate(R.layout.item_listview_storedpassword, parent, false);
}

TextView rowTitle= (TextView) convertView.findViewById(R.id.passwordTitle);
TextView rowDescription = (TextView) convertView.findViewById(R.id.passwordDescription);
TextView rowUpdateDate = (TextView) convertView.findViewById(R.id.passwordDescription);

if(rowTitle != null) rowTitle.setText(title[position]);
if(rowDescription != null) rowDescription.setText(description[position]);
if(rowUpdateDate != null) rowUpdateDate.setText(updateDate[position]);

return convertView;
Chris
  • 406
  • 3
  • 8