1

为什么这个错误通常出现在listview我在 Android 4.0 版本中运行时,任何人都可以帮助我解决这个问题。我搜索了一些网站,发现asynctaskurl如何实现它。

我试过的:这是异步任务

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);
    }
}

使用代码我收到“空指针异常”。这是我在 4.0 版本中尝试上述代码时的日志

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)

有人可以帮我解决这个问题吗@Thanks

4

3 回答 3

2

您当前在以下代码块中的问题

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

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

发生的情况是,当调用 GetTask() 时,将显示一个 progressdialog,并且在构造函数调用后 onPreExecute() 将被调用,并且此方法再次尝试显示相同的对话框。

您只需要将您的方法修改为 ..

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();
        }

快乐的编码:)

于 2013-04-30T07:25:09.323 回答
0

从下面的代码中删除此方法Callpagedetails();,因为您已经在AsyncTask. 无需Thread再次使用AsyncTask

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

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

                 }

只需使用:

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

                 }
于 2013-04-30T06:41:33.523 回答
0

当应用程序尝试在其主线程上执行网络或线程操作时抛出此异常,即在 Activity 的 onCreate() 方法中 - 有两种解决方案 -

1) 不要在主 UI 线程中做与网络相关的工作,为此使用异步任务。

2)在setContentView(R.layout.main)之后将以下代码写入您的MainActivity文件;

 if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
于 2013-04-30T06:42:27.757 回答