0

每当我使用 Cursor 对象从数据库中检索数据时,我的应用程序每次都会崩溃。我必须为此采取什么预防措施?请帮忙

在这个例子中,我有一个简单的表单和一个提交按钮。提交后,将在微调器中显示火车名称列表。

错误

02-14 00:38:48.428: E/AndroidRuntime(456): FATAL EXCEPTION: main
02-14 00:38:48.428: E/AndroidRuntime(456): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.spinnerexample/com.example.spinnerexample.MainActivity}: java.lang.NullPointerException02-14 00:38:48.428: E/AndroidRuntime(456):     atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.os.Looper.loop(Looper.java:123)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-14 00:38:48.428: E/AndroidRuntime(456):  at java.lang.reflect.Method.invokeNative(Native Method)
02-14 00:38:48.428: E/AndroidRuntime(456):  at java.lang.reflect.Method.invoke(Method.java:507)
02-14 00:38:48.428: E/AndroidRuntime(456):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-14 00:38:48.428: E/AndroidRuntime(456):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-14 00:38:48.428: E/AndroidRuntime(456):  at dalvik.system.NativeStart.main(Native Method)
02-14 00:38:48.428: E/AndroidRuntime(456): Caused by: java.lang.NullPointerException
02-14 00:38:48.428: E/AndroidRuntime(456):  at com.example.spinnerexample.MainActivity.onCreate(MainActivity.java:42)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 00:38:48.428: E/AndroidRuntime(456):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-14 00:38:48.428: E/AndroidRuntime(456):  ... 11 more

我的光标对象 java 文件

    public class AddTrains {

    private static final int DB_VERSION = 1;
    private static final String DB_NAME = "mydb";
    private static final String TABLE_NAME = "trains";
    private static final String KEY_ID = "_id";
    private static final String KEY_TRAIN_NAME = "train_name";
    private static final String KEY_TRAIN_NUMBER = "train_number";
    private static final String KEY_HALT1 = "halt1";
    private static final String KEY_HALT2 = "halt2";
    private static final String KEY_HALT3 = "halt3";
    private static final String KEY_HALT4 = "halt4";
    private static final String KEY_HALT5 = "halt5";

    private DBHelper ourHelper;
    private final Context context;
    private SQLiteDatabase ourDatabase;

    private static final class DBHelper extends SQLiteOpenHelper {

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_TRAIN_NAME + " TEXT NOT NULL, " +
                KEY_TRAIN_NUMBER + " TEXT NOT NULL, " +
                KEY_HALT1 + " TEXT NOT NULL, " +
                KEY_HALT2 + " TEXT NOT NULL, " +
                KEY_HALT3 + " TEXT NOT NULL, " +
                KEY_HALT4 + " TEXT NOT NULL, " +
                KEY_HALT5 + " TEXT NOT NULL); "
                );

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
    }

    public AddTrains(Context c){
        context = c;
    }

    public AddTrains open() throws SQLException{

        ourHelper = new DBHelper(context);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }

    public void close(){

        ourHelper.close();
    }

    public long createEntry(String tname, int tnumber, String stop1,
            String stop2, String stop3, String stop4, String stop5) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues();
        cv.put(KEY_TRAIN_NAME, tname);
        cv.put(KEY_TRAIN_NUMBER, tnumber);
        cv.put(KEY_HALT1, stop1);
        cv.put(KEY_HALT2, stop2);
        cv.put(KEY_HALT3, stop3);
        cv.put(KEY_HALT4, stop4);
        cv.put(KEY_HALT5, stop5);
        return ourDatabase.insert(TABLE_NAME, null, cv);
    }

    public Set<String> getAllData() {
        // TODO Auto-generated method stub
        Set<String> set = new HashSet<String>();
        String selectQuery = "select * from " + TABLE_NAME;
        Cursor cursor = ourDatabase.rawQuery(selectQuery, null);
        Log.d("Count",cursor.getCount()+""); 
        if(cursor.getCount() > 0){
        if (cursor.moveToFirst()) {
        do {
        set.add(cursor.getString(1));
        } while (cursor.moveToNext());
        }
        }
        cursor.close();
        return set;
    }
}

主要活动文件

public class MainActivity extends Activity implements OnClickListener {

    EditText train_name, train_number, halt1, halt2, halt3, halt4, halt5;
    Button submit;
    Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        train_name = (EditText) findViewById(R.id.train_name);
        train_number = (EditText) findViewById(R.id.train_no);
        halt1 = (EditText) findViewById(R.id.halt1);
        halt2 = (EditText) findViewById(R.id.halt2);
        halt3 = (EditText) findViewById(R.id.halt3);
        halt4 = (EditText) findViewById(R.id.halt4);
        halt5 = (EditText) findViewById(R.id.halt5);
        spinner = (Spinner) findViewById(R.id.spinner1);

        submit.setOnClickListener(this);

        AddTrains db = new AddTrains(this);
        db.open();
        Set<String> set = db.getAllData();
           //Convert set into list
        List<String> list = new ArrayList<String>(set);
          //Sort Data Alphabetical order
        Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String lhs, String rhs) {
        return lhs.compareTo(rhs);
        }
        });
        ArrayAdapter adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View v, int position,long id) {
                 String name = parent.getItemAtPosition(position).toString();
               Toast.makeText(getApplicationContext(), "You Selected: "+ name, Toast.LENGTH_LONG ).show();
                   }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
                });
        db.close();
    }

    public void onClick(View v){
        switch(v.getId()){

        case R.id.button1:
            boolean DidItWorked = true;

            try{
                String tname = train_name.getText().toString();
                int tnumber = Integer.parseInt(train_number.getText().toString());
                String stop1 = halt1.getText().toString();
                String stop2 = halt2.getText().toString();
                String stop3 = halt3.getText().toString();
                String stop4 = halt4.getText().toString();
                String stop5 = halt5.getText().toString();

                AddTrains at = new AddTrains(this);
                at.open();
                at.createEntry(tname, tnumber, stop1, stop2, stop3, stop4, stop5);
                at.close();
            }catch(SQLiteException e){

            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
4

3 回答 3

1

多个问题:

  1. Delcared 按钮,设置一个侦听器,但从未在 xml 中创建或查找。

    Button submit;
    submit.setOnClickListener(this);
    

    你失踪了吗

    submit=(Button)findViewById(R.id.awesome_button);
    
  2. 你确定这是你的意思吗?

    while (cursor.moveToNext());
    
  3. 你查过42号线吗?请指出这条线在哪里

    02-14 00:38:48.428: E/AndroidRuntime(456): Caused by: java.lang.NullPointerException
    02-14 00:38:48.428: E/AndroidRuntime(456):  at com.example.spinnerexample.MainActivity.onCreate(MainActivity.java:42)
    
于 2013-02-13T19:45:21.920 回答
1

如果您解决了这个问题,最好将您的发现发布给其他人(比如我 :) 学习。

经常做的一件事是在任何有 catch 块的地方放一个 Log.d msg。例如,你有

public void onClick(View v){
    switch(v.getId()){

    case R.id.button1:
        boolean DidItWorked = true;

        try{
            String tname = train_name.getText().toString();
            // snip ...  
            at.createEntry(tname, tnumber, stop1, stop2, stop3, stop4, stop5);
            at.close();
        }catch(SQLiteException e){

        }

那么如果它在那里爆炸会发生什么?你得到一个堆栈跟踪转储 - 你的应用程序在用户身上崩溃。我建议这样做:

        }catch(SQLiteException e){
            Log.d ("OMG", "button1::onClick() caught exception: " + e.toString() ) ;
            // do nothing or some clean up ... but app will likely keep chugging along  
        }
于 2013-03-05T23:58:30.743 回答
0

您正在取消引用一个空指针。ourDatabase 为 null,或者 rawQuery 返回 null。检查第 42 行,如果其查询语句为数据库,则为空,否则为游标。如何解决它取决于你在那里找到什么。

于 2013-02-13T19:39:48.833 回答