No matter what I seem to be trying to do with this list and trying to dynamically update it, when I click the button, it does not work and always throws NPE.
The code is below and I really welcome the help. I tried the request and I tried notifyDataSetChanged, but I will be honest, this is only my second project and the first that uses a database or list, and I will fight.
DatabaseHelper
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; import android.util.Log; public class DatabaseHelper extends SQLiteOpenHelper { private static final String TAG = DatabaseHelper.class.getSimpleName(); public static final String DATABASE_NAME = "reordermymeds.db"; public static final int DATABASE_VERSION = 3; public static final String TABLE_NAME = "Medicines"; public static final String C_ID = BaseColumns._ID; public static final String C_MED_NAME = "med_name"; public static final String TABLE_NAME_SURG = "Surgeries"; public static final String SURGERY_NAME = "sName"; public static final String SURGERY_TEL = "sTel"; public static final String SURGERY_MAIL = "sMail"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION);
and
Activities
import android.app.ListActivity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.SimpleCursorAdapter; import android.widget.Toast; public class pills extends ListActivity { public static final String C_MED_NAME = "med_name"; public SimpleCursorAdapter ladapter; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.pills); try { DatabaseHelper DBHelper= new DatabaseHelper(this); SQLiteDatabase db = DBHelper.getReadableDatabase(); Cursor cur = db.query("Medicines", null, null, null, null, null, null); startManagingCursor(cur); String [] columns = new String[] {C_MED_NAME}; int [] to = new int[] {R.id.meditem}; SimpleCursorAdapter ladapter = new SimpleCursorAdapter(this, R.layout.meditem, cur, columns, to); this.setListAdapter(ladapter); } catch (Exception e) {
and, of course, the last, but no less important, stack trace. I see a line with an error, but everything that I tried to use Google leads to the same. Can anyone show that I have to go stupidly wrong?
Stack trace
08-11 01:21:26.095: ERROR/AndroidRuntime(15327): FATAL EXCEPTION: main 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): java.lang.NullPointerException 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at com.asurya.reordmymeds.pills$1.onClick(pills.java:90) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.view.View.performClick(View.java:2485) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.view.View$PerformClick.run(View.java:9080) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.os.Handler.handleCallback(Handler.java:587) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.os.Handler.dispatchMessage(Handler.java:92) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.os.Looper.loop(Looper.java:130) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at android.app.ActivityThread.main(ActivityThread.java:3683) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at java.lang.reflect.Method.invokeNative(Native Method) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at java.lang.reflect.Method.invoke(Method.java:507) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 08-11 01:21:26.095: ERROR/AndroidRuntime(15327): at dalvik.system.NativeStart.main(Native Method) 08-11 01:21:26.107: WARN/ActivityManager(110): Force finishing activity com.asurya.reordmymeds/.pills
Unfortunately. It would help if I let you know everything, and not consider them to be myself :)
The line of code that calls NPE is:
ladapter.notifyDataSetChanged();
In addition, if I just comment out this line, the application works fine, but you need to update the Activity to see the updated data in the database. Thanks to everyone.