已编辑忘记包含实际问题。
所以我知道我的文件返回null是因为我的“GetData()”调用正在流经我的其余部分,导致错误。但是,我不知道如何防止这种情况。有人能指出我正确的方向吗?
问题
我得到的错误是
Attempt to invoke virtual method 'java.lang.Object[] java.util.ArrayList.toArray()' on a null object reference
从我的理解当我使用该行
titles = readRss.titles;
它通过此订单拉取空响应.ReadRss> ProcessXml()> GETDATA()
并且因为get data是一个“try”“catch”部分,它返回xmldoc和null。
当我登录我的ProcessXml()时,我可以获得完整的标题列表,我可以单独记录订阅源项,但是如果我尝试在其外部登录,则返回null。
我错误的是什么。ReadRss脚本:
public class ReadRss extends AsyncTask<Void, Void, Void> {
ArrayList<String>titles;
BlankFragment context;
String address="https://www.sciencemag.org/rss/news_current.xml";
//ProgressDialog progressDialog;
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
URL url;
public ReadRss(BlankFragment context, RecyclerView recyclerView){
this.recyclerView=recyclerView;
this.context=context;
// progressDialog = new ProgressDialog(context);
// progressDialog.setMessage("Loading...");
}
@Override
protected void onPreExecute() {
//progressDialog.show();
super.onPreExecute();
}
@Override
public void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
//progressDialog.dismiss();
}
@Override
public Void doInBackground(Void... voids) {
ProcessXml(Getdata());
return null;
}
public ArrayList<String> ProcessXml(Document data) {
if (data!=null) {
titles=new ArrayList<>();
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 currentchild=items.item(i);
if (currentchild.getNodeName().equalsIgnoreCase("item")){
FeedItem item=new FeedItem();
NodeList itemchilds=currentchild.getChildNodes();
for (int j=0;j<itemchilds.getLength();j++){
Node current=itemchilds.item(j);
if (current.getNodeName().equalsIgnoreCase("title")){
item.setTitle(current.getTextContent());
titles.add(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")){
String url=current.getAttributes().item(0).getTextContent();
item.setThumbnailUrl(url);
}
}
feedItems.add(item);
Log.d("hello", item.getTitle());
}
}
}
return titles;
}
public Document Getdata(){
try{
url = new URL(address);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
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;
}
}
BlankFragment脚本调用标题。
public class BlankFragment extends Fragment {
ArrayList<FeedItem>feedItems;
ArrayList<String>titles;
String address="https://www.sciencemag.org/rss/news_current.xml";
URL url;
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@TargetApi(Build.VERSION_CODES.N)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
ReadRss readRss = new ReadRss(this, rv);
readRss.getClass();
titles = readRss.titles;
String[] display_titles = (String[]) titles.toArray();
//Document data = readRss.Getdata();
//ArrayList<FeedItem> feedItems = readRss.ProcessXml(data);
MyAdapter adapter = new MyAdapter(display_titles);
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
答案 0 :(得分:0)
这是一种竞争条件,虽然听起来像一条路总能赢得比赛。 您正在使用2个线程;
如果主线程在后台线程实例化之前达到了读取'titles'的程度 - 那么'titles'将为null。
也许在创建AsyncTask之后,您可以继续创建视图 - 显示“加载”,标题将在此处显示。然后当AsyncTask完成时,它可以使用标题更新视图 - onPostExecute()。