Tirolel
Tirolel

Reputation: 928

Android AsyncTask failure in Dialog

Iam standing with this problem and I cant figure it out. I have this application, it run both on Galaxy S3 and Wildfire s, but when iam trying to run it on Galaxy S2 iam getting error. Galaxy S3 runs 4.1.2, Wilfire runs 2.2.1 and Galaxy S2 running 2.3.3 android os.

Problem there is a progress dialog and it stop and app exist the error is down but i dont understand like I mentioned before its working on 2 device but not on Galaxy S2. this is my error from logcat :

    04-03 11:02:42.010: E/AndroidRuntime(12339): FATAL EXCEPTION: AsyncTask #1
    04-03 11:02:42.010: E/AndroidRuntime(12339): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.lang.Thread.run(Thread.java:1019)
    04-03 11:02:42.010: E/AndroidRuntime(12339): Caused by: java.lang.NullPointerException
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at dk.tankegang.skraldespand.AlleKommuner$LoadAllProducts.doInBackground(AlleKommuner.java:138)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at dk.tankegang.skraldespand.AlleKommuner$LoadAllProducts.doInBackground(AlleKommuner.java:1)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
    04-03 11:02:42.010: E/AndroidRuntime(12339):    ... 4 more
    04-03 11:02:46.975: E/WindowManager(12339): Activity dk.tankegang.skraldespand.AlleKommuner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534248 that was originally added here
    04-03 11:02:46.975: E/WindowManager(12339): android.view.WindowLeaked: Activity dk.tankegang.skraldespand.AlleKommuner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40534248 that was originally added here
    04-03 11:02:46.975: E/WindowManager(12339):     at android.view.ViewRoot.<init>(ViewRoot.java:266)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.Dialog.show(Dialog.java:241)
    04-03 11:02:46.975: E/WindowManager(12339):     at dk.tankegang.skraldespand.AlleKommuner$LoadAllProducts.onPreExecute(AlleKommuner.java:126)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.os.AsyncTask.execute(AsyncTask.java:391)
    04-03 11:02:46.975: E/WindowManager(12339):     at dk.tankegang.skraldespand.AlleKommuner.onCreate(AlleKommuner.java:63)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.os.Handler.dispatchMessage(Handler.java:99)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.os.Looper.loop(Looper.java:123)
    04-03 11:02:46.975: E/WindowManager(12339):     at android.app.ActivityThread.main(ActivityThread.java:3691)
    04-03 11:02:46.975: E/WindowManager(12339):     at java.lang.reflect.Method.invokeNative(Native Method)
    04-03 11:02:46.975: E/WindowManager(12339):     at java.lang.reflect.Method.invoke(Method.java:507)
     04-03 11:02:46.975: E/WindowManager(12339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
     04-03 11:02:46.975: E/WindowManager(12339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
     04-03 11:02:46.975: E/WindowManager(12339):    at dalvik.system.NativeStart.main(Native Method)

And this is the code iam using, the class :

    public class AlleKommuner extends ListActivity {

SQLiteDatabase db;
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();

ArrayList<HashMap<String, String>> kommunerList;

private static String url_all_kommuner = "http://xxx.com";

// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_KOMMUNER = "kommuner";
private static final String TAG_KID = "kId";
private static final String TAG_KNAME = "kName";
private static final String TAG_KURL = "kUrl";

// products JSONArray
JSONArray kommuner = null;
  String TAG = "LEYTH Alle Kommuner";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.all_products);


    // Hashmap for ListView
    kommunerList = new ArrayList<HashMap<String, String>>();

    // Loading products in Background Thread
    new LoadAllProducts().execute();

    // Get listview
    ListView lv = getListView();

    // on seleting single product
    // launching Edit Product Screen
    lv.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

            db = openOrCreateDatabase("value.db", Context.MODE_PRIVATE, null); // optional CursorFactory

            String kUrl = ((TextView) view.findViewById(R.id.kUrl)).getText().toString();

            Intent in = new Intent(getApplicationContext(), EnkeltView.class);
            in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            db.execSQL("INSERT INTO startValue (myValue, myUrl) VALUES (1, "+"'" + kUrl +"'"+")");
            db.close();

            finish();
            startActivityForResult(in, 100);
            }    
    });

    ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
        @Override
        public void onRefresh() {
            // Do work to refresh the list here.
            new LoadAllProducts().execute();
            Log.d(TAG, "Here iam 2");

        }
    });

}

// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // if result code 100
    if (resultCode == 100) {
        // if result code 100 is received 
        // means user edited/deleted product
        // reload this screen again
        Intent intent = getIntent();
        finish();
        startActivity(intent);
    }
}

class LoadAllProducts extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AlleKommuner.this);
        pDialog.setMessage("Vent Venligst..");
        pDialog.setTitle("Henter Kommuner");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_kommuner, "GET", params);

        try {
                        // products found
                // Getting Array of Products
                kommuner = json.getJSONArray(TAG_KOMMUNER);

                kommunerList.clear();

                // looping through All Products
                for (int i = 0; i < kommuner.length(); i++) {
                    JSONObject c = kommuner.getJSONObject(i);

                    // Storing each json item in variable
                    String k_id = c.getString(TAG_KID);
                    String k_name = c.getString(TAG_KNAME);
                    String k_url = c.getString(TAG_KURL);

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

                    // adding each child node to HashMap key => value
                    map.put(TAG_KID, k_id);
                    map.put(TAG_KNAME, k_name);
                    map.put(TAG_KURL, k_url);


                    Log.v(TAG, "this is kUrl --> ; " + k_url + " --- And this is TAG --> " + TAG_KURL);


                    // adding HashList to ArrayList
                    kommunerList.add(map);
                }

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

        return null;
    }

    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        AlleKommuner.this, kommunerList,
                        R.layout.list_item, new String[] { TAG_KID,
                                TAG_KNAME, TAG_KURL},
                        new int[] { R.id.kId, R.id.name, R.id.kUrl });
                // updating listview
                setListAdapter(adapter);
            }
        });

        ((PullToRefreshListView) getListView()).onRefreshComplete();

    }

}

Upvotes: 0

Views: 392

Answers (1)

Yaroslav Mytkalyk
Yaroslav Mytkalyk

Reputation: 17115

json is null at this line. jParser.makeHttpRequest fails on those devices. Check what happens in jParser.makeHttpRequest and what can cause the problem. Maybe internet connection, maybe something else. It should throw an exception or if not, check the server response status code. Also you might want to log out the response string and see what you get.

Upvotes: 1

Related Questions