如何从 MainActivity 转到 Fragment?

How to go from MainActivity to Fragment?

我启动了 android 工作室并创建了一个新项目(文件-> 新建-> 导航抽屉 activity)并为我自己添加了一些有用的代码。现在我的代码工作正常。但是代码中有问题。我在工具栏中创建了一个菜单按钮,并为这个菜单按钮编写了代码。

main.xml(菜单->main.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/spinner"
        android:title="Spinning"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:backgroundTint="#000000"
        app:actionViewClass="android.widget.Spinner"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_cart"
        android:icon="@drawable/card"
        android:title="Cart"
        app:actionLayout="@layout/custom_action_item_layout"
        app:showAsAction="always" />

</menu>

custom_action_item_layout.xml(对于main.xml)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    style="?attr/actionButtonStyle"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:focusable="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/card"/>

    <TextView
        android:id="@+id/cart_badge"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="right|end|top"
        android:layout_marginEnd="-5dp"
        android:layout_marginRight="-5dp"
        android:layout_marginTop="3dp"
        android:background="@drawable/badge_background"
        android:gravity="center"
        android:padding="3dp"
        android:textColor="@android:color/white"
        android:text="0"
        android:textSize="10sp"/>

</FrameLayout>

结果

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        MyToolMenu = menu;
        final MenuItem menuItem = menu.findItem(R.id.action_cart);

        View actionView = menuItem.getActionView();

        actionView.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("WrongConstant")
            @Override
            public void onClick(View v) {
               //Action button BASKET

                try {
                    Intent intent = new Intent(v.getContext(), fragment_order.class);
                    startActivity(intent);
                } catch (Exception e) {
                    Toast.makeText(MainActivity.this, "Error:"+e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }

                onOptionsItemSelected(menuItem);
            }
        });

Toast显示错误:

unable to find explicit activity class{} have you declared this activity in your AndroidManifest.xm

我想在按下菜单按钮时转到fragments_order,这段代码写在MainActivity中。为什么 Intent 不起作用以及如何解决此错误?我的错误在哪里?

什么是 fragment_order.class?我打赌它不是 Activity,所以你遇到了 unable to find explicit activity class 异常。 Fragment != Activity

关于 HERE

Fragment 的一些文档

如果 fragment_orderActivity 那么:

  1. 更改此名称
  2. 正如您的 Toast 所说:在 AndroidManifest 中声明此 Activty

您最常使用 fragmentManager 来更改片段。 Intent 用于启动 Activity , Service 你可以像这样替换片段:

Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.f1_container, f1); // f1_container is your FrameLayout 
container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();  

你可以这样开始 activity:

Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);