片段事务中的错误

时间:2017-06-01 07:26:59

标签: android android-fragments fragmentmanager

我刚开始使用android片段和git卡住了,同时从internet实现了一个例子。下面的代码如下所示。我真的很想得到一些帮助。 我在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>

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

改变它。

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

    }
    }

答案 2 :(得分:0)

因此请使用factories.stream().filter(f -> f.getCars().stream().filter(c -> C.getName().equals("Mazda3")).).collect(Collectors.toList()); 代替getSupportFragmentManager()

因为getFragmentManager()来自支持库(FragmentOne)而非来自框架(android.support.v4.app.Fragment),所以您需要使用android.app.Fragment与{{1}进行交易}}。