当前位置: 首页 > 知识库问答 >
问题:

java.lang.ClassCastException:类/活动不能强制转换为MainActivity

包修贤
2023-03-14

我只想把这个支付成功后的片段称为状态,但我不知道如何让它工作。

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

PaymentGatWay类

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

输出错误

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)

共有1个答案

文凯康
2023-03-14

java.lang.ClassCastException:Gogrocer.tcc.PaymentGatWay不能强制转换为Gogrocer.tcc.MainActivity

更改此:

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

对此:

((PaymentGatWay) getActivity()).setTitle(getResources().getString(R.string.thank_you));
getActivity().setTitle(getResources().getString(R.string.thank_you));
 类似资料: