Java 从另一个类检索到的ArrayList为null

Java 从另一个类检索到的ArrayList为null,java,android,arraylist,null,Java,Android,Arraylist,Null,我试图访问在另一个类中创建的arraylist以执行搜索功能。但是,arraylist为空 `public class RssParser extends Activity{ Calendar calendar = null; List<RssItem> items = new ArrayList<RssItem>(); ArrayList<String> list; @Override public void onCreate(Bundle sa

我试图访问在另一个类中创建的arraylist以执行搜索功能。但是,arraylist为空

 `public class RssParser extends Activity{
    Calendar calendar = null;
List<RssItem> items = new ArrayList<RssItem>();
ArrayList<String> list;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState== null){
    setContentView(R.layout.fragment_layout);}}

public List<RssItem> build(InputStream inputStream) throws XmlPullParserException, IOException, ParserException {
    try {
        CalendarBuilder builder = new CalendarBuilder();

        calendar = builder.build(inputStream);

        return readFeed(calendar);
    } finally {
        inputStream.close();
    }
}

private List<RssItem> readFeed(Calendar calendar) throws XmlPullParserException, IOException {

    java.util.Calendar today= java.util.Calendar.getInstance();
    Period period= new Period(new DateTime(today.getTime()), new Dur(today.getTime(), today.getTime()) );
     Filter filter= new Filter(new PeriodRule(period));
    ComponentList events = (ComponentList) filter.filter(calendar.getComponents(Component.VEVENT));

     for (Object balling: events){
         VEvent event1 = (VEvent) balling;
        Date jobs = event1.getStartDate().getDate();
         String honey = null;
        String event = event1.getSummary().getValue();
         String happy=event1.getCategories().getValue();
          list= new ArrayList<String>();
         list.add(event);
         list.add(happy);
         Log.i ("list", ""+ list);

        SimpleDateFormat format =
                 new SimpleDateFormat("MMM dd yyyy h:mm a");
         SimpleDateFormat format2 =
                 new SimpleDateFormat("MMM dd yyyy");
         if (jobs.toString().length()>12){
         DateFormat[] formats = new DateFormat[]{
                     format
             };
         for (DateFormat df : formats) {
              honey = (df.format(jobs));
         }}
         else if( jobs.toString().length()!= 0){
             DateFormat[] formats = new DateFormat[] {
                     format2
             };
             for (DateFormat df : formats) {
                 honey = (df.format(jobs));
             }}
         else {honey= "no time/date given";}


        if (happy.equals ("KGHS Athletics" )){
             if (event != null) {
                 RssItem time= new RssItem(event, honey, happy);
                 items.add(time);

             }}
          if (happy.equals("Division Testing")){
              if (event != null) {
             RssItem time= new RssItem(event, honey, happy);
             items.add(time);

           }
     }
         if (happy.equals("King George High")){
             if (event != null) {
                 RssItem time= new RssItem(event, honey, happy);
                 items.add(time);
             }
         }
    if (happy.equals("Division Calendar")){
        if (event != null) {
            RssItem time= new RssItem(event, honey, happy);
            items.add(time);
        }
    }}
    return items;
}
public ArrayList<String> getList(){


    return list;
}

}`

看起来检索列表的工作一直持续到某一点,因为日志记录在第一行显示了一些内容。但不久之后,它以同样的方式崩溃了。我不能100%确定RssParser.java中的第70行是哪一行,但看起来
jobs
可以为空。


您是否尝试使用调试器逐步检查代码?您能验证
作业的正确值吗?

哦,对不起,这是我尝试时的日志:如果没有任何事件,请尝试{this.readFeed(calendar);}catch(XmlPullParserException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}(因此for循环不起任何作用)然后列表将为空。如果有2个以上的事件,则列表似乎只包含最后一个事件。为什么列表是在for循环中创建的而不是在for循环之外创建的?否则,它只会说I/ArrayList:null I/list:不是空的,但感谢循环的提示。事实上,它只是一步一步地在cod中循环使用调试器时,它是否在任何地方崩溃?也许您应该添加更多日志记录,以便更容易查看代码崩溃的位置?是的,除了I/:arrayList为null之外,没有什么异常。另一个类中的arrayList应该返回它,但它不是。您希望收到哪个列表?
list items=new arrayList()
数组列表;
?由于
生成
读取提要
返回
,以及
获取列表
返回
列表
?列出RssItem one是列表视图的getter setter,因此如果
事件
为空,则
列表
为空。如果对于
事件
中的每个项,
列表
将被重新创建,因此添加到
列表中的所有项目都将消失。
public class RssFragment extends Fragment implements AdapterView.OnItemClickListener, SearchView.OnQueryTextListener,
    SearchView.OnCloseListener {

private ProgressBar progressBar;
private View view;
private JazzyListView listView;
private SearchView searchView;
private SearchableActivity mDbHelper;
private RssAdapter adapter;
ArrayList<String> array;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState==null) {

         mDbHelper = new SearchableActivity(getActivity().getBaseContext());
        try {
            mDbHelper.open();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        //Clear all names
        mDbHelper.deleteAllNames();}

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (savedInstanceState == null){
    if (view == null ) {

        view = inflater.inflate(R.layout.fragment_layout, container, false);
        progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
        listView = (JazzyListView) view.findViewById(R.id.listView);
        searchView = (SearchView) view.findViewById(R.id.searchView);

        //Sets the default or resting state of the search field. If true, a single search icon is shown by default and
        // expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it
        // collapses to that state when the close button is pressed. Changes to this property will take effect immediately.
        //The default value is true.
        searchView.setIconifiedByDefault(false);

        searchView.setOnQueryTextListener(this);
        searchView.setOnCloseListener(this);
        listView.setOnItemClickListener(this);
        listView.setSmoothScrollbarEnabled(true);


        startService();}
     else {
        // If we are returning from a configuration change:
        // "view" is still attached to the previous view hierarchy
        // so we need to remove it and re-attach it to the current one
        ViewGroup parent = (ViewGroup) view.getParent();
        parent.removeView(view);
    }}
    return view;
}

private void startService() {
    Intent intent = new Intent(getActivity(), RssService.class);
    intent.putExtra(RssService.RECEIVER, resultReceiver);
    getActivity().startService(intent);
}

/**
 * Once the {@link RssService} finishes its task, the result is sent to this ResultReceiver.
 */
private final ResultReceiver resultReceiver = new ResultReceiver(new Handler()) {
    @SuppressWarnings("unchecked")
    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {

  List<RssItem> items = (List<RssItem>) resultData.getSerializable(RssService.ITEMS);
        adapter = new RssAdapter(getActivity(), items);


        if (items != null) {
            listView.setAdapter(adapter);
            RssParser parser= new RssParser();
           array= parser.getList();
            Log.i("array List", "" + array);
            for (String name : array) {
                mDbHelper.createList(name);
            }

        } else {
            Toast.makeText(getActivity(), "The feed is unable to be downloaded at this time",
                    Toast.LENGTH_LONG).show();
        }

        progressBar.setVisibility(View.GONE);
        listView.setVisibility(View.VISIBLE);
         listView.setTransitionEffect(JazzyHelper.GROW);
    };
};

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    RssAdapter adapter = (RssAdapter) parent.getAdapter();
    RssItem item = (RssItem) adapter.getItem(position);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    startActivity(intent);
}
@Override
public void onDestroy() {
    super.onDestroy();

    if (mDbHelper  != null) {
        mDbHelper.close();
    }
}

@Override
public boolean onClose() {
    listView.setAdapter(adapter);
    return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
    displayResults(query + "*");
    return false;
}

@Override
public boolean onQueryTextChange(String newText) {
    if (!newText.isEmpty()){
        displayResults(newText + "*");
    } else {
        listView.setAdapter(adapter);
    }

    return false;
}

/**
 * Method used for performing the search and displaying the results. This method is called every time a letter
 * is introduced in the search field.
 *
 * @param query Query used for performing the search
 */
private void displayResults(String query) {

    Cursor cursor = null;
    try {
        cursor = mDbHelper.searchByInputText((query != null ? query : "@@@@"));
    } catch (SQLException e) {
        e.printStackTrace();
    }

    if (cursor != null) {

        String[] from = new String[] {SearchableActivity.COLUMN_NAME};

        // Specify the view where we want the results to go
        int[] to = new int[] {R.id.listView};

        // Create a simple cursor adapter to keep the search data
        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(getActivity().getBaseContext(), R.layout.fragment_layout, cursor, from, to);
       listView.setAdapter(cursorAdapter);

        // Click listener for the searched item that was selected
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView parent, View view, int position, long id) {

                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String selectedName = cursor.getString(cursor.getColumnIndexOrThrow("name"));
                Toast.makeText(getActivity(), selectedName, Toast.LENGTH_SHORT).show();

                // Set the default adapter
                listView.setAdapter(adapter);

                // Find the position for the original list by the selected name from search
                for (int pos = 0; pos < array.size();
                pos++){
                    if (array.get(pos).equals(selectedName)) {
                        position = pos;
                        break;
                    }
                }

                // Create a handler. This is necessary because the adapter has just been set on the list again and
                // the list might not be finished setting the adapter by the time we perform setSelection.
                Handler handler = new Handler();
                final int finalPosition = position;
                handler.post(new Runnable() {
                    @Override
                    public void run() {

                        listView.setSelection(finalPosition);
                    }
                });

                searchView.setQuery("", true);
            }


        });

    }
}


}
  public class RssItem {

    private final String title;
    private final String date2;
private final String categories;
    public RssItem(String title, String date2, String categories) {
        this.title = title; this.date2= date2; this.categories= categories;
    }
 public String getTitle() {
        return title;
    }
    public String getDate(){return date2;}
public String getCategories(){return categories;}}