相关文章推荐
爱喝酒的双杠  ·  Visual Studio 2022 ...·  1 年前    · 
潇洒的硬币  ·  javascript - Firefox: ...·  2 年前    · 
帅气的猴子  ·  Canvas 类 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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)
  

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));
                it's working, but it create an issue, I think that's a simple way to do it, let me explain the details.
– Afonso Cardozo Cruz
                Apr 16, 2020 at 20:45
                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
– Afonso Cardozo Cruz
                Apr 16, 2020 at 20:50
                It works too but, i'm using an method called updateHeader() that can't be resolved without ((MainActivity) getActivity())
– Afonso Cardozo Cruz
                Apr 16, 2020 at 21:25
                @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
– ygngy
                Apr 16, 2020 at 21:30

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.