0

我正在尝试使用ArrayAdapter带有PageAdapterand的自定义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 行的位置添加了注释。

任何帮助都会很棒。

4

1 回答 1

2

您将应用程序上下文传递给 SettingListAdapter 的构造函数。然后你将该上下文转换为活动((Activity)context),它显然失败了。将活动上下文传递给适配器。更改getActivity().getApplicationContext()getActivity()

于 2013-10-05T20:28:53.347 回答