简体   繁体   中英

FATAL EXCEPTION: AsyncTask #1 - Android App crashes when clicking on spinner

I am trying to build an app for informing people who don't have access to information available on net. I have completed it 90%, all the bugs that were coming have been resolved. When I launch and click on spinner value my app crashes.

My log cat is:

10-20 20:38:57.298 13722-13742/trial.spinners E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab98af70
10-20 20:38:58.966 13722-13722/trial.spinners E/ANAME =: sector-18
10-20 20:38:59.001 13722-15968/trial.spinners E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-20 20:38:59.001 13722-15968/trial.spinners E/AndroidRuntime: Process: trial.spinners, PID: 13722
10-20 20:38:59.001 13722-15968/trial.spinners E/AndroidRuntime: java.lang.RuntimeException: An error occurred while executing doInBackground()
10-20 20:38:59.001 13722-15968/trial.spinners E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java:309)
10-20 20:38:59.001 13722-15968/trial.spinners E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)

public class ServerRequests {
ProgressDialog progressDialog;
public static final int CONNECTION_TIMEOUT = 15000;
public static final String SERVER_ADDRESS = "http://kamster.net16.net/";


public ServerRequests(Context context)
{
    progressDialog = new ProgressDialog(context);
    progressDialog.setCancelable(false);
    progressDialog.setTitle("Processing");
    progressDialog.setMessage("Please wait..");

}


public void fetchDataInBackground(Contact contact , GetUserCallback callback)
{
    progressDialog.show();
    new FetchDataAsyncTask(contact, callback).execute();


}

public class FetchDataAsyncTask extends AsyncTask<Void , Void , Contact>
{
    Contact contact;
    GetUserCallback callback;
    Contact returnedContact;

    public FetchDataAsyncTask(Contact contact , GetUserCallback callback)
    {
        this.contact = contact;
        this.callback = callback;
    }

    @Override
    protected Contact doInBackground(Void... voids) {
        ArrayList<NameValuePair> data_to_send = new ArrayList<NameValuePair>();
        data_to_send.add(new BasicNameValuePair("areaname" , contact.areaname));
        data_to_send.add(new BasicNameValuePair("distance" , contact.distance));

        HttpParams httpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpRequestParams , CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(httpRequestParams , CONNECTION_TIMEOUT);

        HttpClient client = new DefaultHttpClient(httpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADDRESS + "newInput.php");

       // Contact returnedContact = null;
        try {
            post.setEntity(new UrlEncodedFormEntity(data_to_send));
            HttpResponse httpResponse = client.execute(post);

            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity);

            try {
                JSONObject jsonObject = new JSONObject(result);
                returnedContact = null;

                if (jsonObject.length() == 0) {
                    returnedContact = null;

                } else {
                    // String areaname,distance;
                    //  areaname = null;
                    // distance=null;

                    // if(jsonObject.has("areaname"))
                    //  areaname = jsonObject.getString("areaname");
                    // if(jsonObject.has("distance"))
                    // distance =jsonObject.getString("distance");

                    // returnedContact = new Contact(areaname,distance);
                    returnedContact = new Contact(contact.areaname, contact.distance);

                }
            }catch(JSONException jE){
                jE.printStackTrace();
            }

        }
        catch(IOException e)
        {
            e.printStackTrace();
        }

        return returnedContact;
    }
    @Override
    protected void onPostExecute(Contact returnedContact) {
        progressDialog.dismiss();
        callback.done(returnedContact);
        super.onPostExecute(returnedContact);
    }

}
}

I think the problem is the Json data retrieved. You may refer to this post : Android sending data to php and receiving issue nullpointerexception

Hope it can help you !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM