Foro de elhacker.net

Programación => Java => Mensaje iniciado por: LikeVodka en 18 Abril 2015, 22:30 pm



Título: Ayuda llamar a una clase y sus métodos dentro de otra clase (Android)
Publicado por: LikeVodka en 18 Abril 2015, 22:30 pm
Pues eso de toda la vida

Ipprivada test = new Ipprivada();
test.mimetodo();

El problema es que simplemente parece no funcionar en android, aviso que soy nuevo en android no seáis muy duros conmigo, aquí dejo las dos clases

Código:
package com.example.tab2;

import android.support.v7.app.ActionBarActivity;
import android.widget.TabHost;
import android.widget.TextView;
import android.content.Intent;
import android.os.Bundle;


public class MainActivity extends ActionBarActivity {

TextView texto;

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

Ipprivada test = new Ipprivada();





TabHost tabs=(TabHost)findViewById(android.R.id.tabhost);
tabs.setup();


TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
test.Mostrar();
spec.setContent(R.id.tab1);
spec.setIndicator("TAB1");
   
tabs.addTab(spec);

spec=tabs.newTabSpec("mitab2");



spec.setContent(R.id.tab2);
spec.setIndicator("TAB2");
 
tabs.addTab(spec);

tabs.setCurrentTab(0);
}

}




Código:
package com.example.tab2;


import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.TextView;

public class Ipprivada extends ActionBarActivity {

TextView texto;


void Mostrar(){

texto = (TextView) findViewById(R.id.Ipprivada);
texto.setText("Hola");


}




}



El main xml:

Código:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
 
<TabHost android:id="@android:id/tabhost"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
 
     <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
 
         <TabWidget android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@android:id/tabs" />
 
         <FrameLayout android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:id="@android:id/tabcontent" >
 
            <LinearLayout android:id="@+id/tab1"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
 
                <TextView android:id="@+id/Ipprivada"
                      android:text="Contenido Tab 1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
        </LinearLayout>
 
            <LinearLayout android:id="@+id/tab2"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent" >
 
                <TextView android:id="@+id/Ippublica"
                      android:text="Contenido Tab 2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content" />
        </LinearLayout>
         </FrameLayout>
    </LinearLayout>
</TabHost>
</LinearLayout>



Igual es la cosa mas simple del mundo, pero lo he intentado todo, como veis esta todo en el mismo paquete, la verdad es que hay conceptos de programación de android que se me escapan bastante, nunca he trabajado con interfaces y son una locura, encima tampoco se si tengo que poner la clase en manifiesto.xml android pufff todo un lió la verdad, por cierto si creo un método con la función mostrar();dentro de la clase Main la aplicación funciona correctamente, intentando cargar la clase Ipprivada y luego llamando al método Mostrar la aplicación deja de funcionar.


Título: Re: Ayuda llamar a una clase y sus métodos dentro de otra clase (Android)
Publicado por: MNicolas en 19 Abril 2015, 11:22 am
Lo que quieres es pasar de una activity a otra. Usando la clase Intent es la solución.

Un saludo!