调用自定义arrayadapter实现时的ClassCastException

时间:2014-06-24 07:21:38

标签: android android-fragments android-arrayadapter classcastexception

我一直在重构我的代码并将我的活动变成片段,以便容纳导航抽屉。我现在遇到了一个问题,我的ArrayAdapter实现抛出了ClassCastException:

06-24 09:03:22.120: E/AndroidRuntime(27106): FATAL EXCEPTION: main
06-24 09:03:22.120: E/AndroidRuntime(27106): java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
06-24 09:03:22.120: E/AndroidRuntime(27106):    at nl.tizin.healthapp.utils.CustomGridViewAdapter.getView(CustomGridViewAdapter.java:54)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.AbsListView.obtainView(AbsListView.java:2177)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.ListView.onMeasure(ListView.java:1159)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.View.measure(View.java:15848)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.Choreographer.doFrame(Choreographer.java:532)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.os.Handler.handleCallback(Handler.java:730)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.os.Looper.loop(Looper.java:137)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at android.app.ActivityThread.main(ActivityThread.java:5103)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at java.lang.reflect.Method.invoke(Method.java:525)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-24 09:03:22.120: E/AndroidRuntime(27106):    at dalvik.system.NativeStart.main(Native Method)

我的getView()方法:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    SmallGoalCard smallGoalCard = null;
    if (row == null) {
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        smallGoalCard = new SmallGoalCard();
        smallGoalCard.picture = (ImageView) row.findViewById(R.id.grid_item_goal_image);
        smallGoalCard.title = (TextView) row.findViewById(R.id.grid_item_goal_title);
        row.setTag(smallGoalCard);
    } else {
        smallGoalCard = (SmallGoalCard) row.getTag();
    }
    System.out.println();
    String[] goalData = (String[]) data.get(position);
    smallGoalCard.title.setText(goalData[0]);
    int category = Integer.parseInt(goalData[1]);
    if(category == SharedData.EXERCISE_CARD.getIntValue()){
        smallGoalCard.picture.setImageResource(R.drawable.exercise_goal_thumb);
    }else if(category == SharedData.FOOD_CARD.getIntValue()){
        smallGoalCard.picture.setImageResource(R.drawable.food_goal_thumb);
    }
    return row;
}

sLayoutInflater inflater =((Activity)mContext).getLayoutInflater(); 是发生错误的地方。在重构之前,CustomGridViewAdapter的调用者是一个Activity。现在,它是一个片段,这可能是错误的原因。现在的问题是我该如何解决这个问题?

编辑:在这里调用CustomGridViewAdapter:

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.goals_main_screen, container, false);
    mCtx = getActivity().getApplicationContext();
    mListView = (ListView) view.findViewById(R.id.goal_listview);
    customGridAdapter = new CustomGridViewAdapter(mCtx, R.layout.card_grid_item, mGridArray);
    customGridAdapter.registerDataSetObserver(new DataSetObserver(){
        @Override
        public void onChanged(){
            retrieveCurrentGoals();
        }
    });

    mListView.setAdapter(customGridAdapter);
    mListView.setOnItemClickListener(new OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            goToDetailedGoalView((int) id);
        }
    });

    retrieveCurrentGoals();
    return view;
}

3 个答案:

答案 0 :(得分:1)

<强>问题:

mCtx = getActivity().getApplicationContext();

您正在尝试获取应用程序上下文

应该是活动上下文而不是整个应用程序上下文,从而为您提供

java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity

<强>溶液

mCtx = getActivity(); //just your current activity context.

答案 1 :(得分:0)

您不需要将Context强制转换为Activity来检索LayoutInflater。你可以使用

 LayoutInflater inflater = LayoutInflater.from(mContext);

 LayoutInflater inflater (LayoutInflater) = mContext.getSystemService(Service.LAYOUT_INFLATER_SERVICE);

答案 2 :(得分:0)

使用它:

LayoutInflater inflater =LayoutInflater.from(mContext); 

而不是:

LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();