App se cierra automaticamente

  • Respuestas:0
  • AbiertoSin chinchetaSin solución
  • Posts del Foro: 1

26 ago. 2019 8:26:26 vía Web

Buenos dias,
Soy nueva en esto (aviso jaja), para mi tfm me atrevi a proponer el desarrollo de una app y estoy realizandolo a pasos muy pequeños y de manera totalmente autodidacta.
La estructura general de la app es la de varios fragments dominados por un menu lateral. Finalicé una pequeña parte del primer fragment y este fragment lo enlacé con la MainActivity del programa pero al ejecutarlo en el emulador se cierra la app.
Copio los codigos Java de MainActivity y del fragment.

public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab = 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();
        }
    });
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    navigationView.setNavigationItemSelectedListener(this);

    FragmentManager fragmentManager=getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.fragment_inmueble,new Inmueble()).commit();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
   /* switch (item.getItemId()){
        case R.id.nav_inmueble:
            class Inmueble;
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }*/
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    FragmentManager fragmentManager=getSupportFragmentManager();

    if (id == R.id.nav_inmueble) {
        // Handle the camera action
        fragmentManager.beginTransaction().replace(R.id.fragment_inmueble, new Inmueble()).commit();

    } else if (id == R.id.nav_edemandada) {
        //fragmentManager.beginTransaction().replace(R.id.contenedor, new Enecesaria()).commit();
    } else if (id == R.id.nav_eabsorbida) {
        //fragmentManager.beginTransaction().replace(R.id.contenedor, new Eabsorbida()).commit();
    } else if (id == R.id.nav_instalacion) {
        //fragmentManager.beginTransaction().replace(R.id.contenedor, new Economico()).commit();
    } else if (id == R.id.nav_economico) {
       // fragmentManager.beginTransaction().replace(R.id.contenedor, new Instalacion()).commit();
    } else if (id == R.id.nav_compartir) {

    } else if (id == R.id.nav_enviar) {

    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

}

*Y del primer fragment (fragment_inmueble se llama)*

/*

public class Inmueble extends Fragment {

private Spinner sprovincia;

public Inmueble() {
    // Required empty public constructor
}


@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment


    View view = inflater.inflate(R.layout.fragment_inmueble, container, false);
    sprovincia = (Spinner)getView().findViewById(R.id.spprovincia);
   /* incopn = (EditText) getView().findViewById(R.id.io);
    si = (RadioButton) getView().findViewById(R.id.afirmativo);
    no = (RadioButton) getView().findViewById(R.id.negativo);
    limpia = (RadioButton) getView().findViewById(R.id.part);
    urbana = (RadioButton) getView().findViewById(R.id.com);*/

    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),R.array.spinnerProv,android.R.layout.simple_spinner_item);
    sprovincia.setAdapter(adapter);

    sprovincia.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(parent.getContext(),"Seleccionado: "+parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG).show();

            int altitud;
            double H;
            double Tmedia;
            int Tred;
            double latitud;


            switch (parent.getItemAtPosition(position).toString()){
                case "Albacete":
                    altitud=677; H=4.98; Tmedia=15.4; Tred=13; latitud=39;
                case "Alicante":
                    altitud=7; H=5.05; Tmedia=20.1; Tred=15; latitud=38.2;
                case "Almería":
                    altitud=0; H=5.29; Tmedia=20.5; Tred=16; latitud=36.25;
                case "Ávila":
                    altitud=1054; H=4.63; Tmedia=12.3; Tred=10; latitud=40.39;
                case "Badajoz":
                    altitud=168; H=5.02; Tmedia=18.9; Tred=14; latitud=38.35;
                case "Barcelona":
                    altitud=1; H=4.56; Tmedia=18.5; Tred=14; latitud=41.23;
                case "Bilbao":
                    altitud=214; H=3.54; Tmedia=14.3; Tred=13; latitud=43.15;
                case "Burgos":
                    altitud=861; H=4.31; Tmedia=12.5; Tred=10; latitud=43.15;
                case "Cáceres":
                    altitud=385; H=4.99; Tmedia=18.3; Tred=13; latitud=39.28;
                case "Cádiz":
                    altitud=0; H=5.28; Tmedia=20.3; Tred=16; latitud=36.32;
                case "Castellón":
                    altitud=18; H=4.76; Tmedia=19.2; Tred=15; latitud=39.59;
                case "Ceuta":
                    altitud=0; H=4.91; Tmedia=19.6; Tred=16; latitud=35.9;
                case "Ciudad Real":
                    altitud=630; H=5.03; Tmedia=16.3; Tred=16; latitud=38.59;
                case "Córdoba":
                    altitud=113; H=5.12; Tmedia=20; Tred=15; latitud=37.53;
                case "Coruña":
                    altitud=0; H=3.86; Tmedia=15.9; Tred=13; latitud=33.22;
                case "Cuenca":
                    altitud=975; H=4.73; Tmedia=13.6; Tred=12; latitud=40.04;
                case "Girona":
                    altitud=143; H=4.36; Tmedia=17; Tred=13; latitud=41.59;
                case "Granada":
                    altitud=754; H=5.2; Tmedia=17.3; Tred=13; latitud=37.11;
                case "Guadalajara":
                    altitud=708; H=4.82; Tmedia=15.8; Tred=12; latitud=40.38;
                case "Huelva":
                    altitud=50; H=5.22; Tmedia=19.9; Tred=16; latitud=37.16;
                case "Huesca":
                    altitud=432; H=4.76; Tmedia=15.6; Tred=12; latitud=42.08;
                case "Jaén":
                    altitud=436; H=5.18; Tmedia=19; Tred=15; latitud=37.46;
                case "León":
                    altitud=346; H=4.49; Tmedia=13.3; Tred=13; latitud=42.36;
                case "Lérida":
                    altitud=131; H=4.79; Tmedia=17.1; Tred=13; latitud=41.37;
                case "Logroño":
                    altitud=379; H=4.22; Tmedia=15.3; Tred=12; latitud=42.28;
                case "Lugo":
                    altitud=412; H=3.83; Tmedia=14; Tred=11; latitud=43.04;
                case "Madrid":
                    altitud=589; H=4.82; Tmedia=15.6; Tred=13; latitud=40.24;
                case "Málaga":
                    altitud=0; H=5.2; Tmedia=20.7; Tred=16; latitud=36.43;
                case "Melilla":
                    altitud=130; H=5.09; Tmedia=20.6; Tred=16; latitud=35.29;
                case "Murcia":
                    altitud=25; H=5.13; Tmedia=19.3; Tred=15; latitud=37.59;
                case "Ourense":
                    altitud=327; H=4.11; Tmedia=15.8; Tred=13; latitud=42.2;
                case "Oviedo":
                    altitud=214; H=3.57; Tmedia=14.3; Tred=12; latitud=43.22;
                case "Palencia":
                    altitud=722; H=4.61; Tmedia=13.8; Tred=11; latitud=42;
                case "Palma de Mallorca":
                    altitud=1; H=4.77; Tmedia=18.8; Tred=15; latitud=39.35;
                case "Palmas de Gran Canaria":
                    altitud=114; H=5.06; Tmedia=22.9; Tred=17; latitud=28.06;
                case "Pamplona":
                    altitud=456; H=4.04; Tmedia=14.3; Tred=12; latitud=42.49;
                case "Pontevedra":
                    altitud=77; H=4.08; Tmedia=16.6; Tred=13; latitud=42.26;
                case "Salamanca":
                    altitud=770; H=4.72; Tmedia=14; Tred=11; latitud=40.57;
                case "San Sebastián":
                    altitud=5; H=3.55; Tmedia=15.3; Tred=12; latitud=43.19;
                case "SC de Tenerife":
                    altitud=0; H=5.4; Tmedia=22.8; Tred=17; latitud=28.28;
                case "Santander":
                    altitud=1; H=3.66; Tmedia=15.8; Tred=12; latitud=43.28;
                case "Segovia":
                    altitud=1013; H=4.55; Tmedia=13.5; Tred=11; latitud=40.57;
                case "Sevilla":
                    altitud=9; H=5.23; Tmedia=19.3; Tred=16; latitud=37.23;
                case "Soria":
                    altitud=984; H=4.48; Tmedia=12.6; Tred=10; latitud=41.46;
                case "Tarragona":
                    altitud=1; H=4.65; Tmedia=17.9; Tred=15; latitud=41.07;
                case "Teruel":
                    altitud=995; H=4.73; Tmedia=13.6; Tred=11; latitud=40.2;
                case "Toledo":
                    altitud=445; H=5; Tmedia=16.9; Tred=14; latitud=39.51;
                case "Valencia":
                    altitud=8; H=4.92; Tmedia=18.8; Tred=15; latitud=39.28;
                case "Valladolid":
                    altitud=704; H=4.66; Tmedia=13.3; Tred=12; latitud=41.39;
                case "Vitoria":
                    altitud=512; H=3.8; Tmedia=15.8; Tred=11; latitud=42.51;
                case "Zamora":
                    altitud=677; H=4.71; Tmedia=14.3; Tred=12; latitud=41.3;
                case "Zaragoza":
                    altitud=204; H=4.78; Tmedia=16.8; Tred=13; latitud=41.39;


            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }


    });
    return view;
}

}

Espero que alguien me pueda ayudar. Gracias de antemano

Contesta el/la primero/a
Recomendado por NextPit