5

我的 AsyncTask 类的 onPostExecute 有问题。这是代码。

--编辑:按要求在其余代码中添加--

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    preliminaryTasks();
}

private void preliminaryTasks() {

    //removed more code

    new discoverAddresses().execute();

}

public class discoverAddresses extends AsyncTask<String, Void, List<String>> {

    @Override
    protected List<String> doInBackground(String... params){

        /* Lots of code here taken out */

        List<String> addresses = new ArrayList<String>();
        //Added for debugging purposes
        addresses.add("http://192.168.1.120:80");
        addresses.add("http://192.168.1.110:80");
        return addresses;

    }

    @Override
    protected void onPostExecute(List<String> addresses){

        MainActivity.this.runOnUiThread(new Runnable(){
            public void run(){
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setMessage("Are you sure you want to exit?").setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                MainActivity.this.finish();
                            }
                        }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            }
        });

        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setMessage("Are you sure you want to exit?").setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        MainActivity.this.finish();
                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();

        return; 

    }
}
}

但是警告对话框永远不会出现;当我在调试器中观看时,它会执行 alert.show() 行,但它永远不会显示在 UI 上。有没有人知道为什么会这样?谢谢!

编辑:两个 AlertDialogs 都在调试器中执行,但从不在 UI 上显示

EDIT2:我只复制了我在此处发布的代码(以查看是否是我为简化而删除的导致问题的代码)到一个新项目中,并且实际出现了 alertdialog,这意味着我取出的代码是一个问题。在强制关闭应用程序终止后,这将出现在我的原始项目中的 LogCat 中。

02-02 11:13:28.746: D/AndroidRuntime(11658): Shutting down VM
02-02 11:13:28.746: W/dalvikvm(11658): threadid=1: thread exiting with uncaught exception (group=0x40fce300)
02-02 11:13:28.762: E/AndroidRuntime(11658): FATAL EXCEPTION: main
02-02 11:13:28.762: E/AndroidRuntime(11658): java.lang.NullPointerException
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.AbsListView.obtainView(AbsListView.java:2257)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.ListView.onMeasure(ListView.java:1156)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.View.measure(View.java:15172)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1077)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.Choreographer.doFrame(Choreographer.java:525)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.os.Handler.handleCallback(Handler.java:615)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.os.Looper.loop(Looper.java:137)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at android.app.ActivityThread.main(ActivityThread.java:4899)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at java.lang.reflect.Method.invoke(Method.java:511)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
02-02 11:13:28.762: E/AndroidRuntime(11658):    at dalvik.system.NativeStart.main(Native Method)
4

5 回答 5

3

这对我有用,在 OnPostExecute 方法中。

我在我的 AsyncTask 类中添加 Activity 对象作为属性,我在该 AsyncTack 的构造函数中实例化它,以便我可以调用该活动并在 onpostexe 中执行 runOnUiThread ...

AlertDialog.Builder builder = new AlertDialog.Builder(Activity);
            builder.setTitle("Connection ?");
            builder.setIcon(android.R.drawable.ic_dialog_alert);
            builder.setMessage("Test \n ");
            builder.setPositiveButton("Ok",null);
            final AlertDialog alert = builder.create();
            Activity.runOnUiThread(new java.lang.Runnable(){
                public void run(){
                    //show AlertDialog
                    alert.show();
                }
            });
于 2013-03-20T02:26:53.437 回答
1

我发现我的代码有问题。在某个时候,这个方法被调用了 promptAddresses。它有自己独立的 AlertDialog。因为 addressArray 的索引为 5,所以它与 AlertDialog.builder 搞砸了,因为其中三个索引为空。

TLDR:在处理 AlertDialog.Builders 时,字符串数组中没有任何空值

解决方案:最终 String[] addressArray = new String[address.size () ];

非常感谢所有帮助过的人。我很抱歉没有提供完整的代码并混淆了整个问题。无论如何,您的回答仍然很有帮助。

public String promptAddresses(List<String> addresses){
    final String[] addressArray = new String[5];
    int i = 0;

    Iterator it = addresses.iterator();
    while(it.hasNext()){
        String next = (String) it.next();
        addressArray[i] = next;
        Log.i("ADDRESSES", next);
        i++;
    }

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Pick your address");
    builder.setItems(addressArray, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
               // The 'which' argument contains the index position
               // of the selected item
           }
    });
    AlertDialog alert =  builder.create();
    alert.show();

    return hubAddress;
}
于 2013-02-02T17:45:41.090 回答
0

我看了你的代码,但找不到任何问题。你AsyncTask在主线程中执行了吗?如果没有,您可以包装以下代码以显示AlertDialog

runOnUiThread(new Runable(){
    public void run(){
        //show AlertDialog
    }
})
于 2013-02-02T02:03:44.883 回答
0

doInBackground() 函数的代码中有一个错误:

List<String> addresses = null;
//Added for debugging purposes
addresses.add("http://192.168.1.120:80");

这将在第二行生成 Null 异常,因为地址为空,因此将终止您的线程。

于 2013-02-02T04:03:01.287 回答
0

我猜你的actvity_main.xml. 我通过空白尝试了您提供的代码RelativeLayout该代码工作正常,它显示了两个警报对话框以及编码功能。只需尝试相同并跟踪错误

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>
于 2013-02-02T17:26:35.393 回答