在新活动中按下按钮时Android应用程序崩溃

时间:2013-03-28 14:16:06

标签: android database button logcat

我正在制作一个Android应用程序,其中包含SQLite数据库中的国家/地区列表。初始屏幕显示列表。当您按“编辑列表”时,将启动一项新活动,您可以在此处输入新国家/地区或删除整个列表。该应用程序完美运行,直到我在第二个活动中单击一个按钮。如果我进入我的新国家并单击添加,该应用程序会给我一条消息,表明它已停止,但该国家仍然添加到我的数据库中。我的删除所有按钮都是一样的。我在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)

1 个答案:

答案 0 :(得分:1)

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