0

主要目的是让我将我的应用程序与网络上的 mysql 数据库连接起来。数据以 JSON 格式收集.. 数据似乎来自日志猫,但我的应用程序在收到数据后不断崩溃.. 这是我从 logCat 得到的错误

    E/SpannableStringBuilder(3081): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-08 06:17:57.720: E/SpannableStringBuilder(3081): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-08 06:17:57.740: E/SpannableStringBuilder(3081): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-08 06:17:57.740: E/SpannableStringBuilder(3081): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
09-08 06:18:05.655: E/JSON(3081): {"tag":"login","success":1,"error":0,"uid":"20","user":{"name":"mash","email":"mafulun4real@gmail.com","created_at":"28\/04\/13 06:27:"}}n
09-08 06:18:05.665: E/AndroidRuntime(3081): FATAL EXCEPTION: main
09-08 06:18:05.665: E/AndroidRuntime(3081): java.lang.NullPointerException
09-08 06:18:05.665: E/AndroidRuntime(3081):     at com.nairation.rss.LoginActivity.onPostExecute(LoginActivity.java:70)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at com.nairation.rss.LoginActivity.onPostExecute(LoginActivity.java:1)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.os.AsyncTask.finish(AsyncTask.java:631)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.os.Looper.loop(Looper.java:137)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at android.app.ActivityThread.main(ActivityThread.java:5289)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at java.lang.reflect.Method.invoke(Method.java:525)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
09-08 06:18:05.665: E/AndroidRuntime(3081):     at dalvik.system.NativeStart.main(Native Method)

这是代码

package com.nairation.rss;

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

import com.nairation.library.DatabaseHandler;
import com.nairation.library.JSONParser;
import com.nairation.library.UserFunctions;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.widget.EditText;

public class LoginActivity extends AsyncTask<String, Void, Integer>{

    private ProgressDialog progressDialog;
    private int responsecode = 0;
    private JSONParser jparser;
    private String username;
    private String password;
    private Login activity;

    public LoginActivity(ProgressDialog progressDialog){
        this.progressDialog = progressDialog;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        progressDialog.show();
    }

    @Override
    protected Integer doInBackground(String... params) {
        // TODO Auto-generated method stub
        int count = params.length;
        if(count == 2){
            username = params[0];
            password = params[1];
            UserFunctions userfunc = new UserFunctions();
            JSONObject jp = userfunc.loginUser(username, password);
            try{
                if(jp.getString("success") != null){
                    String res = jp.getString("success");

                    if(Integer.parseInt(res) == 1){
                        //Dat means the use is successfully logged in
                        DatabaseHandler db = new DatabaseHandler(activity.getApplicationContext());
                        JSONObject juser = jp.getJSONObject("user");
                        userfunc.logoutUser(activity.getApplicationContext());
                        db.addUser(juser.getString("name"), juser.getString("email"), jp.getString("uid"), juser.getString("created_at"));
                        responsecode = 1;
                    }
                }
            } catch(NullPointerException e){
                e.printStackTrace();
            } catch(JSONException e){
                e.printStackTrace();
            }
        }else{
            responsecode = 0;
        }
        return responsecode;
    }

    @Override
    protected void onPostExecute(Integer responsecode) {
        // TODO Auto-generated method stub
        //super.onPostExecute(responsecode);
        EditText username = (EditText) activity.findViewById(R.id.loginname);
        EditText pword = (EditText) activity.findViewById(R.id.loginpass);

        if (responsecode == 1){
            username.setText("");
            pword.setText("");
            progressDialog.dismiss();
        }

        if (responsecode == 0){
            progressDialog.dismiss();
            activity.showLoginError(responsecode);
        }
    }

}
4

2 回答 2

2

首先,不要初始化你的AsyncTaskwithProgressDialog并没有错,但我认为你应该在Activity那里传递一个上下文。通过这种方式,您可以使用它Context来创建您ProgressDialog的视图并正确初始化您的视图。所以它应该看起来像这样:

private Context mContext;

public LoginActivity(Context context){
    this.mContext = context;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = new ProgressDialog(mContext);
    progressDialog.setMessage("Logging in ...");
    progressDialog.show();
}

@Override
protected void onPostExecute(Integer responsecode) {
    //super.onPostExecute(responsecode);
    EditText username = (EditText) ((Activity)mContext).findViewById(R.id.loginname);
    EditText pword = (EditText) ((Activity)mContext).findViewById(R.id.loginpass);

    // and etc ...
}

还有其他一些东西,我认为可以更好地使用AsyncTask:如果你在你的视图中操作一些视图onPostExecute并且你只在一个视图中这样做Activity,你应该创建你的作为你的ActivityLoginActivity的内部类。Login通过这种方式,将您EditText的 's 声明为您的全局变量,您Activity可以在onPostExecute. 如果您要AsyncTask在整个应用程序中的多个地方使用它,对我来说更好的选择是创建一个接口,例如:

public interface OnLoginRequestHandler(){
    public void onLoginHandler(int responseCode);
}

并在你onPostExecute刚刚开火mMyLoginHandler.onLoginHandler(mMyResponseCode);。你应该知道你必须OnLoginRequestHandler从你的实现Activity@Override onLoginHandler所以你可以得到结果并根据它正确更新你的 UI。

希望这可以帮助您了解哪个最适合您的目的。:)

于 2013-09-08T05:52:40.967 回答
0

Login正在初始化的类型的变量活动在哪里?如果 responseCode 为 0,则 onPostExecute 运行,您进入 if 语句并且活动尚未初始化,因此它为空。

于 2013-09-08T05:52:07.543 回答