When calling the startActivityForResult() method from its own adapter, an error is displayed

 E/AndroidRuntime: FATAL EXCEPTION: main Process: ilhom4ik_app.com.girbar, PID: 9561 java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity at ilhom4ik_app.com.girbar.ItemsAdapter$1.onClick(ItemsAdapter.java:65) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Calling activation from the adapter like this

 holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent item = new Intent(mContext,ItemActivity_level1.class); item.putExtra("item",itemList.getUrl()); item.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Activity activity = (Activity) mContext; activity.startActivityForResult(item,1); } }); 
  • Specify how you transfer mContext to the adapter? - Vladimir Yarovoy
  • adapter = new ItemsAdapter (listItems, getApplicationContext ()); - ILHOM4IK
  • one
    Well, yes, I correctly suggested. You transfer the application context, but you need to transfer the activation context. - Vladimir Yarovoy
  • Thanks worked - ILHOM4IK

1 answer 1

It looks like you are simply passing this from your activation to the adapter, and you need to transfer ActivityName.this to transfer the activation context, not the application context to the adapter. UPD: instead of getApplicationContext () you need to pass _name_._.this

  • Thank you very much for the answer ! Works ! - ILHOM4IK