Android onCreate方法中的AsyncTask
我有一个简单的应用程序,可以播放在线广播。为了显示在线php服务的标题,我使用AsyncTask并从onCreate方法调用它。在安卓4中,一切正常,但在安卓2中,它被错误压碎了Android onCreate方法中的AsyncTask,android,android-asynctask,Android,Android Asynctask,我有一个简单的应用程序,可以播放在线广播。为了显示在线php服务的标题,我使用AsyncTask并从onCreate方法调用它。在安卓4中,一切正常,但在安卓2中,它被错误压碎了 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 然后在互联网上我发现,我必须使用这样的代码 new Thread(new Runnable() {
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
然后在互联网上我发现,我必须使用这样的代码
new Thread(new Runnable() {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
//my code
}
});
}
}).start();
但是在我使用这个技巧之后,a在我的android 4和android 2版本中看不到任何按钮和文本视图。这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//thread for update title every second
new Thread(new Runnable() {
@Override
public void run() {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
while(true) {
try {
new ShowTitle()
.execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
Thread.sleep(1000);
} catch (InterruptedException e) {
String tag = "Update title";
Log.e(tag, "Update title crashed", e);
}
}
}
});
}
}).start();
}
//get title string from online source
private String getMusicTitle(String url) {
Document doc = null;
String title = "Music Title";
try {
url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream input = new URL(url).openStream();
doc = Jsoup.parse(input, "CP1251", url);
title = doc.body().text();//doc.select(".products_name").first().text();
} catch (IOException e) {
Log.e(TAG, "Failed to load HTML code", e);
Toast.makeText(this, "Failed to load title", Toast.LENGTH_SHORT).show();
}
return title;
}
//class for show the audio title
private class ShowTitle extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
return getMusicTitle(urls[0]);
}
protected void onPostExecute(final String result) {
lblMusicName.setText(result);
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//每秒更新标题的线程
新线程(newrunnable()){
@凌驾
公开募捐{
MainActivity.this.runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
while(true){
试一试{
新节目名称()
.执行(”http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
睡眠(1000);
}捕捉(中断异常e){
String tag=“更新标题”;
Log.e(标签“更新标题崩溃”,e);
}
}
}
});
}
}).start();
}
//从联机源获取标题字符串
私有字符串getMusicTitle(字符串url){
单据单据=空;
String title=“音乐标题”;
试一试{
url=”http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream输入=新URL(URL).openStream();
doc=Jsoup.parse(输入“CP1251”,url);
title=doc.body().text();//doc.select(“.products_name”).first().text();
}捕获(IOE异常){
Log.e(标记“加载HTML代码失败”,e);
Toast.makeText(这是“加载标题失败”,Toast.LENGTH_SHORT.show();
}
返回标题;
}
//用于显示音频标题的类
私有类ShowTitle扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
返回getMusicTitle(URL[0]);
}
受保护的void onPostExecute(最终字符串结果){
lblMusicName.setText(结果);
}
}
编辑:(我的工作代码)
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新节目名称()
.执行(”http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
}
私有字符串getMusicTitle(字符串url){
单据单据=空;
String title=“音乐标题”;
试一试{
url=”http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream输入=新URL(URL).openStream();
doc=Jsoup.parse(输入“CP1251”,url);
title=doc.body().text();
}捕获(IOE异常){
Log.e(标记“加载HTML代码失败”,e);
title=“加载标题失败”;
}
返回标题;
}
私有类ShowTitle扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
while(true){
String str=getMusicTitle(URL[0]);
出版进度(str);
试一试{
睡眠(1000);
}捕捉(中断异常e){
String tag=“更新标题”;
Log.e(标签“更新标题崩溃”,e);
}
}
}
受保护的void onProgressUpdate(字符串…结果){
lblMusicName.setText(结果[0]);
}
}
在这里:
try {
//....your code here
} catch (IOException e) {
Log.e(TAG, "Failed to load HTML code", e);
Toast.makeText(this, "Failed to load title",
Toast.LENGTH_SHORT).show(); //<<< this line
}
这将始终冻结异步任务执行后的Ui线程。因此需要移动
线程。sleep(1000)
外部runOnUiThread
代码块runOnUiThread
和AsyncTask
是两件不同的事情。你用错了它
试着这样做:-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ShowTitle().execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
}
//get title string from online source
private String getMusicTitle(String url) {
Document doc = null;
String title = "Music Title";
try {
url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream input = new URL(url).openStream();
doc = Jsoup.parse(input, "CP1251", url);
title = doc.body().text();//doc.select(".products_name").first().text();
} catch (IOException e) {
Log.e(TAG, "Failed to load HTML code", e);
title = "Failed to load title";
}
return title;
}
//class for show the audio title
private class ShowTitle extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
String str = getMusicTitle(urls[0]);
while(true) {
publishProgress(str);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
String tag = "Update title";
Log.e(tag, "Update title crashed", e);
}
}
return str;
}
@Override
protected void onProgressUpdate(String... progress) {
if(returnVal.startsWith("Failed")) {
Toast.makeText(this, returnVal, Toast.LENGTH_SHORT).show();
} else {
lblMusicName.setText(result);
}
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建ShowTitle()。执行(“http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
}
//从联机源获取标题字符串
私有字符串getMusicTitle(字符串url){
单据单据=空;
String title=“音乐标题”;
试一试{
url=”http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream输入=新URL(URL).openStream();
doc=Jsoup.parse(输入“CP1251”,url);
title=doc.body().text();//doc.select(“.products_name”).first().text();
}捕获(IOE异常){
Log.e(标记“加载HTML代码失败”,e);
title=“加载标题失败”;
}
返回标题;
}
//用于显示音频标题的类
私有类ShowTitle扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
String str=getMusicTitle(URL[0]);
while(true){
出版进度(str);
试一试{
睡眠(1000);
}捕捉(中断异常e){
String tag=“更新标题”;
Log.e(标签“更新标题崩溃”,e);
}
}
返回str;
}
@凌驾
受保护的void onProgressUpdate(字符串…进度){
if(returnVal.startsWith(“失败”)){
Toast.makeText(this,returnVal,Toast.LENGTH_SHORT).show();
}否则{
lblMusicName.setText(结果);
}
}
}
你必须在run()内的
onProgressUpdate
run()中完成所有与UI相关的任务,你在开玩笑吗?我不需要Toast,我将它从代码中删除,并将Thread.sleep()从runOnUiThread移动到主线程。但什么都没有changed@JackDaniel字体什么都没有改变是什么意思?请同时更新您的问题
while(true) {
try {
...
Thread.sleep(1000); //<<< here calling Thread.sleep on Main UI Thread
} catch (InterruptedException e) {
String tag = "Update title";
Log.e(tag, "Update title crashed", e);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ShowTitle().execute("http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm");
}
//get title string from online source
private String getMusicTitle(String url) {
Document doc = null;
String title = "Music Title";
try {
url = "http://info.radiostyle.ru/inc/getinfo.php?getcurentsong=20383&mount=lezgifm";
InputStream input = new URL(url).openStream();
doc = Jsoup.parse(input, "CP1251", url);
title = doc.body().text();//doc.select(".products_name").first().text();
} catch (IOException e) {
Log.e(TAG, "Failed to load HTML code", e);
title = "Failed to load title";
}
return title;
}
//class for show the audio title
private class ShowTitle extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... urls) {
String str = getMusicTitle(urls[0]);
while(true) {
publishProgress(str);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
String tag = "Update title";
Log.e(tag, "Update title crashed", e);
}
}
return str;
}
@Override
protected void onProgressUpdate(String... progress) {
if(returnVal.startsWith("Failed")) {
Toast.makeText(this, returnVal, Toast.LENGTH_SHORT).show();
} else {
lblMusicName.setText(result);
}
}
}