0

我想知道如何将 AsyncTask (成功)添加到以下代码中。我已经实现了 AsyncTask (据我所知 - 使用我在网上找到的信息)但是它一直在崩溃,我不确定为什么......非常感谢任何帮助!

爪哇:

import org.json.JSONException;
import org.json.JSONObject;

import com.nfc.linked.DatabaseHandler;
import com.nfc.linked.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);

    private class RegisterActivity extends AsyncTask<string, jsonobject="" string,=""> {
        @Override
        protected JSONObject doInBackground(linkingmanager.comze.com) {

        }
    // Importing all assets like buttons, text fields
    inputFullName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);

    // Register Button Click event
    btnRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String name = inputFullName.getText().toString();
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.registerUser(name, email, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    registerErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1){
                        // user successfully registred
                        // 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));
                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(), DashboardActivity.class);
                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);
                        // Close Registration Screen
                        finish();
                    }else{
                        // Error in registration
                        registerErrorMsg.setText("Error occured in registration");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    // Link to Login Screen
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    LoginActivity.class);
            startActivity(i);
            // Close Registration View
            finish();
        }
    });
}

日志猫:

03-16 21:55:23.746: D/AndroidRuntime(483): Shutting down VM
03-16 21:55:23.746: W/dalvikvm(483): threadid=1: thread exiting with uncaught exception (group=0x41604930)
03-16 21:55:23.746: E/AndroidRuntime(483): FATAL EXCEPTION: main
03-16 21:55:23.746: E/AndroidRuntime(483): java.lang.Error: Unresolved compilation problems: 
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error, insert "}" to complete ClassBody
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error on token(s), misplaced construct(s)
03-16 21:55:23.746: E/AndroidRuntime(483):  The nested type RegisterActivity cannot hide an enclosing type
03-16 21:55:23.746: E/AndroidRuntime(483):  Illegal modifier for the local class RegisterActivity; only abstract or final is permitted
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error on token(s), misplaced construct(s)
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error on token """", { expected
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error on token "=", delete this token
03-16 21:55:23.746: E/AndroidRuntime(483):  Syntax error on tokens, delete these tokens
03-16 21:55:23.746: E/AndroidRuntime(483):  The method doInBackground() of type RegisterActivity must override or implement a supertype method
03-16 21:55:23.746: E/AndroidRuntime(483):  at com.nfc.linked.RegisterActivity.<init>(RegisterActivity.java:34)
03-16 21:55:23.746: E/AndroidRuntime(483):  at java.lang.Class.newInstanceImpl(Native Method)
03-16 21:55:23.746: E/AndroidRuntime(483):  at java.lang.Class.newInstance(Class.java:1319)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.os.Looper.loop(Looper.java:137)
03-16 21:55:23.746: E/AndroidRuntime(483):  at android.app.ActivityThread.main(ActivityThread.java:5039)
03-16 21:55:23.746: E/AndroidRuntime(483):  at java.lang.reflect.Method.invokeNative(Native Method)
03-16 21:55:23.746: E/AndroidRuntime(483):  at java.lang.reflect.Method.invoke(Method.java:511)
03-16 21:55:23.746: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-16 21:55:23.746: E/AndroidRuntime(483):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-16 21:55:23.746: E/AndroidRuntime(483):  at dalvik.system.NativeStart.main(Native Method)
03-16 21:55:26.296: I/Process(483): Sending signal. PID: 483 SIG: 9
4

2 回答 2

1

很抱歉直言不讳。您对 Java 语法有基本的误解,当然对线程也有误解。有人可能会自行重新格式化您的代码以“工作”,但我认为您将被困在 Painville 很长一段时间而没有碰到书。

“doInBackground”方法中实际上没有代码。此外,该论点无效。我会继续,但说真的,在尝试 Android 代码之前,您需要基本的 Java 编码技能。与任何平台相同(首先需要基本的编码技能)。

于 2013-03-17T02:58:32.590 回答
0

拿出RegisterActivityonCreate()

有关更多信息,您可以参考本指南。

于 2013-03-17T03:52:02.230 回答