我是Android新手,我在填写textView以显示错误消息方面遇到了问题。 在RSSReader服务中,在doInBackground()方法中,当它无法从NewsActivity给出的url中获取数据时,它应该在TextView errorMessage中设置一些消息,并且仅当给定的url根本不是url时它才有效,否则活动崩溃了。
我不明白为什么方法setText()并不总是有效。
这是ReadRss服务类
public class ReadRss extends AsyncTask<Void,Void,Void> {
Context context;
ProgressDialog progressDialog;
String address;
URL url;
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
Dialog dialog;
NewsActivity newsActivity;
public ReadRss(Context context, RecyclerView recyclerView, String address, NewsActivity newsActivity) {
this.context = context;
this.recyclerView = recyclerView;
this.address = address;
this.newsActivity = newsActivity;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading");
}
@Override
protected void onPreExecute() { //prima di caricare i dati mostro il dialog
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
Log.e("POST EXECUTE", "E' ENTRATO");
MyAdapter adapter = new MyAdapter(feedItems, context);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
}
@Override
protected Void doInBackground(Void... voids) {
if(getData()!=null){
processXml(getData());
}else {
Log.e("DET DATA NULL", "E' ENTRATO");
feedItems = new ArrayList<>();
newsActivity.setErrorMessage("Invalid Url, Please delete this feed and create one with a valid url");
}
return null;
}
// In this method we will process Rss feed document we downloaded to parse useful information from it
private void processXml(Document data) {
if (data != null) {
feedItems = new ArrayList<>();
Element root = data.getDocumentElement();
Node channel = root.getChildNodes().item(1);
NodeList items = channel.getChildNodes();
for (int i = 0; i < items.getLength(); i++) {
Node cureentchild = items.item(i);
if (cureentchild.getNodeName().equalsIgnoreCase("item")) {
FeedItem item = new FeedItem();
NodeList itemchilds = cureentchild.getChildNodes();
for (int j = 0; j < itemchilds.getLength(); j++) {
Node cureent = itemchilds.item(j);
if (cureent.getNodeName().equalsIgnoreCase("title")) {
item.setTitle(cureent.getTextContent());
}else if (cureent.getNodeName().equalsIgnoreCase("pubDate")) {
item.setPubDate(cureent.getTextContent());
} else if (cureent.getNodeName().equalsIgnoreCase("link")) {
item.setLink(cureent.getTextContent());
}
}
feedItems.add(item);
}
}
}else{
feedItems = new ArrayList<>();
}
}
public Document getData(){
try {
url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document xmlDocument = documentBuilder.parse(inputStream);
return xmlDocument;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (ParserConfigurationException e) {
e.printStackTrace();
return null;
} catch (SAXException e) {
e.printStackTrace();
return null;
}
}
}
这个NewsActivity Class
public class NewsActivity extends AppCompatActivity {
RecyclerView recyclerView;
String url;
TextView errorMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
Bundle bundle = getIntent().getExtras();
url = bundle.getString("Url");
recyclerView = findViewById(R.id.recyclerViewNewsActivity);
errorMessage = findViewById(R.id.error_message);
ReadRss readRss = new ReadRss(this, recyclerView,url, this);
readRss.execute();
}
public void setErrorMessage(String errorMessage){
this.errorMessage.setText(errorMessage);
}
}
我希望我很清楚,谢谢你的帮助