我正在尝试访问我在另一个类中创建的arraylist,以执行搜索功能。但是,arraylist为null。
`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;
}
}`
这是我试图获得arraylist的课程。
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);
}
});
}
}
}
所以基本上我要做的就是从我为searchview下载的ics文件中获取一个arraylist。但是由于类执行的顺序,这个arraylist是null。如果我展示RssItem,也许会有所帮助。
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;}}
答案 0 :(得分:0)
由于日志记录显示第一行中的某些内容,因此检索列表可能会达到某一点。但不久之后,在同样的方法中,它崩溃了。我不是百分之百确定RssParser.java中哪一行是第70行,但看起来jobs
可以为空。
您是否尝试使用调试器单步执行代码?您可以验证jobs
的正确值吗?