Programación y Desarrollo para Android Subforo exclusivo para temas de programación de software para PDAs y desarrollo de aplicaciones, interfaces, etc bajo Android


 
Herramientas
  #1  
Viejo 08/01/16, 13:00:45
Avatar de manolazo
manolazo manolazo no está en línea
Miembro del foro
Mensajes: 219
 
Fecha de registro: jun 2012
Localización: Madrid
Mensajes: 219
Modelo de smartphone: Samsung Galaxy S7 edge
Tu operador: Pepephone
Mencionado: 1 comentarios
Tagged: 0 hilos
Pasar datos desde Activity a Fragment

Buenas.

Hace bastante que no entro en el foro y he retomado esto de Android despues de un tiempo y acudo a vosotros a ver si me echais una mano para ver donde esta el error.

Quiero pasar dato desde una Activity a un Fragment pero no me deja.

Error en esta linea : transaction.add(R.id.fragment_container, fragment1).commit(); -> no puede resolver el metodo add.
DOnde esta el error? a la hora de instanciar Fragment1 ?
hay algo que no cuadra.

MainActivity:

Código:
public class MainActivity extends FragmentActivity {

    @override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);       
       	
	//Traigo el fragment 
        Fragment1 fragment1= new Fragment1();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, fragment1).commit();

	Bundle data = new Bundle();
        data.putString("dato", "lo que sea");
	fragment1.setArguments(data);
   }
xml donde agregamos al fragment en el FrameLayout:

Código:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearlayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ccc"
        android:layout_weight="1"
        android:orientation="vertical">
        <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#eee"
        android:orientation="vertical">
        <fragment android:name="com.example.chukk.fragmentpassingdata.Fragment2"
            android:id="@+id/frag_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>
Fragment1:

Código:
public class Fragment1 extends Fragment {

    @override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

        String data =getArguments().getString("dato");
        Toast.makeText(getActivity(),data, Toast.LENGTH_LONG).show();

        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment1, null);      
        return root;
    }
fragment1 xml :

Código:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First Layout" />
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pass data" />
</LinearLayout>
Responder Con Cita


  #2  
Viejo 10/01/16, 21:15:14
Avatar de manolazo
manolazo manolazo no está en línea
Miembro del foro
Mensajes: 219
 
Fecha de registro: jun 2012
Localización: Madrid
Mensajes: 219
Modelo de smartphone: Samsung Galaxy S7 edge
Tu operador: Pepephone
Mencionado: 1 comentarios
Tagged: 0 hilos
sorry.

Se me olvido incluir la librería suppor v4 en las dependencias.
solucionado
Responder Con Cita
Respuesta

Estás aquí
Regresar   HTCMania > Todo sobre Android > Programación y Desarrollo para Android

Herramientas

Reglas de Mensajes
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Las caritas están On
Código [IMG] está On
Código HTML está Off

Saltar a Foro



Hora actual: 12:20:36 (GMT +1)

Cookies settings
Powered by vBulletin™
Copyright © vBulletin Solutions, Inc. All rights reserved.
 
HTCMania: líderes desde el 2007