2013-03-17 57 views
0

我想知道如何將AsyncTask(成功)添加到以下代碼。我已經實施了AsyncTask(盡我所知 - 使用我在網上找到的信息),但它不斷崩潰,我不確定爲什麼...任何幫助,非常感謝!將異步任務添加到JSON崩潰活動

JAVA:

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(); 
     } 
    }); 
} 

的logcat:

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 
+0

我想你可能需要重新安裝Eclipse或ADT插件。它允許你運行這麼多的編譯錯誤,只要看一下堆棧跟蹤)。 – 2013-03-17 02:02:26

+0

它不允許我運行 - 我在運行它之前刪除了錯誤,以查看是否可以使其運行。 – user2161499 2013-03-17 02:11:54

+0

Eclipse和ADT運行正常:) – user2161499 2013-03-17 02:16:17

回答

1

對不起,我的拙鈍。你對Java語法有一個基本的誤解,當然還有線程的誤解。有人可能會自己重新格式化您的代碼以「工作」,但我認爲您將長時間卡在Painville中,而不會觸及書本。

沒有代碼實際上是在「doInBackground」方法中。而且,這個參數是無效的。我會繼續,但嚴重的是,在嘗試Android代碼之前,您需要基本的Java編碼技能。與任何平臺相同(首先需要基本的編碼技巧)。

+0

我真的可以使用一些幫助...即使我被困在painville(我在期限後面) – user2161499 2013-03-17 03:06:10

+0

@ user2161499沒有人可以幫助你,如果你不幫助你自己的基礎知識 – wtsang02 2013-03-17 03:06:55

+1

我有一個基本的理解 - 我只是試圖按照關於創建註冊屏幕的教程,但是當我從教程中導入代碼時 - 我得到了強制關閉問題。 http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/ – user2161499 2013-03-17 03:12:29