web-dev-qa-db-fra.com

Comment mettre FloatingActionButton to Fragment?

Comment puis-je mettre ma fab sur mon fragment? aide s'il vous plaît je reçois des erreurs telles que.

Android.view.InflateException: ligne de fichier XML binaire n ° 13: erreur lors de l'insufflation du fragment de classe

dans mon fragment acticity. Eh bien voici mes codes.

bien le problème est dans le getActivity().setContentView(R.layout.activity_ownerhome);

#ownerhome.Java

public class ownerhome extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ownerhome);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    }

}

ownerhomeFragment.Java

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v =  inflater.inflate(R.layout.fragment_owner, container, false);


    ImageLoader.getInstance().init(UILConfig.config(getActivity()));
    lvCars = (ListView) v.findViewById(R.id.lvOnwer);

    pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
    Log.d(TAG, pref.getString("username", ""));
    Log.d(TAG, pref.getString("password", ""));


    PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);
    tvUser = (TextView) v.findViewById(R.id.tvUser);
    tvUser.setText("Hello! "+pref.getString("username", ""));

    taskRead.execute("http://carkila.esy.es/user.php?owner="+pref.getString("username", ""));

    FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent in = new Intent(getActivity(), InsertActivity.class);
            startActivity(in);
        }
    });
    registerForContextMenu(lvCars);
    return v;
}

content_ownerhome.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="@layout/activity_ownerhome">

<fragment
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:name="com.example.kun.carkila.ownerFragment"
Android:id="@+id/ownerhomeFragment"/>

</RelativeLayout>

activity_ownerhome.Java

<?xml version="1.0" encoding="utf-8"?>    <Android.support.design.widget.CoordinatorLayout 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"
Android:fitsSystemWindows="true"
tools:context="com.example.kun.carkila.ownerhome">

<Android.support.design.widget.AppBarLayout
    Android:layout_width="match_parent"
    Android:layout_height="wrap_content"
    Android:theme="@style/AppTheme.AppBarOverlay">

    <Android.support.v7.widget.Toolbar
        Android:id="@+id/toolbar"
        Android:layout_width="match_parent"
        Android:layout_height="?attr/actionBarSize"
        Android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</Android.support.design.widget.AppBarLayout>

<include layout="@layout/content_ownerhome" />

<Android.support.design.widget.FloatingActionButton
    Android:id="@+id/fab"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_gravity="bottom|end"
    Android:layout_margin="@dimen/fab_margin"
    Android:src="@drawable/plus" />


</Android.support.design.widget.CoordinatorLayout>

fragmentOwnerHome.Java

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="@layout/activity_ownerhome">


<ListView
    Android:layout_width="match_parent"
    Android:layout_height="450sp"
    Android:id="@+id/lvOnwer"
    Android:layout_alignParentLeft="true"
    Android:layout_alignParentStart="true"
    Android:layout_alignParentBottom="true" />

<TextView
    Android:text="TextView"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_alignParentTop="true"
    Android:layout_alignParentLeft="true"
    Android:layout_alignParentStart="true"
    Android:layout_marginTop="24dp"
    Android:textSize="30sp"
    Android:textColor="@color/colorPrimaryDark"
    Android:id="@+id/tvUser" />

</RelativeLayout>

TRACE DE LA PILE

FATAL EXCEPTION: main
Process: com.example.kun.carkila, PID: 9267
Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kun.carkila/com.example.kun.carkila.ownerhome}: Android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2195)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2245)
at Android.app.ActivityThread.access$800(ActivityThread.Java:135)
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1196)
at Android.os.Handler.dispatchMessage(Handler.Java:102)
at Android.os.Looper.loop(Looper.Java:136)
at Android.app.ActivityThread.main(ActivityThread.Java:5021)
at Java.lang.reflect.Method.invokeNative(Native Method)
at Java.lang.reflect.Method.invoke(Method.Java:515)
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:827)
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: Android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:713)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:755)
at Android.view.LayoutInflater.parseInclude(LayoutInflater.Java:839)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:745)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:492)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:397)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:353)
at Android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.Java:280)
at Android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.Java:140)
at com.example.kun.carkila.ownerFragment.onCreateView(ownerFragment.Java:127)
at Android.support.v4.app.Fragment.performCreateView(Fragment.Java:1974)
at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1036)
at Android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.Java:1230)
at Android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.Java:1332)
at Android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.Java:2288)
at Android.support.v4.app.FragmentController.onCreateView(FragmentController.Java:120)
at Android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.Java:356)
at Android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.Java:31)
at Android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.Java:79)
at Android.view.LayoutInflater.createViewFromTag(LayoutInflater.Java:689)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:755)
at Android.view.LayoutInflater.parseInclude(LayoutInflater.Java:839)
at Android.view.LayoutInflater.rInflate(LayoutInflater.Java:745)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:492)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:397)
at Android.view.LayoutInflater.inflate(LayoutInflater.Java:353)
at Android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.Java:280)
at Android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.Java:140)
at com.example.kun.carkila.ownerhome.onCreate(ownerhome.Java:12)
at Android.app.Activity.performCreate(Activity.Java:5231)
at Android.app.Instrumentation.callActivityOnCreate(Instrumentation.Java:1090)
at Android.app.ActivityThread.performLaunchActivity(ActivityThread.Java:2159)
at Android.app.ActivityThread.handleLaunchActivity(ActivityThread.Java:2245) 
at Android.app.ActivityThread.access$800(ActivityThread.Java:135) 
at Android.app.ActivityThread$H.handleMessage(ActivityThread.Java:1196) 
at Android.os.Handler.dispatchMessage(Handler.Java:102) 
at Android.os.Looper.loop(Looper.Java:136) 
at Android.app.ActivityThread.main(ActivityThread.Java:5021) 
at Java.lang.reflect.Method.invokeNative(Native Method) 
at Java.lang.reflect.Method.invoke(Method.Java:515) 
at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:827) 
at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:643) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: Java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f0e00aa, tag null, or parent id 0xffffffff with another fragment for com.example.kun.carkila.ownerFragment
at Android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.Java:2293)
at Android.support.v4.app.FragmentController.onCreateView(FragmentController.Java:120)
at Android.support.v4.app.FragmentActivity
4
J. Shmoe

Vous avez ajouté le bouton d'action flottant dans votre activity_ownerhome.xml et vous l'initialisez dans ownerhomeFragment.Java.

Alors ajoutez votre FloatingActionButton dans fragmentownerhhome.xml lui-même comme ceci

<RelativeLayout 
  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"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"
  tools:context="com.example.kun.carkila.ownerhome"
  tools:showIn="@layout/activity_ownerhome">

  <ListView
    Android:layout_width="match_parent"
    Android:layout_height="450sp"
    Android:id="@+id/lvOnwer"
    Android:layout_alignParentLeft="true"
    Android:layout_alignParentStart="true"
    Android:layout_alignParentBottom="true" />

  <TextView
    Android:text="TextView"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_alignParentTop="true"
    Android:layout_alignParentLeft="true"
    Android:layout_alignParentStart="true"
    Android:layout_marginTop="24dp"
    Android:textSize="30sp"
    Android:textColor="@color/colorPrimaryDark"
    Android:id="@+id/tvUser" />

  <Android.support.design.widget.FloatingActionButton
    Android:id="@+id/fab"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_gravity="bottom|end"
    Android:layout_margin="@dimen/fab_margin"
    Android:src="@drawable/plus" />
</RelativeLayout>

et supprimez le FloatingActionButton de votre fichier activity_ownerhome.xml.

5
Hasan shaikh

Vous devez implémenter le fragment dans votre classe principale, par exemple ...

public class ownerhome extends AppCompatActivity implement ownerhomeFragment {...
0
jose luis Ribero

Le problème ne vient pas du FAB, mais de la façon dont vous gonflez votre vue.
Remplacez ce code dans le fragment:

View v = super.onCreateView(inflater, container, savedInstanceState);
getActivity().setContentView(R.layout.activity_ownerhome);

Avec ça:

var v = inflater.inflate(R.layout.{your fragment xml}, container, false);

Et retirez ceci:

 <fragment
Android:layout_width="wrap_content"
Android:layout_height="wrap_content"
Android:name="com.example.kun.carkila.ownerFragment"
Android:id="@+id/ownerhomeFragment"/>

Vous devez définir l'apparence de votre fragment xml, puis le gonfler à l'aide du code inflater.inflate... et y insérer votre fragment xml, comme je l'ai écrit ci-dessus.

Mettre à jour:
Au lieu de <include layout="@layout/content_ownerhome" />, placez-le dans ceci:

<FrameLayout
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:id="@+id/content_frame" />

Ensuite, dans votre activité principale, écrivez ce code:

 getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();

Et vous êtes censé voir votre fragment si, dans le fragment, vous avez correctement gonflé votre fragment xml, comme indiqué ci-dessus.

0
amitairos

La cause de cet incident est due au fait que votre code XML FAB se trouve dans le "Modèle d'activité", il est supposé se trouver dans le fichier "Modèle de fragment".

Voici comment j'utilise FAB dans un fragment

Dans mon fragment fichier de mise en page:

    <Android.support.design.widget.FloatingActionButton
    Android:id="@+id/fab"
    Android:layout_width="wrap_content"
    Android:layout_height="wrap_content"
    Android:layout_gravity="bottom|end"
    Android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@Android:drawable/ic_dialog_email" />

Dans mon fichier de classe de fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    return view;
}

Et il fonctionne...

NOTE: Quand j'ai inclus Android:backgroundTint="@Android:color/transparent" , l'application s'est effondrée et a donné l'erreur:

Binary XML file line #14: Error inflating class Android.support.design.widget.FloatingActionButton

J'espère que cela t'aides.

0
Bukunmi