我正在尝试将自定义ArrayAdapter
与PageAdapter
和ListFragments
一起使用。我遇到getView
方法与ClassCastException
崩溃的问题,我无法弄清楚我做错了什么。
以下是MainListFragment
的代码,到目前为止一切正常。我还应该指出这是一个静态的内部类:
public static class MainListFragment extends ListFragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
int fragNum;
public static final String ARG_SECTION_NUMBER = "section_number";
private static MainListFragment init(int val) {
MainListFragment mfl = new MainListFragment();
Bundle args = new Bundle();
args.putInt("val", val);
mfl.setArguments(args);
return mfl;
}
public MainListFragment() {
}
/**
* Retrieving this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragNum = getArguments() != null ? getArguments().getInt("val") : 1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
int page = getArguments().getInt(ARG_SECTION_NUMBER);
View rootView = inflater.inflate(R.layout.setting_fragment,
container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String arr[] = { "This is", "a ", "Demo", "App", "For", "Showing",
"FragmentPagerAdapter", "and ViewPager", "Implementation" };
SettingListAdapter settingAdapter = new SettingListAdapter(
getActivity().getApplicationContext(),
R.layout.setting_item_row, arr);
ListView listView = (ListView) getActivity().findViewById(android.R.id.list);
listView.setAdapter(settingAdapter);
}
}
以下是我的自定义ArrayAdapter
的代码:
public class SettingListAdapter extends ArrayAdapter<String> {
Context context;
int layoutResourceId;
String[] arr;
public SettingListAdapter(Context context, int layoutResourceId,
String[] arr) {
super(context, layoutResourceId, arr);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.arr = arr;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ProfileHolder holder;
if(row == null){
//this is line 35 from the error
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ProfileHolder();
holder.title = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else{
holder = (ProfileHolder)row.getTag();
}
String profile = arr[position];
holder.title.setText(profile);
return row;
}
static class ProfileHolder{
TextView title;
}
}
这是我得到的堆栈跟踪:
10-05 13:14:00.104: E/ListView(4863): android.app.Application
10-05 13:14:00.104: E/ListView(4863): java.lang.ClassCastException: android.app.Application
10-05 13:14:00.104: E/ListView(4863): at com.redclay.fanaddict.helpers.SettingListAdapter.getView(SettingListAdapter.java:35)
10-05 13:14:00.104: E/ListView(4863): at android.widget.AbsListView.obtainView(AbsListView.java:1449)
10-05 13:14:00.104: E/ListView(4863): at android.widget.ListView.makeAndAddView(ListView.java:1801)
10-05 13:14:00.104: E/ListView(4863): at android.widget.ListView.fillDown(ListView.java:671)
10-05 13:14:00.104: E/ListView(4863): at android.widget.ListView.fillFromTop(ListView.java:728)
10-05 13:14:00.104: E/ListView(4863): at android.widget.ListView.layoutChildren(ListView.java:1647)
10-05 13:14:00.104: E/ListView(4863): at android.widget.AbsListView.onLayout(AbsListView.java:1279)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1388)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-05 13:14:00.104: E/ListView(4863): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:00.104: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:00.104: E/ListView(4863): at android.view.ViewRoot.performTraversals(ViewRoot.java:1217)
10-05 13:14:00.104: E/ListView(4863): at android.view.ViewRoot.handleMessage(ViewRoot.java:1973)
10-05 13:14:00.104: E/ListView(4863): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 13:14:00.104: E/ListView(4863): at android.os.Looper.loop(Looper.java:150)
10-05 13:14:00.104: E/ListView(4863): at android.app.ActivityThread.main(ActivityThread.java:4293)
10-05 13:14:00.104: E/ListView(4863): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 13:14:00.104: E/ListView(4863): at java.lang.reflect.Method.invoke(Method.java:507)
10-05 13:14:00.104: E/ListView(4863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 13:14:00.104: E/ListView(4863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 13:14:00.104: E/ListView(4863): at dalvik.system.NativeStart.main(Native Method)
10-05 13:14:01.836: E/ListView(4863): android.app.Application
10-05 13:14:01.836: E/ListView(4863): java.lang.ClassCastException: android.app.Application
10-05 13:14:01.836: E/ListView(4863): at com.redclay.fanaddict.helpers.SettingListAdapter.getView(SettingListAdapter.java:35)
10-05 13:14:01.836: E/ListView(4863): at android.widget.AbsListView.obtainView(AbsListView.java:1449)
10-05 13:14:01.836: E/ListView(4863): at android.widget.ListView.makeAndAddView(ListView.java:1801)
10-05 13:14:01.836: E/ListView(4863): at android.widget.ListView.fillDown(ListView.java:671)
10-05 13:14:01.836: E/ListView(4863): at android.widget.ListView.fillFromTop(ListView.java:728)
10-05 13:14:01.836: E/ListView(4863): at android.widget.ListView.layoutChildren(ListView.java:1647)
10-05 13:14:01.836: E/ListView(4863): at android.widget.AbsListView.onLayout(AbsListView.java:1279)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1388)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
10-05 13:14:01.836: E/ListView(4863): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
10-05 13:14:01.836: E/ListView(4863): at android.view.View.layout(View.java:7292)
10-05 13:14:01.836: E/ListView(4863): at android.view.ViewRoot.performTraversals(ViewRoot.java:1217)
10-05 13:14:01.836: E/ListView(4863): at android.view.ViewRoot.handleMessage(ViewRoot.java:1973)
10-05 13:14:01.836: E/ListView(4863): at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 13:14:01.836: E/ListView(4863): at android.os.Looper.loop(Looper.java:150)
10-05 13:14:01.836: E/ListView(4863): at android.app.ActivityThread.main(ActivityThread.java:4293)
10-05 13:14:01.836: E/ListView(4863): at java.lang.reflect.Method.invokeNative(Native Method)
10-05 13:14:01.836: E/ListView(4863): at java.lang.reflect.Method.invoke(Method.java:507)
10-05 13:14:01.836: E/ListView(4863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-05 13:14:01.836: E/ListView(4863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-05 13:14:01.836: E/ListView(4863): at dalvik.system.NativeStart.main(Native Method)
我已在ArrayAdapter
类的第35行添加注释。
任何帮助都会很棒。
答案 0 :(得分:2)
您正在将应用程序上下文传递给SettingListAdapter的构造函数。然后将该上下文转换为活动((Activity)context
),它显然失败了。
将活动上下文传递给适配器将getActivity().getApplicationContext()
更改为getActivity()
。