java.lang.ClassCastException转换为Activity的适配器

时间:2018-03-05 18:33:16

标签: java android listview

我有这样的功能

  private void goToSalesActList(View view) {
    CustomerActivity ca = (CustomerActivity)view.getTag();
    Intent intent = new Intent(this, SalesActListActivity.class);
    Bundle bundle = new Bundle();
    bundle.putParcelable(Const.OBJ_USER, user);
    bundle.putParcelable("CustomerListActivity", ca);
    intent.putExtras(bundle);
    startActivity(intent);
  }

我在该行的blockquote上有一个错误

  

java.lang.ClassCastException:com.kreators.nvmobile.dashboard.AdapterSalesAct $ ViewHolder无法强制转换为com.kreators.nvmobile.dashboard.SalesActActivity $ CustomerActivity

CustomerActivity ca = (CustomerActivity)view.getTag();

简单地说,我想将我的类函数CustomerActivity上的一些数据分配给intentActListActivity,但我有这样的错误。谁能帮我 ? 至少,这是我的CustomerActivity函数

static class CustomerActivity implements Parcelable {
public int id, qty, AROD, HAVE, Visit;
public String code, name, sales, salesname, target, amt, psi;

public CustomerActivity(JSONObject o) {
  try {
    id = o.getInt("CustId");
    code = o.getString("CustCode");
    name = o.getString("CustDesc");
    salesname = o.getString("Salesname");
    sales = o.getString("Sales");
    target = o.getString("SITarget");
    amt = o.getString("SIAmt");
    psi = o.getString("persenPSI");
    qty = o.getInt("SOQty");
    AROD= o.getInt("AROD");
    HAVE= o.getInt("HAVE");
    Visit= o.getInt("Visit");
  } catch (JSONException e) {}
}

protected CustomerActivity(Parcel in) {
  id = in.readInt();
  qty = in.readInt();
  code = in.readString();
  name = in.readString();
  salesname = in.readString();
  sales = in.readString();
  target = in.readString();
  amt = in.readString();
  psi = in.readString();
  AROD = in.readInt();
  HAVE = in.readInt();
  Visit = in.readInt();
}

public static final Creator<CustomerActivity> CREATOR = new Creator<CustomerActivity>() {
  @Override
  public CustomerActivity createFromParcel(Parcel in) {
    return new CustomerActivity(in);
  }

  @Override
  public CustomerActivity[] newArray(int size) {
    return new CustomerActivity[size];
  }
};

@Override
public int describeContents() {
  return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
  parcel.writeInt(id);
  parcel.writeInt(qty);
  parcel.writeString(code);
  parcel.writeString(name);
  parcel.writeString(salesname);
  parcel.writeString(sales);
  parcel.writeString(target);
  parcel.writeString(amt);
  parcel.writeString(psi);
  parcel.writeInt(AROD);
  parcel.writeInt(HAVE);
  parcel.writeInt(Visit);
}

}

  

java.lang.ClassCastException:com.kreators.nvmobile.dashboard.AdapterSalesAct $ ViewHolder无法强制转换为com.kreators.nvmobile.dashboard.SalesActActivity $ CustomerActivity                         在com.kreators.nvmobile.dashboard.SalesActActivity.goToSalesActList(SalesActActivity.java:210)                         在com.kreators.nvmobile.dashboard.SalesActActivity.access $ 000(SalesActActivity.java:33)                         在com.kreators.nvmobile.dashboard.SalesActActivity $ 1.onItemClick(SalesActActivity.java:256)                         在android.widget.AdapterView.performItemClick(AdapterView.java:305)                         在android.widget.AbsListView.performItemClick(AbsListView.java:1146)                         在android.widget.AbsListView $ PerformClick.run(AbsListView.java:3053)                         在android.widget.AbsListView $ 3.run(AbsListView.java:3865)                         在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:5345)                         at java.lang.reflect.Method.invoke(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:372)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:947)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)

这是我的错误消息

抱歉,我的语法很糟糕,我是新手 谢谢

2 个答案:

答案 0 :(得分:1)

替换

CustomerActivity ca = (CustomerActivity)view.getTag();

CustomerActivity ca = (CustomerActivity)view.getContext();

在Android中,每个View个实例一旦创建,就可以访问Context,在大多数情况下*会是Activity

*有时这将是ApplicationContext的其他实现。

答案 1 :(得分:0)

以下错误:

java.lang.ClassCastException: com.kreators.nvmobile.dashboard.AdapterSalesAct$ViewHolder cannot be cast to com.kreators.nvmobile.dashboard.SalesActActivity$CustomerActivity
发生了

因为你正试图将ViewHolder强制转换为CustomerActivity对象:

private void goToSalesActList(View view) {
  CustomerActivity ca = (CustomerActivity)view.getTag();
  ...
}

发表评论:

  

当我单击列表视图时,我将该标记设置为读取到下一个意图

你需要使用setTag将对象设置为你点击的项目视图:

// selected item view
View view;
...
CustomerActivity selectedItem;
...
view.setTag(selectedItem);

// Then you can call the method with item
goToSalesActList(view);

上面的代码只是一个伪代码,因为如果不提供适配器类代码,我就无法确定您的代码。

旁注:
您需要遵循Android项目的命名约定。例如,对于pojo使用CustomerActivity名称是不鼓励的,因为Android开发人员通常认为以Activity结尾的每个类名都是Activity