【问题标题】:java.lang.ClassCastException: com.parse.ParseObject cannot be cast tojava.lang.ClassCastException:com.parse.ParseObject 无法转换为
【发布时间】:2016-06-30 03:10:07
【问题描述】:

我创建了 2 个扩展 ParseObject 的类(MealRestaurant),并创建了 2 个自定义适配器(BottomFragmentMealAdapterRestaurantAdapter)。我尝试为RestaurantAdapter 做任何我在BottomFragmentMealAdapter(它有效)中所做的事情。但它不起作用。
这是我的日志:

致命异常:主要 进程:com.bogazici.menumizer,PID:14739 java.lang.ClassCastException:com.parse.ParseObject 无法转换为 com.bogazici.menumizer.Restaurant 在 com.bogazici.menumizer.RestaurantAdapter.getItemView(RestaurantAdapter.java:17) 在 com.parse.ParseQueryAdapter.getView(ParseQueryAdapter.java:547) 在 android.widget.AbsListView.obtainView(AbsListView.java:2347) 在 android.widget.ListView.makeAndAddView(ListView.java:1864) 在 android.widget.ListView.fillDown(ListView.java:698) 在 android.widget.ListView.fillFromTop(ListView.java:759) 在 android.widget.ListView.layoutChildren(ListView.java:1659) 在 android.widget.AbsListView.onLayout(AbsListView.java:2151) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:581) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1034) 在 android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:744) 在 android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) 在 android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1180) 在 android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:757) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703) 在 android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557) 在 android.widget.LinearLayout.onLayout(LinearLayout.java:1466) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.widget.FrameLayout.layoutChildren(FrameLayout.java:579) 在 android.widget.FrameLayout.onLayout(FrameLayout.java:514) 在 android.view.View.layout(View.java:15671) 在 android.view.ViewGroup.layout(ViewGroup.java:5038) 在 android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086) 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843) 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) 在 android.view.Choreographer.doCallbacks(Choreographer.java:580) 在 android.view.Choreographer.doFrame(Choreographer.java:550) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5257) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(Zyg

这是我的RestaurantAdapter

public class RestaurantAdapter extends ParseQueryAdapter<Restaurant> {
    //top line is 17
    private AlphaAnimation buttonClick;
    public RestaurantAdapter(Context context, final String filter_edit, final String filter_city, final String filter_region,0 final int filter_sort) {
       super(context, new ParseQueryAdapter.QueryFactory<Restaurant>() {
           public ParseQuery<Restaurant> create() {
               // Here we can configure a ParseQuery to display
               // only top-rated meals.
               ParseQuery query = new ParseQuery("Restaurant");
               query.whereContains("city", filter_city);       
    if(!filter_region.equals("*Hepsi*")&&!filter_region.equals("*All*"))
                   query.whereContains("region",filter_region);
               if(filter_edit.equals("nothing")){
               }else{
                   query.whereStartsWith("restaurant", filter_edit);
               }
               switch(filter_sort){
                   case 0:
                       query.orderByAscending("averagePrice");
                       break;
                   case 1:
                       query.orderByDescending("averagePrice");
                       break;
                   case 2:
                       query.orderByAscending("restaurant");
                       break;
                   case 3:
                       query.orderByDescending("averagePoint");
                       break;
                   default:
                       query.orderByAscending("restaurant");
                       break;
               }
               return query;
           }
       });
    }
    @Override
    public View getItemView(Restaurant restaurant, View v, ViewGroup parent) {
        //buttonClick = new AlphaAnimation(1F, 0.8F);
        if (v == null) {
            v = View.inflate(getContext(), R.layout.restaurant_list_item, null);
        }
        super.getItemView(restaurant, v, parent);
        final String str_call = restaurant.getPhone();
        TextView locationTextView = (TextView) v.findViewById(R.id.restaurant_location);
        String str_location= restaurant.getRegion()+", "+restaurant.getCity();
        locationTextView.setText(str_location);
        TextView nameTextView = (TextView) v.findViewById(R.id.restaurant_name);
        nameTextView.setText(restaurant.getName());
        TextView pointTextView = (TextView) v.findViewById(R.id.restaurant_point);
        String str_point = String.valueOf(restaurant.getAveragePoint());
        pointTextView.setText(str_point);
        TextView priceTextView = (TextView) v.findViewById(R.id.restaurant_averageprice);
        String str_price = String.valueOf(restaurant.getAveragePrice())+" TL";
        priceTextView.setText(str_price);
        ImageView callImage = (ImageView) v.findViewById(R.id.restaurant_call);
        callImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View w) {
                // w.startAnimation(buttonClick);
                Intent intent = new Intent(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:"+str_call));
                w.getContext().startActivity(intent);
            }
        });
        TextView mapText = (TextView) v.findViewById(R.id.restaurant_map);
        mapText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View w) {
                //w.startAnimation(buttonClick);
            }
        });
        return v;
    }
}

【问题讨论】:

  • 您在 Restaurant 课程中是否使用了正确的 @ParseClassName("Restaurant)?
  • @ParseClassName("Restaurant") 公共类餐厅扩展 ParseObject { public Restaurant() { } public String getObjectId(){ return getString("objectId"); } ...

标签: android classcastexception


【解决方案1】:

你必须检查两件事

  1. 确保您的子类具有public default(即零参数)构造函数。您不得修改此构造函数中的任何 ParseObject 字段。 和

  2. 在调用Parse.initialize()之前,在您的应用程序构造函数中调用ParseObject.registerSubclass(YourClass.class)

【讨论】:

    【解决方案2】:

    确保您在应用程序中注册了解析类

    【讨论】:

      【解决方案3】:

      我认为 ParseQueryAdapter 需要 ParseObject 作为泛型,而 'Restaurant' 类不是 ParseObject 类型

      【讨论】:

        【解决方案4】:

        将您的 Adapter's getItemView() 方法更改为 -

         @Override
            public View getItemView(ParseObject parseObject, View v, ViewGroup parent) {
        }
        

        【讨论】:

        • @ParseClassName("Meal") public class Meal extends ParseObject{ public Meal() { } // 有效 @ParseClassName("Restaurant") public class Restaurant extends ParseObject { public Restaurant() { } //不起作用。
        猜你喜欢
        • 1970-01-01
        • 2023-02-02
        • 2016-01-14
        • 2013-07-25
        • 2016-07-05
        • 2017-01-24
        • 2018-08-03
        • 2016-01-07
        • 2015-07-16
        相关资源
        最近更新 更多