在 AsyncTask 完成之前,我无法使用 GUI。我没有认识到在“onCreate”或“onCreateOptionsMenu”方法中发布 execute() 之间有什么区别。你知道出了什么问题吗?谢谢你的帮助。
package com.test11;
import java.util.concurrent.ExecutionException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class TestActitivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_actitivity);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test_actitivity, menu);
try
{
int t = new TestTask().execute().get();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
return true;
}
class TestTask extends AsyncTask<Void, Void, Integer>
{
public Integer doInBackground(Void...voids)
{
try
{
Thread.sleep(10000);
}
catch(Exception e)
{
e.printStackTrace();
}
return 1;
}
}
}