0
package com.example.shubhraj.notesp;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import com.example.shubhraj.notesp.data.NoteContract.NoteEntry;

/**
 * {@link NoteCursorAdapter} is an adapter for a list or grid view
 * that uses a {@link Cursor} of pet data as its data source. This adapter knows
 * how to create list items for each row of pet data in the {@link Cursor}.
 */
public class NoteCursorAdapter extends CursorAdapter {

    /**
     * Constructs a new {@link NoteCursorAdapter}.
     *
     * @param context The context
     * @param c       The cursor from which to get the data.
     */
    public NoteCursorAdapter(Context context, Cursor c) {
        super(context, c,0);
    }
    /**
     * Makes a new blank list item view. No data is set (or bound) to the views yet.
     *
     * @param context app context
     * @param cursor  The cursor from which to get the data. The cursor is already
     *                moved to the correct position.
     * @param parent  The parent to which the new view is attached to
     * @return the newly created list item view.
     */
    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        // Inflate a list item view using the layout specified in list_item.xml
        LayoutInflater inflater = LayoutInflater.from(context);
        return inflater.inflate(R.layout.fragment_note_list, parent, false);
        //return LayoutInflater.from(context).inflate(R.layout.fragment_note_list, parent, false);
    }

    /**
     * This method binds the pet data (in the current row pointed to by cursor) to the given
     * list item layout. For example, the name for the current pet can be set on the name TextView
     * in the list item layout.
     *
     * @param view    Existing view, returned earlier by newView() method
     * @param context app context
     * @param cursor  The cursor from which to get the data. The cursor is already moved to the
     *                correct row.
     */
    @Override
    public void bindView(View view, Context context, Cursor cursor)
    {
        // Find individual views that we want to modify in the list item layout
        TextView nameTextView = (TextView) view.findViewById(R.id.note);

        // Find the columns of pet attributes that we're interested in
        int nameColumnIndex = cursor.getColumnIndex(NoteEntry.COLUMN_NOTE_CONTENT);

        // Read the pet attributes from the Cursor for the current pet
        String noteContent = cursor.getString(nameColumnIndex);

        // Update the TextViews with the attributes for the current pet
        nameTextView.setText(noteContent);
    }
}

EXCEPTION:-FATAL EXCEPTION: main Process: com.example.shubhraj.notesp, PID: 22020 java.lang.NullPointerException at com.example.shubhraj.notesp.NoteCursorAdapter.bindView(NoteCursorAdapter.java:70) The error is showing at nameTextView.setText(noteContent);

Shubhraj
  • 21
  • 5
  • Try debugger on bindView function to see what is null, then fix it – TuyenNTA Apr 23 '17 at 02:36
  • It would seem that the `` with ID `note` is not in the `fragment_note_list` layout you're inflating in `newView()`. Are you sure that's the right layout for your list items? – Mike M. Apr 23 '17 at 02:47
  • I am unable to find much information about the error in Debugger. The textView and all layouts are correctI have uploaded the project to github. Please check it if you can find it then please tell me:- www.github.com/Shubhraaaj/NoteSP – Shubhraj Apr 23 '17 at 02:54
  • You need to include the parts of your project necessary for a [mcve] in the question itself. However, I went ahead and had a look at your off-site repo, 'cause I don't wanna reopen this. The error is exactly as I stated. You're inflating [`fragment_note_list`](https://github.com/Shubhraaaj/NoteSP/blob/master/app/src/main/res/layout/fragment_note_list.xml) in `newView()`, but that is not the layout for the list _items_. That's the layout for the `Fragment`. In `newView()`, you need to inflate the layout that has the `` with ID `note`; the layout for the _items_, not the `Fragment`. – Mike M. Apr 23 '17 at 03:02
  • I would assume that should be [`fragment_noteitem`](https://github.com/Shubhraaaj/NoteSP/blob/master/app/src/main/res/layout/fragment_noteitem.xml), but neither of the ``s there have that ID. Typo, maybe? – Mike M. Apr 23 '17 at 03:05
  • Please show `fragment_note_list.xml`. – Code-Apprentice Apr 23 '17 at 04:30

0 Answers0