[英]android update custom view in asynctask
Ok, I have a custom view which plays gifs from the internet. 好的,我有一个自定义视图,可从互联网播放gif图像。 Therefor I need to add an url to my view to download the gif.
因此,我需要在我的视图中添加一个网址以下载gif。 But I can't seem to update my custom view inside my
asynctask
. 但是我似乎无法在
asynctask
更新自定义视图。 I need to add an url string to my custom view gifView.setUrl()
. 我需要在自定义视图
gifView.setUrl()
添加一个url字符串。 It works in the onCreate Class but it gives me null in asynctask. 它可以在onCreate类中工作,但在asynctask中却为我提供了null。
Oncreate class Oncreate类
GifView gifView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
id = extras.getInt("id");
String idStr = String.valueOf(id);
String extension = extras.getString("extension");
if(extension.equals(".gif")){
setContentView(R.layout.activity_post_gif);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gifView = (GifView)findViewById(R.id.gifview);
titleStr = (TextView)findViewById(R.id.titleTXT);
postInfo = (TextView)findViewById(R.id.infoTXT);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
//the url
new getJsonInfoGif().execute("http://www.website.com/jsonApi");
}else{
Asynctask 的AsyncTask
public class getJsonInfoGif extends AsyncTask<String, Void, String>{
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("loading post...");
progressDialog.show();
}
@Override
protected String doInBackground(String... strings) {
return GET(strings[0]);
}
@Override
protected void onPostExecute(String res) {
try {
JSONObject jsonObject = new JSONObject("{'postinfo':[" + res + "]}");
JSONArray jsonArray = jsonObject.getJSONArray("postinfo");
JSONObject obj = jsonArray.getJSONObject(0);
//post title
titleStr.setText(obj.getString("name"));
//category and maker full name
//large image
JSONObject imgObj = obj.getJSONObject("thumbnails");
gifView.setUrl("http://www.website.com/my.gif");
} catch (JSONException e) {
e.printStackTrace();
}
if (progressDialog != null) {
progressDialog.dismiss();
}
}
}
GifView.java GifView.java
public void setUrl(String urlStr){
this.urlStr = urlStr;
invalidate();
requestLayout();
}
public String getUrl(){
return this.urlStr;
}
public void init(final Context context)throws IOException{
setFocusable(true);
movie = null;
movieWidth = 0;
movieHeight = 0;
movieDuration = 0;
final Thread thread = new Thread(new Runnable() {
@Override
public void run(){
try{
Log.d("DEBUG", "URL" + urlStr);
URL url = new URL(urlStr);
try {
HttpURLConnection http = (HttpURLConnection) url.openConnection();
inputStream = http.getInputStream();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
movie = Movie.decodeStream(inputStream);
movieWidth = movie.width();
movieHeight = movie.height();
movieDuration = movie.duration();
((PostActivity) context).runOnUiThread(new Runnable() {
@Override
public void run() {
invalidate();
requestLayout();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}
}
});
thread.start();
}
Here is the Log from the url, it gives me null if I add the url inside my asynctask in Activity. 这是该网址的日志,如果我在Activity中的asynctask中添加该网址,则会给我null。
11-07 14:41:58.821 5674-6076/svenmobile.tools.showcase D/DEBUG﹕ URLnull
What I want to know is what the problem is and how to solve it if possible. 我想知道的是问题所在,并在可能的情况下如何解决。 Thanks in advance, Sven
预先感谢,斯文
Maybe you called init()
before setUrl()
. 也许您在
setUrl()
之前调用了init()
setUrl()
。
You can pass it the url in the contructor, or public void init(final Context context, String urlStr)throws IOException{
您可以向其传递构造函数中的url,也可以将
public void init(final Context context, String urlStr)throws IOException{
I also suggest you to move all that network code to doInBackground
我还建议您将所有网络代码移至
doInBackground
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.