我正在尝试更改列表视图项目的字体。没有 AsyncTasc 会花费太多时间。将 AsyncTask 活动与该列表视图一起使用会很快出现,然后在实例应用程序崩溃后出现。我知道我不能直接从 AsyncTasc 更改 UI。我可以使用什么?提前致谢
import ru.ayratbadykov.feedhandler.FEED;
import ru.ayratbadykov.feedhandler.RssMessage;
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
private FEED _data;
Context _c;
CustomAdapter(FEED data, Context c) {
_data = data;
_c = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.w("here","here");
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) _c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.customadapter, null);
}
TextView fromView = (TextView) v.findViewById(R.id.textView1);
TextView subView = (TextView) v.findViewById(R.id.textView2);
TextView View = (TextView) v.findViewById(R.id.textView3);
RssMessage msg = _data.getMessages().get(position);
new Font().execute(fromView,subView,View);
fromView.setText(msg.getTitle());
subView.setText(msg.getPUBDATE());
View.setText(_data.getTitle());
return v;
}
public class Font extends AsyncTask<TextView, Void, Boolean> {
@Override
protected Boolean doInBackground(TextView... params) {
// TODO Auto-generated method stub
String fontPath = "fonts/Qlassik_TB.ttf";
Typeface tf = Typeface.createFromAsset(_c.getAssets(), fontPath);
params[0].setTypeface(tf);
fontPath="fonts/damase.ttf";
tf = Typeface.createFromAsset(_c.getAssets(), fontPath);
params[1].setTypeface(tf);
tf = Typeface.createFromAsset(_c.getAssets(), fontPath);
params[3].setTypeface(tf);
return true;
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return _data.getMessages().size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return _data.getMessages().get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
日志
05-31 22:32:27.602: E/AndroidRuntime(2803): FATAL EXCEPTION: AsyncTask #1
05-31 22:32:27.602: E/AndroidRuntime(2803): java.lang.RuntimeException: An error occured while executing doInBackground()
05-31 22:32:27.602: E/AndroidRuntime(2803): at android.os.AsyncTask$3.done(AsyncTask.java:200)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.lang.Thread.run(Thread.java:1019)
05-31 22:32:27.602: E/AndroidRuntime(2803): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
05-31 22:32:27.602: E/AndroidRuntime(2803): at android.os.Handler.<init>(Handler.java:121)
05-31 22:32:27.602: E/AndroidRuntime(2803): at ru.ayratbadykov.rssunion.CustomAdapter$Font$1.<init>(CustomAdapter.java:63)
05-31 22:32:27.602: E/AndroidRuntime(2803): at ru.ayratbadykov.rssunion.CustomAdapter$Font.doInBackground(CustomAdapter.java:63)
05-31 22:32:27.602: E/AndroidRuntime(2803): at ru.ayratbadykov.rssunion.CustomAdapter$Font.doInBackground(CustomAdapter.java:1)
05-31 22:32:27.602: E/AndroidRuntime(2803): at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-31 22:32:27.602: E/AndroidRuntime(2803): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
05-31 22:32:27.602: E/AndroidRuntime(2803): ... 4 more