1
votes

I have a function like this

  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);
  }

i have an error like at blockquote on that line

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

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

Simply, I want to parcelable some data on my class function CustomerActivity to intent SalesActListActivity but i have error like this. Can anyone help me ? At least, here is my CustomerActivity function

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 cannot be cast to com.kreators.nvmobile.dashboard.SalesActActivity$CustomerActivity at com.kreators.nvmobile.dashboard.SalesActActivity.goToSalesActList(SalesActActivity.java:210) at com.kreators.nvmobile.dashboard.SalesActActivity.access$000(SalesActActivity.java:33) at com.kreators.nvmobile.dashboard.SalesActActivity$1.onItemClick(SalesActActivity.java:256) at android.widget.AdapterView.performItemClick(AdapterView.java:305) at android.widget.AbsListView.performItemClick(AbsListView.java:1146) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053) at android.widget.AbsListView$3.run(AbsListView.java:3865) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5345) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:947) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:742)

that is my error message

Sorry my grammar is bad and I'm newbie 谢谢

2
where are you setting the tag for that view? - SoroushA
when I click a list view, I set that tag for read to the next intent - 한명훈 陳祖亮
Also, the method is VERY wrong :-( You can not put an Activity in the Bundle. What are you trying to achieve with this code? - Kelevandos
i update my question - 한명훈 陳祖亮

2 Answers

1
votes

Replace

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

with

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

In Android every View instance, once created, has access to it's Context, which in most cases* will be an Activity.

*sometimes this will be the Application or another implementation of Context.

0
votes

The following error:

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

is happened because you're trying to cast ViewHolder to CustomerActivity object with:

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

From you comment:

when I click a list view, I set that tag for read to the next intent

you need to set the object with setTag to your clicked item view something like this:

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

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

The above code is only a pseudo code because I can't determine what you're code doing without you giving your Adapter class code.


Side note:
You need to follow a naming convention for Android project. For example, using CustomerActivity name for a pojo is discourage because Android developer usually think every class name ending with Activity is an Activity