Search code examples
androidandroid-studioonitemclicklistener

onItemClickListener() crashes application


I want to tap on an entry on the listview on my Activity_main to view the details on Activity_View.

When I do so my application crashes.

Where is my coding wrong?

Activity_Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:weightSum="1">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="71dp"
        android:layout_weight="0.35">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1"
            android:focusableInTouchMode="true">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtSearch"
                android:layout_weight="0.98"
                android:hint="Search..." />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Search"
                android:id="@+id/btnSearch" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="1">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sort By Date"
                android:id="@+id/btnSortDate"
                android:layout_gravity="left"
                android:layout_weight="0.90" />

            <Button
                android:layout_width="174dp"
                android:layout_height="wrap_content"
                android:text="Sorty By Title"
                android:id="@+id/btnSortTitle"
                android:layout_gravity="right" />

        </LinearLayout>

    </LinearLayout>

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="252dp"
        android:id="@+id/listView"
        android:layout_weight="0.56" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create Entry"
        android:id="@+id/btnNewEntry"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="10dp" />

  </LinearLayout>

</RelativeLayout>

MainActivity.Java

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_CODE = 100;

private static List<Entry> entryList;

private ListView lvEntries;
private EntryListAdaptor EntryListadapter;
DatabaseHandler dbHandler;
Uri imageUri = Uri.parse("android.resource://unipersonalinc.mypersonalediary/drawable/download.jpg");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button newBtn = (Button) findViewById(R.id.btnNewEntry);
    lvEntries = (ListView) findViewById(R.id.listView);
    dbHandler = new DatabaseHandler(getApplicationContext());

    entryList = new ArrayList<>();
    EntryListadapter = new EntryListAdaptor();
    lvEntries.setAdapter(EntryListadapter);

    newBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent(MainActivity.this, Activity_Write.class), REQUEST_CODE);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        switch (requestCode) {
            case REQUEST_CODE:
                if (data != null) {
                    int id = dbHandler.getEntriesCount();
                    long time = System.currentTimeMillis();
                    SimpleDateFormat dayTime = new SimpleDateFormat("dd-MM-yyyy mm:ss");
                    String date = dayTime.format(new Date(time));
                    String title = data.getStringExtra("Title");
                    String passage = data.getStringExtra("Passage");
                    Entry newEntry = new Entry(id, title, passage, imageUri, date);
                    entryList.add(newEntry);
                    dbHandler.createEntry(newEntry);
                    EntryListadapter.notifyDataSetChanged();
                }
                break;
        }
    }
    lvEntries.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            int searchId = position - 1;
            Cursor cursor = dbHandler.getData(searchId);
            String entryTitle = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_TITLE));
            String entryPassage = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_PASSAGE));
            String entryDate = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_DATE));

            Bundle dataBundle = new Bundle();
            dataBundle.putString("title", entryTitle);
            dataBundle.putString("passage", entryPassage);
            dataBundle.putString("date", entryDate);

            Intent intent = new Intent(getApplicationContext(), Activity_View.class);
            intent.putExtras(dataBundle);
            startActivity(intent);
        }
    });
}


private class EntryListAdaptor extends ArrayAdapter<Entry> {

    private TextView tvTitle, tvDate;

    public EntryListAdaptor() {
        super(MainActivity.this, R.layout.listview_item, entryList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.listview_item, null);

        tvTitle = (TextView) convertView.findViewById(R.id.entryTitle);
        tvDate = (TextView) convertView.findViewById(R.id.entryDate);

        tvTitle.setText(entryList.get(position).getTitle());
        tvDate.setText(entryList.get(position).getDate());
        return convertView;
    }
  }
}

activity_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ivImage"
        android:layout_gravity="center"
        android:src="@drawable/download" />

    <TextView
        android:layout_width="367dp"
        android:layout_height="wrap_content"
        android:text="Testing"
        android:id="@+id/lblTitle"
        android:layout_marginTop="10dp"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="222dp"
        android:text="this is solely for the storyboard and may not be the     final interface of the application"
        android:id="@+id/lblPassage"
        android:layout_marginTop="10dp"
        android:layout_weight="0.82" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1/1/15"
        android:id="@+id/lblDate2"
        android:layout_gravity="right" />

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Edit"
            android:id="@+id/btnEdit"
            android:layout_marginTop="10dp"
            android:layout_gravity="left" />

        <Button
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="Back"
            android:id="@+id/btnBack"
            android:layout_gravity="right"
            android:layout_marginTop="10dp" />
    </LinearLayout>

  </LinearLayout>

</LinearLayout>

Activity_View.Java

public class Activity_View extends AppCompatActivity {

Button BackBtn = (Button) findViewById(R.id.btnBack);
Button EditBtn = (Button) findViewById(R.id.btnEdit);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_view);

    Bundle extras = getIntent().getExtras();
    String entryTitle = extras.getString("title");
    TextView title = (TextView) findViewById(R.id.lblTitle);
    title.setText(entryTitle);
    String entryPassage = extras.getString("passage");
    TextView passage = (TextView) findViewById(R.id.lblPassage);
    passage.setText(entryPassage);
    String entryDate = extras.getString("date");
    TextView date = (TextView) findViewById(R.id.lblDate2);
    date.setText(entryDate);

    BackBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
  }
}

Entry.Java

public class Entry {

public String _title, _passage;
public Uri _imageUri;
public String _date;
public int _id;

public Entry (int id, String title, String passage, Uri imageUri, String date) {
    _id = id;
    _title = title;
    _passage = passage;
    _imageUri = imageUri;
    _date = date;
}

public int getId() { return _id; }
public String getTitle() {
    return _title;
}
public String getPassage() { return _passage; }
public Uri getImageUri() { return  _imageUri; }
public String getDate() { return _date; }
}

DatabaseHandler.Java

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

public static final String DATABASE_NAME = "entriesManager",
TABLE_ENTRIES = "entries",
KEY_ID = "id",
KEY_TITLE = "title",
KEY_PASSAGE = "passage",
KEY_IMAGEURI = "imageUri",
KEY_DATE="date";


public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_ENTRIES + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE + " TEXT," + KEY_PASSAGE + " TEXT," + KEY_IMAGEURI + " TEXT" + KEY_DATE + "TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_ENTRIES);

    onCreate(db);
}

public void createEntry(Entry entry) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();


    values.put(KEY_ID,entry.getId());
    values.put(KEY_TITLE, entry.getTitle());
    values.put(KEY_PASSAGE, entry.getPassage());
    values.put(KEY_IMAGEURI, entry.getImageUri().toString());

    db.insert(TABLE_ENTRIES, null, values);
    db.close();
}

public Entry getEntry(int id) {
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.query(TABLE_ENTRIES, new String[] {KEY_ID, KEY_TITLE, KEY_PASSAGE, KEY_IMAGEURI}, KEY_ID + "=?", new String[] { String.valueOf(id) }, null, null, null, null);

    if (cursor != null)
        cursor.moveToFirst();

    Entry entry = new Entry(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), Uri.parse(cursor.getString(3)), cursor.getString(4));
    db.close();
    cursor.close();
    return entry;
}

public void deleteEntry(Entry entry) {
    SQLiteDatabase db = getWritableDatabase();
    db.delete(TABLE_ENTRIES, KEY_ID + "=?", new String[]{String.valueOf(entry.getId())});
    db.close();
}

public int getEntriesCount() {
    SQLiteDatabase db;

    db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_ENTRIES, null);
    int count = cursor.getCount();
    db.close();
    cursor.close();

    return count;
}

public int updataEntry(Entry entry) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE, entry.getTitle());
    values.put(KEY_PASSAGE, entry.getPassage());
    values.put(KEY_IMAGEURI, entry.getImageUri().toString());

    return db.update(TABLE_ENTRIES, values, KEY_ID + "=?", new String[]{String.valueOf(entry.getId())});
}

public List<Entry> getAllEntries() {
    List<Entry> entries = new ArrayList<Entry>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_ENTRIES, null);

    if (cursor.moveToFirst()) {
        do {
            Entry entry = new Entry(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), Uri.parse(cursor.getString(3)), cursor.getString(4));
            entries.add(entry);
        }
        while (cursor.moveToNext());
    }
    return entries;
}

public Cursor getData(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = db.rawQuery("select * from Entries where KEY_ID = " + id, null);
    res.moveToFirst();
    return res;
 }
}

LogCat

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: Process: unipersonalinc.mypersonalediary, PID: 5811 12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{unipersonalinc.mypersonalediary/unipersonalinc.mypersonalediary.Activity_View}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:2090)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at unipersonalinc.mypersonalediary.Activity_View.(Activity_View.java:17)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at java.lang.Class.newInstance(Native Method)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1067)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java) 

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)  12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)

12-09 06:16:55.140 5811-5811/unipersonalinc.mypersonalediary E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 


Solution

  • The error is in your getData() method. Replace

    Cursor res = db.rawQuery("select * from Entries where KEY_ID = " + id, null);
    

    with

    Cursor res = db.rawQuery("select * from Entries where " + KEY_ID + " = " + id, null);
    

    In your original method you where trying to lookup a Column named KEY_ID, which doesn't exist because KEY_ID is a constant naming your ID column. So you have to insert the value of KEY_ID in the SELECTstatement.

    For your new problem, when you get a Cursor you must iterate through its result to get the corresponding data, otherwise you'll get an Exception. For your code, try replacing

    Cursor cursor = dbHandler.getData(searchId);
    String entryTitle = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_TITLE));
    String entryPassage = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_PASSAGE));
    String entryDate = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_DATE));
    

    with

    Cursor cursor = dbHandler.getData(searchId);
    if(cursor.moveToFirst()){
        String entryTitle = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_TITLE));
        String entryPassage = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_PASSAGE));
        String entryDate = cursor.getString(cursor.getColumnIndex(dbHandler.KEY_DATE));
    }
    

    Also Check for position and searchId in onItemClick callback

    int searchId = position - 1;
    
    Cursor cursor = dbHandler.getData(searchId);
    

    this value should be >=0 ;

    For your next problem, change this

    public class Activity_View extends AppCompatActivity {
    
    Button BackBtn = (Button) findViewById(R.id.btnBack);
    Button EditBtn = (Button) findViewById(R.id.btnEdit);
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_view);
        ...
    

    to this:

    public class Activity_View extends AppCompatActivity {
    
    Button backBtn; 
    Button editBtn;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_view);
    
        backBtn = (Button) findViewById(R.id.btnBack);
        editBtn = (Button) findViewById(R.id.btnEdit);
        ...