the app crashes when starting CursorLoader

  • Thread starter Thread starter AC Question
  • Start date Start date
A

AC Question

package com.example.android.pets;

import android.content.ContentValues;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.example.android.pets.data.PetContract;
import com.example.android.pets.data.PetDbHelper;

public class CatalogActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private static final int URI_LOADER = 0;
private PetDbHelper mDbHelper;
SQLiteDatabase db;
ListView lvItems;
PetCursorAdapter adapter;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_catalog);
lvItems = (ListView) findViewById(R.id.lstview);
View emptyView = findViewById(R.id.empty_view);
lvItems.setEmptyView(emptyView);

adapter = new PetCursorAdapter(this, null);
lvItems.setAdapter(adapter);

// Setup FAB to open EditorActivity
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View view) {
Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
startActivity(intent);
}
});
mDbHelper = new PetDbHelper(this);
db=mDbHelper.getWritableDatabase();
getLoaderManager().initLoader(URI_LOADER, null, this);

}

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu options from the res/menu/menu_catalog.xml file.
// This adds menu items to the app bar.
getMenuInflater().inflate(R.menu.menu_catalog, menu);
return true;
}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// User clicked on a menu option in the app bar overflow menu
switch (item.getItemId()) {
// Respond to a click on the "Insert dummy data" menu option
case R.id.action_insert_dummy_data:
insertdummy();
return true;
// Respond to a click on the "Delete all entries" menu option
case R.id.action_delete_all_entries:
// Do nothing for now
return true;
}
return super.onOptionsItemSelected(item);
}

private void insertdummy() {
ContentValues values=new ContentValues();
values.put(PetContract.PetEntry.COLUMN_PET_NAME, "toto");
values.put(PetContract.PetEntry.COLUMN_PET_BREED,"terrier");
values.put(PetContract.PetEntry.COLUMN_PET_GENDER, PetContract.PetEntry.GENDER_MALE);
values.put(PetContract.PetEntry.COLUMN_PET_WEIGHT, 7);
Uri URI_ID= getContentResolver().insert(PetContract.PetEntry.CONTENT_URI, values);
if(URI_ID==null)
Toast.makeText(getApplicationContext(), R.string.fail_save,Toast.LENGTH_LONG).show();
else
Toast.makeText(getApplicationContext(), R.string.success_save, Toast.LENGTH_LONG).show();

}
@override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
String[] projection = {PetContract.PetEntry._ID,
PetContract.PetEntry.COLUMN_PET_NAME,
PetContract.PetEntry.COLUMN_PET_BREED};
String selection = null;
String selectionArgs[] =null;
return new CursorLoader(getApplicationContext(), ContactsContract.Data.CONTENT_URI,
projection, selection, selectionArgs, null);
}
@override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
@override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}

The app is crashes when first lanuched and give me those error when using the CursorLoaders callback methods, I didn't know the line of code that makes the app crashes but I think the imports has a hand on it, please help?

github.com/NayirMicheal/petsapp this is the link to the entire app on github, this app creating database of pets and saving those pets into database and retriving them back from database and displaying the data retrieved into listview throught CursorLoader

drive.google.com/open?id=0Bx6oqNfteEuOUjQ4Vnc1bF9xcVk full logcat