0

ArrayAdapterと でカスタムを使用しようとしていPageAdapterますListFragmentsgetViewメソッドがクラッシュするという問題があり、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 に答える