import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
/**
* Created by Tim on 17-8-2016.
*/
public class ReadRss extends AsyncTask<Void, Void, Void> {
Context context;
//The URL where the app needs to get the information from
String address = "http://www.sciencemag.org/rss/news_current.xml";
ProgressDialog progressDialog;
ArrayList<FeedItem>feedItems;
RecyclerView recyclerView;
URL url;
//The loading (Loading feed...) message
public ReadRss(Context context, RecyclerView recyclerView){
this.recyclerView = recyclerView;
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading feed...");
}
@Override
//Shows the user that the page is loading (Loading feed...)
protected void onPreExecute() {
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
MyAdapter adapter = new MyAdapter(context, feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(new VerticalSpace(50));
recyclerView.setAdapter(adapter);
}
@Override
protected Void doInBackground(Void... voids) {
processXml(Getdata());
return null;
}
//!!!!!This part ensures all the data is collected from the feed and it is made ready
//so it can be used by recycler view
private void processXml(Document data) {
if (data!=null) {
//Add all feedItems to an ArrayList
feedItems = new ArrayList<>();
//Object that will store root elements
Element root = data.getDocumentElement();
//Node object that will show channel
Node channel = root.getChildNodes().item(1);
//Store all child of channel element
NodeList items = channel.getChildNodes();
//Loop trough each child element of items
for (int i=0; i<items.getLength(); i++){
Node currentchild = items.item(i);
//Check that node is item node by using an if statement
if (currentchild.getNodeName().equalsIgnoreCase("item")){
FeedItem item = new FeedItem();
//Store childs in NodeList of current item
NodeList itemchilds = currentchild.getChildNodes();
//For loop to loop through all childs of item tag
for (int j=0; j<itemchilds.getLength(); j++){
//Store current node
Node current = itemchilds.item(j);
//Check node is title, description, pubDate, link or thumbnail node by if else conditions
if (current.getNodeName().equalsIgnoreCase("title")){
//If node is title, description, pubDate, link or thumbnail then set textcontent of our current node
item.setTitle(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("description")){
item.setDescription(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("pubDate")){
item.setPubDate(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("link")){
item.setLink(current.getTextContent());
}else if (current.getNodeName().equalsIgnoreCase("media:thumbnail")){
//This will return a thumbnail url
String url = current.getAttributes().item(0).getTextContent();
item.setThumbnailUrl(url);
}
}
//feedItems to be added to the ArrayList<FeedItem>
feedItems.add(item);
}
}
}
}
public Document Getdata() {
try {
url = new URL(address);
//Open connection by using Http url connection object
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Sets request method as GET
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDoc = builder.parse(inputStream);
return xmlDoc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
将RSS阅读器的此代码添加到我们的最终项目(学校)时,我们收到此错误:
08-21 16:53:27.583 2734-2734/com.example.codru.stendensocial E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.codru.stendensocial, PID: 2734
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at com.example.codru.stendensocial.ReadRss.onPostExecute(ReadRss.java:61)
at com.example.codru.stendensocial.ReadRss.onPostExecute(ReadRss.java:28)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
我们不知道如何解决这个问题。我们读了一些关于在setLayoutManager前添加final的内容,但是无法弄清楚如何执行此操作。
任何帮助都将受到高度赞赏!
~Tim
答案 0 :(得分:-1)
在构造函数中分配了recyclerView的值,似乎您正在发送空值。
你能发布你如何称呼这个对象吗?