【发布时间】:2013-12-14 23:09:04
【问题描述】:
这是调用列表适配器的主要活动,应用程序每次都会崩溃,因为从未调用过 getview,因此永远不会加载列表。
MainActivity
public class MainActivity extends FragmentActivity {
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
SectionsPagerAdapter
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/** * 一个代表应用程序部分的虚拟片段,但这只是 * 显示虚拟文本。 */
DummySectionFragment
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy, container, false);
//itcItems = (ListView)rootView.findViewById(R.id.streamList);
//itcItems.setAdapter(adapter);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
MyAsyncTask task = new MyAsyncTask(getActivity());
task.execute("http://findaway.in/card/restlist.xml");
super.onActivityCreated(savedInstanceState);
}
}
static class MyAsyncTask extends AsyncTask<String, Void, List<Data> > {
Activity mContext;
//Response response;
public MyAsyncTask(Activity context) {
this.mContext=context;
}
protected List<Data> doInBackground(String... urls) {
// Debug the task thread name
Log.d("ITCRssReader", "inside");
try {
// Create RSS reader
RssReader rssReader = new RssReader(urls[0]);
Log.d("ITCRssReader", "inside1");
// Parse RSS, get items
//Log.d("ITCRssReader", rssReader.getItems().get(3).getName());
return rssReader.getItems();
} catch (Exception e) {
Log.e("ITCRssReader", "error");
}
return null;
}
protected void onPostExecute(List<Data> result) {
Log.d("RESULT", result.get(3).getName());
// Get a ListView from main view
ListView itcItems = (ListView)mContext.findViewById(R.id.streamList);
ListAdapter la = new ListAdapter(mContext,R.layout.fragment_main_dummy, result);
// Create a list adapter
itcItems.setAdapter(la);
}
}
}`
这是我的适配器代码 "Log.d("list",obj.getName())" 永远不会被调用 列表适配器
public class ListAdapter extends ArrayAdapter<Data> {
Context context;
// List values
List<Data> foodList;
public ListAdapter(Context context, int resource , List<Data> result) {
super(context, resource, result);
//Log.d("RESULT2", result.get(3).getName());
this.context = context;
this.foodList = result;
Log.d("RESULT2", foodList.get(3).getName());
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//View rowView=convertView;
// ImageHolder holder = null;
Data obj = foodList.get(position);
Log.d("list",obj.getName());
LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.fragment_main_dummy,parent,false);
TextView n = (TextView)rowView.findViewById(R.id.name);
TextView d = (TextView)rowView.findViewById(R.id.discount);
TextView l = (TextView)rowView.findViewById(R.id.Type);
n.setText(obj.getName());
d.setText(obj.getDiscount());
l.setText(obj.getLocation());
return rowView;
}
}
【问题讨论】:
-
“应用程序崩溃”不是很有帮助。您必须发布 LogCat 错误消息,顺便说一下,这将帮助您了解发生了什么问题。
-
snag.gy/nel1p.jpg 这是 logcat 截图
标签: android android-fragments adapter listadapter