0

我正在制作一个在 SQLite 数据库中保存国家/地区列表的 Android 应用程序。初始屏幕显示列表。当您按下“编辑列表”时,一个新的活动开始,您可以在其中进入一个新的国家或删除整个列表。该应用程序运行良好,直到我在第二个活动中单击一个按钮。如果我进入我的新国家并单击添加,该应用程序会给我一条消息,它已停止,但该国家仍会添加到我的数据库中。我的全部删除按钮也是如此。我的 logcat 中出现错误,但它仍然删除了所有内容。这是我发生错误的类的代码:

public class EditList extends Activity
{
    private DBManager db;
    Cursor cursor;
    EditText editCountry, editYear;
    Button add, delete, back;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editCountry = (EditText)findViewById(R.id.editcountry);
        editYear = (EditText)findViewById(R.id.edityear);
        add = (Button)findViewById(R.id.add);
        delete = (Button)findViewById(R.id.delete);
        back = (Button)findViewById(R.id.backmain);

        //Open database and fill it with content, then close it
        db = new DBManager(this);
        db.openToWrite();

        add.setOnClickListener(buttonAddOnClickListener);
        delete.setOnClickListener(buttonDeleteAllOnClickListener);

        //------
        Log.i("Test", "back to main");

        //go back to the main list screen
        back = (Button)findViewById(R.id.backmain);
        back.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
                db.close();
                finish();
            }
        });
    }

    //insert new country button
    Button.OnClickListener buttonAddOnClickListener = new Button.OnClickListener()
    {  
        @Override
        public void onClick(View arg0) 
        {
            int year = Integer.parseInt(editYear.getText().toString());
            String country = editCountry.getText().toString();

            db.insert(year, country);

            updateList();

            //clear text fields after use
            editYear.setText(null);
            editCountry.setText(null);
        }
    };

    //delete all button
    Button.OnClickListener buttonDeleteAllOnClickListener = new Button.OnClickListener()
    {
        @Override
        public void onClick(View arg0) 
        {
            db.deleteAll();

            updateList();
        }
    };

    private void updateList()
    {
        cursor.requery();
    }
}

这是发生的 logcat 错误:

02-07 22:32:31.117: D/AndroidRuntime(2633): Shutting down VM
02-07 22:32:31.117: W/dalvikvm(2633): threadid=1: thread exiting with uncaught exception (group=0xb4e35908)
02-07 22:32:31.158: E/AndroidRuntime(2633): FATAL EXCEPTION: main
02-07 22:32:31.158: E/AndroidRuntime(2633): java.lang.NullPointerException
02-07 22:32:31.158: E/AndroidRuntime(2633):     at com.mypackage.msdassignment.EditList.updateList(EditList.java:90)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at com.mypackage.msdassignment.EditList.access$1(EditList.java:88)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at com.mypackage.msdassignment.EditList$1.onClick(EditList.java:68)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.view.View.performClick(View.java:4202)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.view.View$PerformClick.run(View.java:17340)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.os.Handler.handleCallback(Handler.java:725)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.os.Looper.loop(Looper.java:137)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at java.lang.reflect.Method.invoke(Method.java:511)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 22:32:31.158: E/AndroidRuntime(2633):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

你永远不会实例化你的光标。您声明它,然后使用它。那将失败。

于 2013-03-28T14:22:26.623 回答