Nick Kahn
Nick Kahn

Reputation: 20078

getView using custom listview getting null exception

03-19 23:38:21.952: E/AndroidRuntime(400): FATAL EXCEPTION: main
03-19 23:38:21.952: E/AndroidRuntime(400): java.lang.NullPointerException
03-19 23:38:21.952: E/AndroidRuntime(400):  at net.website.custom_listview.Lazy_Adapter_Custom_Listview$1.onClick(Lazy_Adapter_Custom_Listview.java:75)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.view.View.performClick(View.java:2485)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.view.View$PerformClick.run(View.java:9080)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.os.Handler.handleCallback(Handler.java:587)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.os.Looper.loop(Looper.java:123)
03-19 23:38:21.952: E/AndroidRuntime(400):  at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 23:38:21.952: E/AndroidRuntime(400):  at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:38:21.952: E/AndroidRuntime(400):  at java.lang.reflect.Method.invoke(Method.java:507)
03-19 23:38:21.952: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 23:38:21.952: E/AndroidRuntime(400):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 23:38:21.952: E/AndroidRuntime(400):  at dalvik.system.NativeStart.main(Native Method)

i am not sure what is wrong in this below code but i am getting null exception(artist)

ps: textview does exists in the layout file.

 public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row_custom_listview, null);


        TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
        TextView link = (TextView)vi.findViewById(R.id.txtLink);


        // Setting all values in listview 
        artist.setText(song.get(Main_Activity_Custom_Listview.KEY_TITLE_ENGLISH));

        link.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        int duration = Toast.LENGTH_SHORT;


        TextView tx =(TextView)v.findViewById(R.id.txtLink); //null exception???
        TextView artist = (TextView)v.findViewById(R.id.artist); // null exception???

        Log.v("link>>", "onItemClick at position" + artist.getText());

        //Toast toast = Toast.makeText(PlayListActivity.this, tx.getText(), duration);
        //toast.show(); 
        }
    }); 
        return vi;
    }

Upvotes: 0

Views: 756

Answers (3)

semih
semih

Reputation: 841

public View getView(final int position, View convertView, ViewGroup parent) {
    .
    .
    .

    vi.findViewById(R.id.dots).setOnClickListener(new OnClickListener() {

       @Override
       public void onClick(View v) {

            MessagesActivity.list.getChildAt(((int) MessagesActivity.list.getItemIdAtPosition(position))-MessagesActivity.list.getFirstVisiblePosition()).findViewById(R.id.top_header).setBackgroundResource(R.layout.list_selector);

       }

    .
    .
    .
    }

maybe this can help you

works for me

Upvotes: 1

neevek
neevek

Reputation: 12138

The problem is in this line link.setOnClickListener(...., you are setting a OnClickListener for the TextView link, so that TextView will be the View parameter passed to your onClick callback, and you certainly can't get(findViewById) your artist view from it.

I think you intended to do vi.setOnClickListener(). Even that, I don't think what you intended to do is appropriate, since there's an onItemClick callback you can override for your ListView, you don't need to set OnClickListener for each of your listview items like what you are doing in the code snippet.

EDIT:

Take a look at AdapterView.OnItemClickListener.

And then you will be doing something like this:

OnItemClickListener onItemClickListener = new OnItemClickListener() {
    onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // handle click events for you list view here.
        // for the parameters, see the link above. 
    }
};
myListView.setOnItemClickListener(onItemClickListener);

Upvotes: 0

Shubhayu
Shubhayu

Reputation: 13552

I think you had mentioned something similar in another post. I had mentioned that you shouldn't be doing this. You need to read up on the onClick() functionality.

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row_custom_listview, null);

    //Make them final so that you can access them in your onClick()
    final TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name 
    final TextView tx =(TextView)vi.findViewById(R.id.txtLink); 
    TextView link = (TextView)vi.findViewById(R.id.txtLink);



    // Setting all values in listview 
    artist.setText(song.get(Main_Activity_Custom_Listview.KEY_TITLE_ENGLISH));

    link.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
        int duration = Toast.LENGTH_SHORT;

        Log.v("link>>", "onItemClick at position" + artist.getText());

        }
    }); 
    return vi;
}

Upvotes: 0

Related Questions