Android:致命异常:Asynctask #1

Android : FATAL EXCEPTION : Asynctask #1

我在运行代码时收到致命异常:AsyncTask #1。日志告诉我它是由 ArrayIndexOutOfBoundsException 引起的。

这是我项目的代码 登录活动

    package com.app.amusa;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;

public class loginactivity extends Activity {
    private EditText email, password;
    private SessionManager session;
    private JSONArray jsonArray;
    private JSONObject jsonObject;
    private Streamer Streamer;
    private String output,url,success;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);
        session = new SessionManager(getApplicationContext());
        Streamer = new Streamer();
        Toast.makeText(getApplicationContext(),
                "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG)
                .show();
        email = (EditText) findViewById(R.id.etEmail);
        password = (EditText) findViewById(R.id.etPass);
        TextView register = (TextView) findViewById(R.id.txtregister);
        register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {   
            Intent i = new Intent(getApplicationContext(), Registeractivity.class);
            startActivity(i);
        }
    });
}
public void login(View v) {
    if (email.getText().toString().trim().length() > 0
            && password.getText().toString().trim().length() > 0) 
    {
        new Masuk().execute();
    } 
    else 
    {
        Toast.makeText(getApplicationContext(), "Email and Password is Empty!!", Toast.LENGTH_LONG).show();
    }
}

public void dologin(){
    try {
        List<NameValuePair>nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("aksi", "login"));
        nameValuePairs.add(new BasicNameValuePair("email", email.getText().toString()));
        nameValuePairs.add(new BasicNameValuePair("password", password.getText().toString()));
        jsonArray = Streamer.postData(VariabelGlobal.url+"/user.php", nameValuePairs);
        jsonObject = jsonArray.getJSONObject(0);
        success = jsonObject.getString(success);
        JSONArray hasil = jsonObject.getJSONArray("login");
        if (success.equals("1")) {
            for (int i = 0; i < hasil.length(); i++) {
                JSONObject c = hasil.getJSONObject(i);                  
                String nama = c.getString("nama").trim();
                String email = c.getString("email").trim();
                session.createLoginSession(nama, email);
                Log.e("ok", " ambil data");
            }
        } else {
            Log.e("erro", "Can not get datas");
        }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
}
    }
private class Masuk extends AsyncTask<Object, Integer, Void> {
    private ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        dialog = new ProgressDialog(loginactivity.this);
        dialog.setMessage("Is Loging In");
        dialog.setIndeterminate(false);
        dialog.setCanceledOnTouchOutside(false);
        //dialog.setCancelable(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setProgress(0);
        dialog.show();
    }

    @Override
    protected Void doInBackground(Object... arg0) {
        if(arg0[0].equals("login")){
            output = "login";
            dologin();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        dialog.setProgress(progress[0]);
    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            dialog.dismiss();
            if(output.equals("login")){
                Toast.makeText(loginactivity.this, jsonObject.getString("status"), Toast.LENGTH_SHORT).show();
                Intent a = new Intent(loginactivity.this, Dashboard.class);
                startActivity(a);
                finish();
            }
        } catch (Exception e) {
        }

    }

}

}

这里是主播。这是 json 方法 POST

    package com.app.amusa;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;

import android.util.Log;

public class Streamer {
    private InputStream input;
    private HttpClient klien;
    private HttpGet req;
    private HttpPost post;
    private HttpResponse response;
    private BufferedReader dataInput;
    private HttpEntity httpEntity;
    private HttpParams params;

public JSONArray postData(String url, List<NameValuePair> data) {
    JSONArray jsonArray = null;
    try {
        klien = new DefaultHttpClient();
        params = klien.getParams();
        HttpConnectionParams.setTcpNoDelay(params, true);
        post = new HttpPost(url);
        post.setEntity(new UrlEncodedFormEntity(data));
        response = klien.execute(post);
        httpEntity = response.getEntity();
        input = httpEntity.getContent();
        dataInput = new BufferedReader(new InputStreamReader(input, "iso-8859-1"));
        StringBuilder sb = new StringBuilder();
        String baris = null;
        while ((baris = dataInput.readLine()) != null) {
            sb.append(baris + "\n");
        }
        dataInput.close();
        input.close();
        jsonArray = new JSONArray(sb.toString());
    } catch (Exception e) {
        Log.e("gagal postData", e.getMessage());
    }
    return jsonArray;
}

}

这是 php 用于连接到我的服务器的文件

if ($aksi == "login"){
$a = mysql_query("select * from data_orangtua where email ='$email' and password = '$password'");
$jml = mysql_num_rows($a);
if ($jml == 0){
    $data[] = NULL;
}else{
    $data['login'] = array();
    while ($dt = mysql_fetch_array($a))
    {
        $h['id_orangtua']   = $dt['id_orangtua'] ;
        $h['nama']          = $dt['nama'] ;
        $h['email']         = $dt['email'] ;
        $h['password']      = $dt['password'];
        array_push($data["login"], $h);
    }
    $data["status"] = "Anda Berhasil Login";
    $data['success']= '1';
}

} echo json_encode($data);

有人请帮助我。 谢谢

您调用 new Masuk().execute() 时不带参数。然后你试图在这里检查一个不存在的第一个参数:

@Override
protected Void doInBackground(Object... arg0) {
    if(arg0[0].equals("login")){
        output = "login";
        dologin();
    }
    return null;
}

为什么不 doLogin()?您已经确保只有在有名称和密码时才执行,那么为什么还要进行另一个无用的检查?