在尝试访问android sqlite数据库中的数据时需要帮助调试错误

时间:2015-02-11 01:19:40

标签: android sqlite

我试图通过使用我从列表中传入的ID来访问存储在我的数据库中的信息。

以下是我mainList课程中的代码:

studentList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {
            try {
                String selected =parent.getItemAtPosition(position).toString();
                Log.d("position: ", selected);
                Class page = Class.forName("com.example.ltss.dyslexia.app.StudentProfilePage");
                //Intent intent = new Intent(getApplicationContext(), StudentProfilePage.class);

                Intent intent = new Intent(MainList.this, StudentProfilePage.class);
                //Intent intent  = new Intent(MainList.this, page);
                intent.putExtra("id", selected);

                String size = String.valueOf(id);
                Log.d("id is: ", size);
                intent.putExtra(ID_EXTRA, String.valueOf(id));
                startActivity(intent);


            }catch(ClassNotFoundException e){
                e.printStackTrace();
            }
           /*Toast.makeText(getApplicationContext(),
            ((TextView) v).getText(), Toast.LENGTH_SHORT).show();*/
        }
    });

和我的studentProfilePage(我传递数据的地方):

public class StudentProfilePage extends Activity {
String passedVar = null;
private TextView id = null;

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

    setContentView(R.layout.student_profile_page);
    passedVar = getIntent().getStringExtra(MainList.ID_EXTRA);
    //id = (TextView)findViewById(R.id.id);
    final DatabaseHandler dyslexiaDb = new DatabaseHandler(this);
    //dyslexiaDb.openDatabase();
    String data[] = dyslexiaDb.retrieveDataFromStudents(passedVar);

以下是我在DatabaseHandler课程中的内容:

public String[] retrieveDataFromStudents(String id){
       String table_name = TABLE_STUDENTS;
       // Create query
       String selectQuery = "SELECT * FROM "+table_name+" WHERE studentId = "+id;
       dyslexiaDatabase = this.getReadableDatabase();
       // set up Studnet Cursor (sCursor)
       Cursor sCursor = dyslexiaDatabase.rawQuery(selectQuery, null);
       String [] data = null;
       int count = 0;
       String[] data2 = data;
    if(sCursor.moveToFirst()){
           do{
               data2[count] = sCursor.getString(count);     
               Log.d("info", data[count]);
           }while(sCursor.moveToNext());
       }
        dyslexiaDatabase.close();
       return data2;
   }

这是我得到的错误:

    02-11 01:09:14.689: E/AndroidRuntime(26180): FATAL EXCEPTION: main

    02-11 01:09:14.689: E/AndroidRuntime(26180): Process: com.example.ltssdyslexiaapp, PID: 26180
    02-11 01:09:14.689: E/AndroidRuntime(26180): java.lang.RuntimeException: 

    Unable to start activity     ComponentInfo{com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.StudentProfilePage}: java.lang.NullPointerException

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread.access$800(ActivityThread.java:135)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.os.Handler.dispatchMessage(Handler.java:102)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.os.Looper.loop(Looper.java:136)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread.main(ActivityThread.java:5001)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at java.lang.reflect.Method.invokeNative(Native Method)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at java.lang.reflect.Method.invoke(Method.java:515)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)

   02-11 01:09:14.689: E/AndroidRuntime(26180):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

   02-11 01:09:14.689: E/AndroidRuntime(26180):     at dalvik.system.NativeStart.main(Native Method)

   02-11 01:09:14.689: E/AndroidRuntime(26180): Caused by: java.lang.NullPointerException

   02-11 01:09:14.689: E/AndroidRuntime(26180):     at com.example.ltss.dyslexia.app.DatabaseHandler.retrieveDataFromStudents(DatabaseHandler.java:322)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at com.example.ltss.dyslexia.app.StudentProfilePage.onCreate(StudentProfilePage.java:26)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at 
android.app.Activity.performCreate(Activity.java:5231)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 

    02-11 01:09:14.689: E/AndroidRuntime(26180):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)

    02-11 01:09:14.689: E/AndroidRuntime(26180):    ... 11 more

    02-11 01:09:24.559: E/WindowManager(569): Starting window AppWindowToken{439fe3e0 token=Token{43152328 ActivityRecord{4279b590 u0 com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.StudentProfilePage t725}}} timed out

    02-11 01:09:25.199: E/WindowManager(569): Starting window AppWindowToken{43f5d3a8 token=Token{42940510 ActivityRecord{42782eb0 u0 com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.MainActivity t725}}} timed out

    02-11 01:09:27.011: E/InputDispatcher(569): channel '43420a88 com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!

    02-11 01:09:27.011: E/InputDispatcher(569): channel '434566b8 com.example.ltssdyslexiaapp/com.example.ltss.dyslexia.app.MainList (server)' ~ Channel is unrecoverably broken and will be disposed!

我目前正在使用适配器而不是CursorAdapter(直到我让CursorAdapter正常工作)我只需要使用通过的ID一次显示用户数据的页面我可以。

我做错了什么吗?

感谢您的帮助,您可以给我!

0 个答案:

没有答案