【问题标题】:Error in Fragment Transaction分片交易错误
【发布时间】:2017-06-01 07:26:58
【问题描述】:

我刚刚开始使用 android 片段和 git 在实现来自互联网的示例时卡住。在下面附加以下代码。获得一些帮助真的很高兴。 我在 ft.add(R.id.fragmentone,fragment) 行出现错误。 问候。

MainActivity.java

  import android.app.Fragment;
    import android.app.FragmentManager;
    import android.app.FragmentTransaction;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
        public void getfragment(View view)  {
            FragmentOne fragment = new FragmentOne();
            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft=fm.beginTransaction();
            ft.add(R.id.fragmentone,fragment);
        }
    }

FragmentOne.java

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fragment_one, container,false);
    }
}

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.vighnesh.fragmentsexp.MainActivity"
    android:orientation="vertical">
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/buttonfragment"
   android:layout_margin="20dp"
    android:text="Click to open fragment"/>
    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragmentone"
        android:layout_margin="20dp">
    </fragment>
</LinearLayout>

fragment_fragment_one.xml

<FrameLayout 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"
    tools:context="com.example.vighnesh.fragmentsexp.FragmentOne">
    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />
</FrameLayout>

【问题讨论】:

  • 什么问题???
  • 我认为您忘记从活动的 onCreate 方法调用 getfragment() 方法,您还需要通过 ft.commit() 提交片段事务;
  • 不是个好办法return inflater.inflate(R.layout.
  • 明白了import android.app.Fragment;import android.support.v4.app.Fragment;之间的冲突
  • 得到了解决方案。谢谢大家。

标签: android android-fragments fragmentmanager


【解决方案1】:

在您的活动中替换此代码。

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

}

public void getfragment() {
    FragmentOne fragment = new FragmentOne();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.fragmentone, fragment);
    ft.commit();
}

【讨论】:

    【解决方案2】:

    改变它。

     import android.app.Fragment;
        import android.app.FragmentManager;
        import android.app.FragmentTransaction;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        public class MainActivity extends AppCompatActivity {
    
           @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btnFragOne = (Button)findViewById(R.id.buttonfragment);
            btnFragOne.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    FragmentOne fragment = new FragmentOne();
                    FragmentManager fm=getFragmentManager();
                    FragmentTransaction ft=fm.beginTransaction();
                    ft.add(R.id.fragmentone,fragment); getfragment();
                }
            });
    
        }
        }
    

    【讨论】:

      【解决方案3】:

      所以使用getSupportFragmentManager() 而不是getFragmentManager()

      因为FragmentOne来自支持库(android.support.v4.app.Fragment)而不是来自框架(android.app.Fragment),所以您需要使用android.support.v4.app.FragmentManagerFragmentOne进行事务。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-14
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-12
        • 2019-09-06
        • 1970-01-01
        相关资源
        最近更新 更多