Android doInBackground致命期待Asynctask#1

时间:2015-03-30 13:58:35

标签: java android multithreading android-asynctask

我尝试使用json在后台工作后,我的应用程序崩溃了。我不知道为什么它不起作用。

类别:

  package com.example.olda.hledac;
  import android.app.ProgressDialog;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.AdapterView.OnItemClickListener;
  import android.widget.ListAdapter;
  import android.widget.ListView;
  import android.widget.SimpleAdapter;
  import android.widget.TextView;
  import android.app.ListActivity;

  import org.apache.http.NameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;

  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;


  public class vypis extends ListActivity {

private ProgressDialog pDialog;

JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> uzivatele;


private static String url = "http://lokace.4fan.cz/vypis_uzivatelu.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_WP_USERS = "wp_users";
private static final String TAG_ID = "ID";
private static final String TAG_USER_login = "user_login";

JSONArray uziv = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_vypis);
    ListView lv = getListView();
    uzivatele = new ArrayList<HashMap<String, String>>();
        new VypisUzivatele().execute();


        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                String ID = ((TextView) view.findViewById(R.id.ID)).getText()
                        .toString();

                Intent in = new Intent(getApplicationContext(),
                        MenuActivity.class);
                in.putExtra(TAG_ID, ID);
                startActivityForResult(in, 100);
            }
        });

    }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == 100) {
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }

}

 class VypisUzivatele extends AsyncTask<String, String, String> {

   @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(vypis.this);
        pDialog.setMessage("vypisování uživatelů");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }


     protected String doInBackground(String... args) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        JSONObject json = jParser.makeHttpRequest(url, "GET", params);
         Log.d("All Products: ", json.toString());

         try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                uziv = json.getJSONArray(TAG_WP_USERS);

                for (int i = 0; i < uziv.length(); i++) {
                    JSONObject c = uziv.getJSONObject(i);

                    String id = c.getString(TAG_ID);
                    String name = c.getString(TAG_USER_login);


                    HashMap<String, String> map = new HashMap<String, String>();


                    map.put(TAG_ID, id);
                    map.put(TAG_USER_login, name);


                    uzivatele.add(map);
                }
            } else {

                Intent i = new Intent(getApplicationContext(),
                        MenuActivity.class);

                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();

        }

        return null;
    }


    protected void onPostExecute(String file_url) {
        pDialog.dismiss();

        runOnUiThread(new Runnable() {
            public void run() {

                ListAdapter adapter = new SimpleAdapter(
                        vypis.this, uzivatele,
                        R.layout.activity_uzivatele, new String[] { TAG_ID,
                        TAG_USER_login},
                        new int[] { R.id.ID, R.id.name });
                setListAdapter(adapter);
            }
        });

    }

}

日志:

    03-30 15:56:42.056    3999-4353/com.example.olda.hledac E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    Process: com.example.olda.hledac, PID: 3999
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.NullPointerException
            at com.example.olda.hledac.vypis$VypisUzivatele.doInBackground(vypis.java:98)
            at com.example.olda.hledac.vypis$VypisUzivatele.doInBackground(vypis.java:82)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
03-30 15:56:42.696    3999-3999/com.example.olda.hledac E/WindowManager﹕ android.view.WindowLeaked: Activity com.example.olda.hledac.vypis has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{41baf8f8 V.E..... R......D 0,0-684,192} that was originally added here
            at android.view.ViewRootImpl.<init>(ViewRootImpl.java:370)
            at      android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:286)
            at com.example.olda.hledac.vypis$VypisUzivatele.onPreExecute(vypis.java:91)
            at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
            at android.os.AsyncTask.execute(AsyncTask.java:535)
            at com.example.olda.hledac.vypis.onCreate(vypis.java:51)
            at android.app.Activity.performCreate(Activity.java:5395)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2248)
            at android.app.ActivityThread.access$800(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1199)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5072)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:780)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:596)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

在这种情况下我的网站主机出了问题。它们在每个页面上都包含了副词,而这个副词是用JSON发送的,这就是问题,并帮助我解决这个问题。我设置了子串,现在可以正常工作。

try {
        jObj = new JSONObject(json.substring(3));
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
    }