-4
public class EntryListActivity extends ListActivity 
implements LoaderCallbacks<Cursor>{
    private SimpleCursorAdapter curAdpt;

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_entry_list);

        //ListView lview= (ListView) this.findViewById(android.R.id.list);

        //this.getLoaderManager().initLoader(0,  null,  (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);

        // For the cursor adapter, specify which  columns go into which views
        String[] fromColumns = {DiaryContract.EntryContract.COL_NAME_CONTENT};
        int[] toViews = {R.id.entry_content}; // The TextView in simple_list_item_1

        Uri entriesUri= Uri.parse("content://"+DiaryProviderContract.AUTHORITY + "/" + DiaryProviderContract.Entry.BASE_PATH) ;
       Cursor entryCursor= this.getContentResolver().query(entriesUri, null,  null, null, null);

       System.out.println("cursor" + entryCursor.getColumnCount());

       this.curAdpt = new SimpleCursorAdapter(this, android.R.layout.activity_list_item, entryCursor, fromColumns, toViews, (Integer) null);

        //lview.setAdapter(this.curAdpt);

        this.setListAdapter(this.curAdpt);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.entry_list, menu);
        return true;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub
        Uri entriesUri= Uri.parse("content://com.metizta.diary.provider/entries");

        return new CursorLoader(this, entriesUri,
                null, null, null, null);

    }
@Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        // TODO Auto-generated method stub
        Uri entriesUri= Uri.parse("content://com.metizta.diary.provider/entries");

        return new CursorLoader(this, entriesUri,
                null, null, null, null);

    }

    @Override
    public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
        // TODO Auto-generated method stub

        this.curAdpt.swapCursor(arg1);


    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub
        this.curAdpt.swapCursor(null);

    }
}

It throws nullPointerException in line

this.curAdpt = new SimpleCursorAdapter(this, android.R.layout.activity_list_item, entryCursor, fromColumns, toViews, (Integer) null);

Cœur
  • 37,241
  • 25
  • 195
  • 267
varuog
  • 3,031
  • 5
  • 28
  • 56
  • I'm confused - is it in the constructor, or is it in deferencing the parameters? – Andy Turner Mar 18 '15 at 13:08
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Jens Mar 18 '15 at 13:08
  • @fallenAngel because `NullPointerException`s like this are quite straightforward to diagnose. You just need to work out which of the things is `null`, which you can do with a debugger. – Andy Turner Mar 18 '15 at 13:12
  • I do understand nullpointerexception. but I dont understand how `this.curAdpt = new SimpleCursorAdapter(this, android.R.layout.activity_list_item, entryCursor, fromColumns, toViews, (Integer) null);` can throw null pointer exception – varuog Mar 18 '15 at 13:12
  • http://ideone.com/5qIkwl – Selvin Mar 18 '15 at 13:12
  • I'd guess something in this reference chain is null: `android.R.layout.activity_list_item`. – Andy Turner Mar 18 '15 at 13:14
  • i love trolling :) ... here is example what is going on there http://ideone.com/jl7es8 – Selvin Mar 18 '15 at 13:17
  • @fallenAngel just put there 0 if you don't wana any special flags – Selvin Mar 18 '15 at 13:20

2 Answers2

0

I suppose the problem is in (Integer) null, you should use other constructor for SimpleCursorAdapter: public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)

0

The last parameter when creating the SimpleCursorAdapter should be an int. Instead of casting a null to an Integer you should pass a zero to indicate you are not setting any of the flags.

this.curAdpt = new SimpleCursorAdapter(this, android.R.layout.activity_list_item, entryCursor, fromColumns, toViews, 0);

The constructor without the flags is not recommended and is deprecated because is enables auto-requery which results in Cursor queries being performed on the application's UI thread.

Richard Strand
  • 733
  • 8
  • 15