我想用Jsoup下载一些网址,然后将它们设置为Picasso图像视图的源网址。但是我这样做有很多困难。
然后我决定只从jSoup类向picasso发送一个单个硬编码的URL,导致异常
这是我的MainActivity的代码
package com.vsoft.tsubdl;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
public ImageView imageView;
public static ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
String[] tinyurl = {"http://2tinysub.net/"};
JsonParser jsonParser;
ArrayList<ImageView> imageViewsArray;
public static Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
imageView4 = (ImageView) findViewById(R.id.imageView4);
mContext = this;
imageViewsArray = new ArrayList<>();
imageViewsArray.add(imageView);
imageViewsArray.add(imageView2);
imageViewsArray.add(imageView3);
imageViewsArray.add(imageView4);
jsonParser = new JsonParser();
try {
jsonParser.execute(tinyurl);
} catch (Exception e) {
e.printStackTrace();
}
}
public void setImages(){
Picasso.with(MainActivity.mContext)
.load(jsonParser.theUrl)
.into(imageView);
}
这是我的JsoupParser活动
public class JsonParser extends AsyncTask<String,Void,String> {
public String[] URLS = {"","","",""};
public static String theUrl = "";
MainActivity mainActivity = new MainActivity();
@Override
protected String doInBackground(String... urls) {
try {
Document doc = Jsoup.connect("http://2tinysub.net/").get();
Elements links = doc.select(".box .details ul li");
Elements picURLS = doc.select(".details .poster img");
Element firsimage = picURLS.first();
theUrl = "https://i.jeded.com/i/now-you-see-me-2.135-52356.jpg";
} catch (Exception e) {
e.printStackTrace();
}
return theUrl;
}
@Override
protected void onPostExecute(String s) {
mainActivity.setImages();
}
}
我认为这与我在setImage方法中设置图像这一事实有关。
我这样说是因为如果我在主活动的try块中的Jsoup执行语句之后执行picasso部分它会正常工作。
现在请问任何人都有答案是什么错误。
答案 0 :(得分:0)
您的问题是您在Asynctask中创建了一个新的MainActivity实例:MainActivity mainActivity = new MainActivity();
如果您考虑使用异步任务的活动生命周期,则此方法存在许多问题。但对于您的问题 - 对于此特定情况,您的异步任务只需要引用启动它的活动。您需要将其传递到异步任务,很可能是在构造函数中。如果你走那条路,请把你的活动包裹在WeakReference中,这样你就不会造成记忆压力。
以下是解决问题的方法:(请注意,我没有编译,因此可能存在拼写错误)
public class JsonParser extends AsyncTask<String,Void,String> {
private WeakReference<MainActivity> mainActivityReference;
public JsonParser(MainActivity activity) {
mainActivityReference = new WeakReference(activity);
}
... <your other code>
@Override
protected void onPostExecute(String s) {
//Because the activity could be destroyed, don't store a hard reference to the activity
//Also you should probably check to make sure the activity is in a good state
MainActivity activity = mainActivityReference.get();
if(activity != null) {
activity.setImages();
}
}