1. AC Question's Avatar
    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
    03-29-2017 06:44 PM

Similar Threads

  1. Replies: 1
    Last Post: 03-29-2017, 07:26 PM
  2. Is Samsung giving Note 7 customers any discounts on the S8 or the Note 8?
    By AC Question in forum Samsung Galaxy S8 & S8+
    Replies: 2
    Last Post: 03-29-2017, 07:20 PM
  3. Replies: 0
    Last Post: 03-29-2017, 06:40 PM
  4. Replies: 0
    Last Post: 03-29-2017, 06:11 PM
  5. can I remove a widget without uninstalling the app?
    By AC Question in forum Ask a Question
    Replies: 1
    Last Post: 03-29-2017, 06:05 PM
LINK TO POST COPIED TO CLIPBOARD