2

i just want to call this fragment after payment success status, but i don't know how to make it works.

Error on ((MainActivity) getActivity()).setTitle(getResources().getString(R.string.thank_you));

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_order_thanks, container, false);

        ((MainActivity) getActivity()).setTitle(getResources().getString(R.string.thank_you));
        preferences = getActivity().getSharedPreferences("lan", MODE_PRIVATE);
        language=preferences.getString("language","");

        view.setFocusableInTouchMode(true);
        view.requestFocus();

The PaymentGatWay Class

public class PaymentGatWay extends Activity{

. . .

protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_instamojo_payment);
            sessionManagement = new Session_management(PaymentGatWay.this);

public void onResponse(JSONObject response) { Log.d(TAG, response.toString());

            try {
                Boolean status = response.getBoolean("responce");
                if (status) {
                    String msg = response.getString("data");
                    String msg_arb=response.getString("data");
                    db_cart.clearCart();
                    Bundle args = new Bundle();
                    Fragment fm = new Thanks_fragment();
                    args.putString("msg", msg);
                    args.putString("msgarb",msg_arb);
                    fm.setArguments(args);


                    FragmentManager fragmentManager = getFragmentManager();

                    FragmentTransaction ft = fragmentManager.beginTransaction();
                    ft.replace(R.id.contentPanel, fm)
                            .addToBackStack(null).commit();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

output error

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.gogrocer.tcc, PID: 9816
    java.lang.ClassCastException: gogrocer.tcc.PaymentGatWay cannot be cast to gogrocer.tcc.MainActivity
        at Fragment.Thanks_fragment.onCreateView(Thanks_fragment.java:51)
        at android.app.Fragment.performCreateView(Fragment.java:2508)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)
        at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2407)
        at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2186)
        at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2142)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2043)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:719)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

2 Answers 2

2

java.lang.ClassCastException: gogrocer.tcc.PaymentGatWay cannot be cast to gogrocer.tcc.MainActivity

change this:

((MainActivity) getActivity()).setTitle(getResources().getString(R.string.thank_you));

to this:

((PaymentGatWay) getActivity()).setTitle(getResources().getString(R.string.thank_you));

Or change it to:

getActivity().setTitle(getResources().getString(R.string.thank_you));
Sign up to request clarification or add additional context in comments.

6 Comments

it's working, but it create an issue, I think that's a simple way to do it, let me explain the details.
it's working, but it create an issue, I think that's a simple way to do it, let me explain the details. All the calls getActivity()).setTitle uses MainActivity is there a way to call it without changing from MainActivity to PaymentGatWay Every class that works with MainActivity extends Fragment While PaymentGatWay extends Activity
@AfonsoCardozoCruz i just add another option
It works too but, i'm using an method called updateHeader() that can't be resolved without ((MainActivity) getActivity())
@AfonsoCardozoCruz Copy that method (updateHeader()) to PaymentGatWay OR copy to a base activity class and inherit all activities from it OR make an interface and implement it
|
1

You have to check which activity you are getting from getActivity().

  if(getActivity() is PaymentGatWay){
        (getActivity() as 
         PaymentGatWay).setTitle(getResources().getString(R.string.thank_you));
      }

2 Comments

i'll try your solution, but how can I check if getActivity() is PaymentGatWay
try above solution in that i have already added code to check activity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.