注册在Android上崩溃

时间:2012-09-13 13:41:50

标签: android eclipse

我正在尝试注册我的用户,标签说成功。但我有一个错误,我不知道问题出在哪里。

这是我的代码:

// Registration in background
    protected String doInBackground(String... params) {
        UserFunctions userFunction = new UserFunctions();

        JSONObject json = userFunction.registerUser(name, email, password);
        try {
            String resu = json.getString(KEY_ERROR);
            if (Integer.parseInt(resu) == 3) {
                usernameExist = true;
            } else if (json.getString(KEY_SUCCESS) != null) {
                String res = json.getString(KEY_SUCCESS);
                if (Integer.parseInt(res) == 1) {
                    registered = true;
                    // user successfully registered
                    // Store user details in SQLite
                    // Database
                    DatabaseHandler db = new DatabaseHandler(
                            getApplicationContext());
                    JSONObject json_user = json.getJSONObject("user");

                    // Clear all previous data in
                    // database
                    userFunction.logoutUser(getApplicationContext());

                    db.addUser(json_user.getString(KEY_NAME),
                            json_user.getString(KEY_EMAIL),
                            json.getString(KEY_UID),
                            json_user.getString(KEY_CREATED_AT));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    // After completing background task Dismiss the progress dialog
    protected void onPostExecute(String file_url) {
        if (usernameExist == true) {
            registerErrorMsg.setText("Username already existed");
        } else if (registered == true) {
            registerErrorMsg.setText("");
            Intent homepage = new Intent(getApplicationContext(),
                    RegisterSuccessActivity.class);
            homepage.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homepage);
            finish();
        } else {
            // Error in registration
            registerErrorMsg.setText("Email address already existed");
        }
        // dismiss the dialog once done
        pDialog.dismiss();
    }

这是我的logcat:

09-13 13:39:45.455: E/JSON(868): {"tag":"register","success":1,"error":0,"uid":"5051e1dac29176.90742708","user":{"name":"user","email":"user@gmail.com","created_at":"2012-09-13 21:38:34","updated_at":null}}
09-13 13:39:46.225: E/AndroidRuntime(868): FATAL EXCEPTION: AsyncTask #2
09-13 13:39:46.225: E/AndroidRuntime(868): java.lang.RuntimeException: An error occured while executing doInBackground()
09-13 13:39:46.225: E/AndroidRuntime(868):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-13 13:39:46.225: E/AndroidRuntime(868):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.lang.Thread.run(Thread.java:856)
09-13 13:39:46.225: E/AndroidRuntime(868): Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1
09-13 13:39:46.225: E/AndroidRuntime(868):  at android.database.sqlite.SQLiteOpenHelper.onDowngrade(SQLiteOpenHelper.java:307)
09-13 13:39:46.225: E/AndroidRuntime(868):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:168)
09-13 13:39:46.225: E/AndroidRuntime(868):  at com.stts.sparetimetradingsystem.library.DatabaseHandler.addUser(DatabaseHandler.java:60)
09-13 13:39:46.225: E/AndroidRuntime(868):  at com.stts.sparetimetradingsystem.RegisterActivity$Register.doInBackground(RegisterActivity.java:150)
09-13 13:39:46.225: E/AndroidRuntime(868):  at com.stts.sparetimetradingsystem.RegisterActivity$Register.doInBackground(RegisterActivity.java:1)
09-13 13:39:46.225: E/AndroidRuntime(868):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-13 13:39:46.225: E/AndroidRuntime(868):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-13 13:39:46.225: E/AndroidRuntime(868):  ... 5 more

有人可以教我这样我可以学习吗?谢谢!

1 个答案:

答案 0 :(得分:0)

这一行

Caused by: android.database.sqlite.SQLiteException: Can't downgrade database from version 2 to 1

表示错误。您打开的SQLite是版本1,而设备版本为2.

您需要将SQLite的版本设置为2。 看到这个答案

https://stackoverflow.com/a/8223507/975959

此外,本教程可能有所帮助 http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/