-3

I have this problem:

I'm trying to create an App with 2 buttons on a Layout and this buttons open 2 different Fragments, but the problem is that when I open the app it crashes.

Here's my code:

MainActivity:

package com.example.akroma.feina_final;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button boton1,boton2;

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

        boton1 = (Button) findViewById(R.id.button2);
        boton1.setOnClickListener(this);
        boton2 = (Button) findViewById(R.id.button);
        boton2.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        Fragment fragment;

        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

        switch (v.getId()) {
            case R.id.button:

                fragment = new FragmentOne();
                ft.replace(R.id.fragment_container, fragment);
                ft.commit();
                break;

            case R.id.button2:
                fragment = new FragmentTwo();
                ft.replace(R.id.fragment_container, fragment);
                ft.commit();
                break;
        }
    }

}

I created the fragments standard (Blank).

Name of fragments:

FragmentOne FragmentTwo

    <?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="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    tools:context="com.example.akroma.feina_final.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Mapa" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Negocis" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

</LinearLayout>

Name of fragments on the tree:

FragmentOne

FragmentTwo

Edit: error: Error inflating class fragment

Solution to the inflating:

Thanks to: @svkaka

Change the fragment for:

    <View
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

New error:

must implement OnFragmentInteractionListener

Any ideas? I don't know what I'm doing bad.

Thanks in advance

Zub
  • 808
  • 3
  • 12
  • 23
akroma
  • 47
  • 1
  • 1
  • 6
  • 1
    What's the crash? Post your stack trace. Can you also post your xml layout file? – Ricardo Dec 08 '17 at 20:23
  • Here is the xml – akroma Dec 08 '17 at 20:24
  • 1
    Possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – Code-Apprentice Dec 08 '17 at 20:29
  • 1
    Please read the above link for tips on how to discover the cause of the crash. – Code-Apprentice Dec 08 '17 at 20:30
  • Your tag needs to specify a fragment class. But I believe what you want is to replace it with a `FrameLayout` or some other view group since you are setting the fragments in your `onClick()` event. – Ricardo Dec 08 '17 at 20:32
  • What i need exactly its that when i open the app it doesn't appear a fragment, but when i click a button it appears. – akroma Dec 08 '17 at 20:34
  • How can i inflate the fragment for don't get this error? – akroma Dec 08 '17 at 20:40
  • The problem its solved but now i need to solve the OnFragmentInteractionListener, how can i do it? – akroma Dec 08 '17 at 21:16

1 Answers1

0

Replace this

<fragment
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

with this

<View
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

U are mixing dynamic and static fragments

svkaka
  • 3,942
  • 2
  • 31
  • 55