0
votes

THis is myDB Class //Please take a look at public Cursor getplaces() --- Am i right or wrong? and How to code in main Activity class if i want to setListAdapter. Thank you for your help.

public class MyDB { 
    protected static final String TAG = "TAG";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private MyDBhelper mDbHelper;
public MyDB(Context context){   
    this.mContext = context;
    mDbHelper = new MyDBhelper(mContext);
}

public MyDB createDatabase() throws SQLException 
{
    try 
    {
        mDbHelper.createDataBase();
    } 
    catch (IOException mIOException) 
    {
        Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
        throw new Error("UnableToCreateDatabase");
    }
    return this;
}

public MyDB open() throws SQLException 
{
    try 
    {
        mDbHelper.openDataBase();
        mDbHelper.close();
        mDb = mDbHelper.getReadableDatabase();
    } 
    catch (SQLException mSQLException) 
    {
        Log.e(TAG, mSQLException.toString());
        throw mSQLException;
    }
    return this;
}


    public Cursor getplaces() 
    {
        Cursor c = mDb.query(Constants.DATABASE_NAME, null, null, null, null, null, null);
        return c;

    }

    public void close() 
    {
        mDbHelper.close();
    }

}

ListplaceActivity Class

public ListplaceActivity extends ListActivity {



    private static MyDB mDbHelper;
    String[] from = new String[] { Constants.TITLE_NAME };
    int[] to = new int[] {R.id.place_title};


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mDbHelper = new MyDB(this);
        Cursor c = mDbHelper.getplaces();
        setListAdapter(new SimpleCursorAdapter(this, 
                  R.layout.list_place, c, 
                  from, to));

        final ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view,int position, long id) {

                Intent newActivity = new Intent(view.getContext(), Place.class);     
                startActivity(newActivity);

            }
          });
    }

}

in LogCat show like this after i click to go to ListplacesActivity page it occur "Stopped Unexpectedly" and didn't show any list :

05-30 21:49:21.543: INFO/Process(17605): Sending signal. PID: 17605 SIG: 9
05-30 21:49:21.583: INFO/ActivityManager(59): Process com.ctg (pid 17605) has died.
05-30 21:49:21.593: INFO/WindowManager(59): WIN DEATH: Window{44de90a0 com.ctg/com.ctg.CTG paused=false}
05-30 21:49:21.674: INFO/ActivityManager(59): Start proc com.ctg for activity com.ctg/.CTG: pid=27207 uid=10028 gids={}
05-30 21:49:21.733: ERROR/gralloc(59): [unregister] handle 0x5fe9d8 still locked (state=40000001)
05-30 21:49:22.103: DEBUG/ddm-heap(27207): Got feature list request
05-30 21:49:22.265: INFO/UsageStats(59): Unexpected resume of com.ctg while already resumed in com.ctg
05-30 21:49:23.084: DEBUG/dalvikvm(27207): GC freed 801 objects / 58520 bytes in 216ms
05-30 21:49:23.433: DEBUG/dalvikvm(27207): GC freed 93 objects / 3632 bytes in 129ms
05-30 21:49:23.623: WARN/PhoneWindow(27207): Previously focused view reported id 16908298 during save, but can't be found during restore.
05-30 21:49:23.843: WARN/InputManagerService(59): Got RemoteException sending setActive(false) notification to pid 17605 uid 10028
05-30 21:49:24.103: INFO/ActivityManager(59): Displayed activity com.ctg/.CTG: 2489 ms (total 33589 ms)
05-30 21:49:29.013: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.ctg/.AttractionsListActivity }
05-30 21:49:29.085: DEBUG/AndroidRuntime(27207): Shutting down VM
05-30 21:49:29.085: WARN/dalvikvm(27207): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-30 21:49:29.094: ERROR/AndroidRuntime(27207): Uncaught handler: thread main exiting due to uncaught exception
05-30 21:49:29.114: ERROR/AndroidRuntime(27207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ctg/com.ctg.AttractionsListActivity}: java.lang.NullPointerException
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.os.Looper.loop(Looper.java:123)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at java.lang.reflect.Method.invokeNative(Native Method)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at java.lang.reflect.Method.invoke(Method.java:521)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at dalvik.system.NativeStart.main(Native Method)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207): Caused by: java.lang.NullPointerException
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at com.ctg.data.MyDB.getplaces(MyDB.java:82)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at com.ctg.AttractionsListActivity.onCreate(AttractionsListActivity.java:32)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-30 21:49:29.114: ERROR/AndroidRuntime(27207):     ... 11 more
05-30 21:49:29.144: INFO/Process(59): Sending signal. PID: 27207 SIG: 3
05-30 21:49:29.144: INFO/dalvikvm(27207): threadid=7: reacting to signal 3
05-30 21:49:29.174: INFO/dalvikvm(27207): Wrote stack trace to '/data/anr/traces.txt'
05-30 21:49:39.039: WARN/ActivityManager(59): Launch timeout has expired, giving up wake lock!
05-30 21:49:39.057: WARN/ActivityManager(59): Activity idle timeout for HistoryRecord{44eae320 com.ctg/.AttractionsListActivity}
1
And what's your problem? What do you expect to see (I guess a list with places XD)? What do you see instead? If it crashes, paste the logcat output.Cristian
i have no idea why it crashes..Please help meApril Smith
It crashes because a NullPointerException... if you do what dmon says it will fix your problem ^_^Cristian

1 Answers

3
votes

You didn't call MDbHelper.open(), so the variable mDb inside getPlaces() is null.