我在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
但警报对话框从未出现过;当我在调试器中观察时,它将执行alert.show()行,但它永远不会显示在UI上.有没有人对这是为什么有任何想法?谢谢!
编辑:两个AlertDialogs都在调试器中执行,但从未在UI上显示
编辑2:我只是复制了我在这里发布的代码(看看是否是为了简化导致问题而删除的代码)进入一个新项目并且实际出现了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)
最佳答案
这对我有用,在OnPostExecute方法中.
原文链接:https://www.f2er.com/android/430789.html我在我的AsyncTask类中添加了Activity对象作为propretie,我在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();
}
});