1

I have two activities MainActivity and Additem

in MainActivity I have this method:

public void updateUI() {
    helper = new TaskDBHelper(MainActivity.this);
    SQLiteDatabase sqlDB = helper.getReadableDatabase();
    Cursor cursor = sqlDB.query(TaskContract.TABLE,
            new String[]{TaskContract.Columns._ID, TaskContract.Columns.TASK},
            null,null,null,null,null);

    listAdapter = new SimpleCursorAdapter(
            this,
            R.layout.task_view,
            cursor,
            new String[] { TaskContract.Columns.TASK},
            new int[] { R.id.taskTextView},
            0
    );

    ListView listView = (ListView)findViewById(R.id.list);
    listView.setAdapter(listAdapter);

}

It updates the tasklist on the Mainscreen.

But when I use updateUI(); in Additem, on saving the task it returns back but the app crashes en restarts itself.

As error I got this:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference

This is my code where I use updateUI() in Additem:

public void saveItem(View view){
    EditText editText = (EditText)findViewById(R.id.editText);
    String task = editText.getText().toString();
    Log.d("Additem", task);

    helper = new TaskDBHelper(Additem.this);
    SQLiteDatabase db = helper.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.clear();
    values.put(TaskContract.Columns.TASK, task);

    db.insertWithOnConflict(TaskContract.TABLE, null, values,
            SQLiteDatabase.CONFLICT_IGNORE);



    updateUI();


    finish();
}

Could someone help me with this problem?

Thanks in advance,

Kind regards, Selin

Pankaj
  • 7,908
  • 6
  • 42
  • 65
Selin Aksu
  • 17
  • 5
  • pls post a log(or check that, and you will know..) of the crash so it can be known what is causing it and why – Pararth Jul 13 '15 at 11:45
  • Thanks for your advice i edited my question :) – Selin Aksu Jul 13 '15 at 11:51
  • Your listView doesn't exist in the moment when you call the method. This is because the activity isn't active and has been recycled to save memory. You should never update the UI from outside of the activity. – adnan_e Jul 13 '15 at 11:53
  • Show wehere you call this function – yshahak Jul 13 '15 at 11:54
  • @AdnanElezovic thanks! but then when the app returns back to mainscreen it doensn't show the last version of the list. – Selin Aksu Jul 13 '15 at 11:56
  • How can you call `updateUI()` method without any object directly from another activity. – Pankaj Jul 13 '15 at 12:04
  • you putted updateUI() that refer to another activity layout??? – yshahak Jul 13 '15 at 12:05
  • @yshahak yes that's what I did :$ – Selin Aksu Jul 13 '15 at 12:07
  • Start the new activity with startActivityForResult(), and when the child activity finishes update your UI accordingly. – adnan_e Jul 13 '15 at 12:10
  • Why you need another activity at all? – yshahak Jul 13 '15 at 12:12
  • No, `startActivityForResult()` is not the best solution, you don't need any data back to the first activity. Simply, you call the `updateUI()` in the `onResume()` of the first activity, and you're done. – GVillani82 Jul 13 '15 at 12:13
  • He shouldn't update the UI if there is no need to, if the child activity was launched and instantly finished, there is no need to call updateUI(), thus the result should be set accordingly. This all heavily depends on what these activities are actually doing, he didn't provide any kind of info on that. – adnan_e Jul 13 '15 at 12:17
  • @yshahak because I need to add a task (with date, time, repeat etc) to the database, like in the Agenda app of Google. – Selin Aksu Jul 13 '15 at 12:18
  • @AdnanElezovic the MainActivity shows a list of the tasks (from the database) the Additem makes adds new task in the database and then returns back to MainActivity – Selin Aksu Jul 13 '15 at 12:25
  • 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) – Selvin Jul 13 '15 at 12:25
  • Your easiest fix would be, like SohailAziz said, to just put updateUI() in onResume() of your MainActivity. If you have an eye for the detail, then you could avoid unnecessary updates using the result method I described above. – adnan_e Jul 13 '15 at 12:27

2 Answers2

2

Your listview is null and when you call it from other activity [when the MainActivity is paused/stopped] you are actually doing null.setAdapter which is causing crash.

You should post some sort of event to MainActivity and update the list/adapter when MainActivity resumes. OR you can always refresh/update the adapter in onCreate/onResume of MainActivity.

SohailAziz
  • 8,034
  • 6
  • 41
  • 43
0

I would suggest you to initialize the listView and listAdapter in your MainActivity and in update UI just refresh your view by using

listAdapter.notifyDataSetChanged()
sam100rav
  • 3,733
  • 4
  • 27
  • 43