1

Why this error usually arise in listview when i run in Android 4.0 version ,could anybody help me regarding this.I searched some sites found like using asynctask in url how could i implement it.

what i have tried: here is the async task

public class GetTask extends AsyncTask<String, Void, Integer> {
    private ProgressDialog progressDialog;
    public GetTask(){
    }
    protected void onPreExecute() {
                   this.progressDialog=new ProgressDialog(FirstActivity.this);
        this.progressDialog.show();
    }
    @Override
    protected Integer doInBackground(String... userurl) {
                         page_count=0;
                         Callpagedetails();
        return null;
    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if (progressDialog.isShowing())
            progressDialog.dismiss();
          listview = (ListView) findViewById(R.id.listquestion);
         listview.setAdapter(adapterf);
    }
}

Using the code i receive `Null pointer Exception. Here is the log when i try the above code in 4.0 version

04-30 07:25:04.842: E/AndroidRuntime(1654): FATAL EXCEPTION: AsyncTask #1
04-30 07:25:04.842: E/AndroidRuntime(1654): java.lang.RuntimeException: An error occured while executing doInBackground()
04-30 07:25:04.842: E/AndroidRuntime(1654):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.lang.Thread.run(Thread.java:856)
04-30 07:25:04.842: E/AndroidRuntime(1654): Caused by: java.lang.NullPointerException
04-30 07:25:04.842: E/AndroidRuntime(1654):     at com.example.know.FirstActivity.Callpagedetails(FirstActivity.java:227)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at com.example.know.FirstActivity$GetTask.doInBackground(FirstActivity.java:125)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at com.example.know.FirstActivity$GetTask.doInBackground(FirstActivity.java:1)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-30 07:25:04.842: E/AndroidRuntime(1654):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-30 07:25:04.842: E/AndroidRuntime(1654):     ... 4 more

    04-30 06:49:34.781: E/WindowManager(851): Activity com.example.know.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cd41e8 V.E..... R.....ID 0,0-191,168} that was originally added here
    04-30 06:49:34.781: E/WindowManager(851): android.view.WindowLeaked: Activity com.example.know.FirstActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40cd41e8 V.E..... R.....ID 0,0-191,168} that was originally added here
    04-30 06:49:34.781: E/WindowManager(851):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
    04-30 06:49:34.781: E/WindowManager(851):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
    04-30 06:49:34.781: E/WindowManager(851):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.Dialog.show(Dialog.java:281)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ProgressDialog.show(ProgressDialog.java:116)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ProgressDialog.show(ProgressDialog.java:99)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ProgressDialog.show(ProgressDialog.java:94)
    04-30 06:49:34.781: E/WindowManager(851):   at com.example.know.FirstActivity$GetTask.<init>(FirstActivity.java:99)
    04-30 06:49:34.781: E/WindowManager(851):   at com.example.know.FirstActivity.onCreate(FirstActivity.java:88)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.Activity.performCreate(Activity.java:5104)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ActivityThread.access$600(ActivityThread.java:141)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    04-30 06:49:34.781: E/WindowManager(851):   at android.os.Handler.dispatchMessage(Handler.java:99)
    04-30 06:49:34.781: E/WindowManager(851):   at android.os.Looper.loop(Looper.java:137)
    04-30 06:49:34.781: E/WindowManager(851):   at android.app.ActivityThread.main(ActivityThread.java:5041)
    04-30 06:49:34.781: E/WindowManager(851):   at java.lang.reflect.Method.invokeNative(Native Method)
    04-30 06:49:34.781: E/WindowManager(851):   at java.lang.reflect.Method.invoke(Method.java:511)
    04-30 06:49:34.781: E/WindowManager(851):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-30 06:49:34.781: E/WindowManager(851):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    04-30 06:49:34.781: E/WindowManager(851):   at dalvik.system.NativeStart.main(Native Method)

Could anybody help me regarding this @Thanks

Siddharth
  • 9,349
  • 16
  • 86
  • 148

3 Answers3

2

Your current problem in bellow code block

public GetTask(){
        progressDialog = ProgressDialog.show(FirstActivity.this, "Loading","Please Wait");
    }

    protected void onPreExecute() {
        this.progressDialog.setMessage("Progress start");
        this.progressDialog.show();
    }

What happening is, when GetTask() gets called a progressdialog will be shown and after constructor call onPreExecute() will get call and this method again trying to show the same dialog.

You just need to modify your method as ..

public GetTask(){
            //progressDialog = ProgressDialog.show(FirstActivity.this, "Loading","Please Wait");
        }

        protected void onPreExecute() {
            this.progressDialog = new ProgressDialog(yourContext);
            this.progressDialog.setTitle("Loading");
            this.progressDialog.setMessage("Progress start");
            this.progressDialog.show();
        }

Happy codeing :)

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
  • What is at line number 227... check if that is null – Pankaj Kumar Apr 30 '13 at 07:36
  • @ Pankaj Kumar i have used loadmorelist in line 227 –  Apr 30 '13 at 07:38
  • We are asking about the full code of callpagedetails() but you posted some parts... check loadmorelist if any susspect can be null put null check there... and one more thing if you are updating your llistview inside callpagedetails(), DONOT DO THAT... move that code inside onPostExecute of AsyncTask.. – Pankaj Kumar Apr 30 '13 at 07:41
  • Why i am getting this statement Emulator without GPU emulation detected. –  Apr 30 '13 at 07:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29172/discussion-between-pankaj-kumar-and-priya2134412) – Pankaj Kumar Apr 30 '13 at 07:56
0

remove this method Callpagedetails(); from below code because you are already do this in AsyncTask. no need to use Thread again in AsyncTask

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

                             page_count=0;
                            Callpagedetails();//here i call the url

                 }

just use:

protected Integer doInBackground(String... userurl) {
            // TODO Auto-generated method stub      
                             page_count=0;
                            Callpagedetails(); //here i call the url

                 }
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • now you are getting 'Activity com.example.know.FirstActivity has leaked window' error means You're trying to change some value in activity or showing dialog before Process complate. – Dhaval Parmar Apr 30 '13 at 06:56
  • if you are trying to set values in layout so Please do that in onPostExecute method not in Callpagedetails(); – Dhaval Parmar Apr 30 '13 at 06:57
  • i have used the listview.setAdapter(adapterf); in postExecute method but still i got the error! –  Apr 30 '13 at 07:04
  • using the above code i am getting the above error how could i make it right –  Apr 30 '13 at 07:09
  • i have added the callpagedetails part too –  Apr 30 '13 at 07:15
0

This exception is thrown when an application attempts to perform a networking or threading operation on its main thread i.e. in onCreate() method of Activity- there are two solutions for that -

1) Don't do network related work in Main UI Thread, Use Async Task for that.

2) Write below code into your MainActivity file after setContentView(R.layout.main);

 if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
Ajit
  • 957
  • 1
  • 8
  • 24