Tiesna
Tiesna

Reputation: 21

App crash when execute asynctask

my app crash when execute asynctask. I try to send data from android to the server but my app crashes when execute AsyncTask ` input input = new input(); input.execute();

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

        }
    }
}

this my complete code http://pastebin.com/jRdxeQKG and this my logcat http://prntscr.com/7p3vbz

Upvotes: 1

Views: 396

Answers (2)

Ojonugwa Jude Ochalifu
Ojonugwa Jude Ochalifu

Reputation: 27256

1 Starting the name of your class with a lowercase letter is bad practice.input should be Input and should be a little more descriptive.

2 Your db initialization should come before the call to execute AsyncTask

3 You need to really understand AsyncTask before using it. You declare three generic types for your AsyncTask

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

but none of them is used. Your doInBackground method should be returning an instance of type NameValuePair or JSONObject.

The first generic type of an AsyncTask is the params, that is what you intend to pass to your doInBackground method. You say it's String but do not pass in any String in input.execute(); This is not a problem right now because you haven't attempted to use args0, but your app will crash if you try to. I just think you should really read about AsyncTask first and understand the basics.It will save you a whole lot of trouble.Cheers.

Upvotes: 0

Gil Vegliach
Gil Vegliach

Reputation: 3572

The reference db is null when you call new input() in onCreate().

Solution: Just initialize db before calling new input():

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

By the way, there are many issues from the design point of view, but this will solve (one of) the crash(es).

Upvotes: 1

Related Questions