Pranesh Sahu Pranesh Sahu - 4 months ago 9
Java Question

Application crashed some times but not in debug mode

I have a strange situation in which my application is getting crashed sometime(say after doing nearly 20 transaction) but in debug mode its not happening even in emulator the crashed doesn't occurred.
I have increase my heap memory too.

The Log error i'm attaching here:

07-20 11:27:39.207: E/AndroidRuntime(17877): FATAL EXCEPTION: main
07-20 11:27:39.207: E/AndroidRuntime(17877): java.lang.OutOfMemoryError
07-20 11:27:39.207: E/AndroidRuntime(17877): at java.util.ArrayList.toArray(ArrayList.java:492)
07-20 11:27:39.207: E/AndroidRuntime(17877): at java.util.ArrayList.<init>(ArrayList.java:93)
07-20 11:27:39.207: E/AndroidRuntime(17877): at com.mis.adapter.MicListAdapter.<init>(MicListAdapter.java:52)
07-20 11:27:39.207: E/AndroidRuntime(17877): at com.mis.controller.InventoryCount$InflateList.onPostExecute(InventoryCount.java:3061)
07-20 11:27:39.207: E/AndroidRuntime(17877): at com.mis.controller.InventoryCount$InflateList.onPostExecute(InventoryCount.java:1)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.os.AsyncTask.finish(AsyncTask.java:631)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.os.Handler.dispatchMessage(Handler.java:99)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.os.Looper.loop(Looper.java:137)
07-20 11:27:39.207: E/AndroidRuntime(17877): at android.app.ActivityThread.main(ActivityThread.java:4895)
07-20 11:27:39.207: E/AndroidRuntime(17877): at java.lang.reflect.Method.invokeNative(Native Method)
07-20 11:27:39.207: E/AndroidRuntime(17877): at java.lang.reflect.Method.invoke(Method.java:511)
07-20 11:27:39.207: E/AndroidRuntime(17877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-20 11:27:39.207: E/AndroidRuntime(17877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-20 11:27:39.207: E/AndroidRuntime(17877): at dalvik.system.NativeStart.main(Native Method)


Here i have attached my onPostMethod()

protected void onPostExecute(List<MIC_OrderDetails> lst) {
dialog.setMessage("Inflating Data...");
if (lst.get(lst.size() - 1).getResult().contains(("success"))) {
ordList = new MicListAdapter(InventoryCount.this, lst);
lstView.setAdapter(ordList);
dialog.dismiss();
} else {
dialog.dismiss();
toastText.setText("Problem in loading Items");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(toastLayout);
toast.show();
}
}

Answer

Don't recreate the adapter again in onPostExecute() method. Instead create the adapter once in onCreate() method and use adapter.notifyDatasetChanged() in onPostExecute() method. Something like this,

Create your dataset list as a class variable and initialize it before onCreate() like this,

List<MIC_OrderDetails> list = new ArrayList<MIC_OrderDetails>();

Inside onCreate method initialize the adapter,

adapter = new MicListAdapter(InventoryCount.this, list);
lstView.setAdapter(adapter);

Inside onPostExecute method,

protected void onPostExecute(List<MIC_OrderDetails> lst) {
    dialog.setMessage("Inflating Data...");
    if (lst.get(lst.size() - 1).getResult().contains(("success"))) {
        list.clear();
        list.addAll(lst);
        adapter.notifyDataSetChanged();
        dialog.dismiss();
    } else {
        dialog.dismiss();
        toastText.setText("Problem in loading Items");
        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(toastLayout);
        toast.show();
    }
}
Comments