当我应该得到<String>一个**编辑**时返回空



编辑忘记包括实际问题。

soo,我知道我的文件正在返回null,因为我的" getData()"呼叫正在流过我的其余scrips,导致错误。但是,我不知道如何预防。有人可以指向正确的方向吗?

问题我遇到的错误是 Attempt to invoke virtual method 'java.lang.Object[] java.util.ArrayList.toArray()' on a null object reference

从我使用该行的时候我的理解 titles = readRss.titles; 它通过此顺序提取无效响应。ReadRSS> processxml()> getData()并且由于获取数据是"尝试"捕获"部分,其返回XMLDOC和NULL。

当我在processxml()中登录时,我可以获得完整的标题列表,我可以单独记录feed项目,但是如果我尝试在此外登录它将返回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;
}


}

这是一个比赛条件,尽管听起来一条路总是赢得比赛。您正在使用2个线程;

  1. 线程1(也许是UI线程) - 创建readrss-读取'title'
  2. 背景线程 - 执行getData() - 执行processxml() - 设置'title'

如果主线程到达背景线程实例化之前读取"标题"的点 - 然后"标题"将为null。

也许在创建可以继续创建视图 - 显示标题将进入的"加载"之后。然后,完成异步函数时,可以使用标题更新视图-Onpostexecute()。

相关内容

  • 没有找到相关文章

最新更新