java - 我的代码有什么问题?我想在Android中制作 'card-flip'动画

标签 java android xml runtime-error android-animation

这是我的主要 Activity :

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager()
                    .beginTransaction()
                    .add(R.id.container, new CardFrontFragment())
                    .commit();
        }

    }

    @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);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
     * A fragment representing the front of the card.
     */
    public class CardFrontFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.layout_front_card, container, false);
        }
    }

    /**
     * A fragment representing the back of the card.
     */
    public class CardBackFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.layout_back_card, container, false);
        }
    }

    private void flipCard() {
        if (mShowingBack) {
            getFragmentManager().popBackStack();
            return;
        }

        // Flip to the back.

        mShowingBack = true;

        // Create and commit a new fragment transaction that adds the fragment for the back of
        // the card, uses custom animations, and is part of the fragment manager's back stack.

        getFragmentManager()
                .beginTransaction()

                        // Replace the default fragment animations with animator resources representing
                        // rotations when switching to the back of the card, as well as animator
                        // resources representing rotations when flipping back to the front (e.g. when
                        // the system Back button is pressed).
                .setCustomAnimations(
                        R.animator.card_flip_right_in, R.animator.card_flip_right_out,
                        R.animator.card_flip_left_in, R.animator.card_flip_left_out)

                        // Replace any fragments currently in the container view with a fragment
                        // representing the next page (indicated by the just-incremented currentPage
                        // variable).
                .replace(R.id.container, new CardBackFragment())

                        // Add this transaction to the back stack, allowing users to press Back
                        // to get to the front of the card.
                .addToBackStack(null)

                        // Commit the transaction.
                .commit();
    }

}

这是我的主要 Activity XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

这是我的 layout_front_card.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#a6c"
    android:padding="16dp"
    android:gravity="bottom">

    <TextView android:id="@android:id/text1"
        style="?android:textAppearanceLarge"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="HI" />

    <TextView style="?android:textAppearanceSmall"
        android:textAllCaps="true"
        android:textColor="#80ffffff"
        android:textStyle="bold"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="How are u?" />

</LinearLayout>

这是我的 layout_back_card.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#a6c"
    android:padding="16dp"
    android:gravity="bottom">

    <TextView android:id="@android:id/text1"
        style="?android:textAppearanceLarge"
        android:textStyle="bold"
        android:textColor="#fff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="HI" />

    <TextView style="?android:textAppearanceSmall"
        android:textAllCaps="true"
        android:textColor="#80ffffff"
        android:textStyle="bold"
        android:lineSpacingMultiplier="1.2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="How are u?" />

</LinearLayout>

我无法运行我的项目!

错误:
Error:(24, 21) error: no suitable method found for add(int,Main.CardFrontFragment)
method FragmentTransaction.add(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.add(int,Fragment) is not applicable
(actual argument Main.CardFrontFragment cannot be converted to Fragment by method invocation conversion)
method FragmentTransaction.add(Fragment,String) is not applicable
(actual argument int cannot be converted to Fragment by method invocation conversion)

Error:(73, 13) error: cannot find symbol variable mShowingBack

Error:(80, 9) error: cannot find symbol variable mShowingBack

Error:(99, 17) error: no suitable method found for replace(int,Main.CardBackFragment)
method FragmentTransaction.replace(int,Fragment,String) is not applicable
(actual and formal argument lists differ in length)
method FragmentTransaction.replace(int,Fragment) is not applicable
(actual argument Main.CardBackFragment cannot be converted to Fragment by method invocation conversion)

Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

因为我是初学者,所以我很难在这里找出错误。

请帮助我!

任何帮助将受到的高度赞赏

提前致谢。

最佳答案

如果使用支持片段android.support.v4.app.Fragment;,则必须使用getSupportFragmentManager()

关于java - 我的代码有什么问题?我想在Android中制作 'card-flip'动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28863732/

相关文章:

java - 在apachecamel中将多个消息路由到同一个流

java - 键盘后面的选项卡 - Android

android - 如何将消息从 Android 发送到 Unity(使用 VR)? (UnityPlayer.UnitySendMessage 不起作用)

Android:SAX解析器进度监控

java - 如何使用java从Excel中获取单元格,该单元格是否包含字符串或数值?

java - 如何在 Java 中使用 ArrayList

java - 局部变量不会初始化 (Java)

android - 在图像android的中间绘制文本

java - 如何在部分刷新之前更新 Backing Bean

xml - 使用 xslt 2.0 生成包含目录中文件列表的文档