Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 执行异步任务时应用程序崩溃_Android - Fatal编程技术网

Android 执行异步任务时应用程序崩溃

Android 执行异步任务时应用程序崩溃,android,Android,我的应用程序在执行异步任务时崩溃。我试图将数据从android发送到服务器,但我的应用程序在执行AsyncTask时崩溃 `输入=新输入(); input.execute() mainActivity=this; 纬度=“-6.711647”; 经度=“108.5413”` 公共类输入扩展了异步任务 { HashMap user=db.getUserDetails(); 字符串email=user.get(“email”); 串成功; @凌驾 受保护的void onPreExecute(){ s

我的应用程序在执行异步任务时崩溃。我试图将数据从android发送到服务器,但我的应用程序在执行AsyncTask时崩溃 `输入=新输入(); input.execute()

mainActivity=this;
纬度=“-6.711647”;
经度=“108.5413”`
公共类输入扩展了异步任务
{
HashMap user=db.getUserDetails();
字符串email=user.get(“email”);
串成功;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(MainActivity.this);
setMessage(“向服务器发送数据…”);
pDialog.setUndeterminate(假);
pDialog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…arg0){
字符串strEMAIL=email.toString();
字符串strNama=latitude.toString();
String strProdi=经度.toString();
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“电子邮件”,strEMAIL));
添加参数(新的BasicNameValuePair(“纬度”,strNama));
参数add(新的BasicNameValuePair(“经度”,strProdi));
JSONObject json=jParser.makeHttpRequest(url,“POST”,参数);
试一试{
success=json.getString(“success”);
}捕获(例外e){
Toast.makeText(getApplicationContext(),“错误”,
Toast.LENGTH_LONG).show();
}
返回null;
}
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
如果(成功等于(“1”))
{
Toast.makeText(getApplicationContext(),“kirim data Sukses!!!”,Toast.LENGTH\u LONG.show();
} 
其他的
{
Toast.makeText(getApplicationContext(),“kirim data Gagal!!!”,Toast.LENGTH\u LONG.show();
}
}
}
这是我的完整代码
当您在
onCreate()
中调用
new input()
时,我的日志猫引用
db
为空

解决方案:只需在调用
新输入()之前初始化
db


顺便说一句,从设计的角度来看,有很多问题,但这将解决(其中一个)崩溃问题。

1用小写字母开始类名是不好的做法。
input
应该是
input
,并且应该更具描述性

2数据库初始化应该在调用执行异步任务之前进行

3在使用AsyncTask之前,您需要真正理解它。为异步任务声明三种泛型类型

公共类输入扩展异步任务{

但是没有使用它们。doInBackground方法应该返回类型为
NameValuePair
JSONObject
的实例


AsyncTask的第一种通用类型是
params
,这是您打算传递给doInBackground方法的内容。您可以说它是
String
,但不要在
input.execute()中传递任何字符串
这现在不是问题,因为你还没有尝试使用
args0
,但是如果你尝试使用,你的应用程序会崩溃。我只是认为你应该先阅读相关内容,了解基本知识。这会帮你省去很多麻烦。干杯。

有什么例外?发布你的日志。不过这应该是另一个问题
    mainActivity = this;
    latitude = "-6.711647";
    longitude ="108.5413";`


public class input extends AsyncTask<String, String, String>
{

    HashMap<String, String> user = db.getUserDetails();
    String email = user.get("email");
    String success;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Sending Data to server...");
        pDialog.setIndeterminate(false);
        pDialog.show();
    }


    @Override
    protected String doInBackground(String... arg0) {
        String strEMAIL = email.toString();
        String strNama = latitude.toString();
        String strProdi = longitude.toString();


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", strEMAIL));
        params.add(new BasicNameValuePair("latitude", strNama));
        params.add(new BasicNameValuePair("longitude", strProdi));


        JSONObject json = jParser.makeHttpRequest(url, "POST", params);

        try {
            success = json.getString("success");

        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "Error",
                    Toast.LENGTH_LONG).show();
        }
        return null;
    }
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once done
        pDialog.dismiss();

        if (success.equals("1")) 
        {
            Toast.makeText(getApplicationContext(), "kirim data Sukses!!!", Toast.LENGTH_LONG).show();
        } 
        else 
        {
            Toast.makeText(getApplicationContext(), "kirim data Gagal!!!", Toast.LENGTH_LONG).show();

        }
    }
}
 db = new SQLiteHandler(getApplicationContext());
 input input = new input();