1

Mi aplicacion de marca un error pero no lo puedo encontrar,mi aplicacion contiene tabs y paginas web se supone que una pestaña me muestra una portada y la siguiente me muestra las paginas web usando un spinner

Esto me dice mi logCat:

04-25 15:12:01.973: E/AndroidRuntime(1136): FATAL EXCEPTION: main
04-25 15:12:01.973: E/AndroidRuntime(1136): Process: com.example.ecologia, PID: 1136
04-25 15:12:01.973: E/AndroidRuntime(1136): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ecologia/com.example.ecologia.MainActivity}: java.lang.NullPointerException
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.os.Looper.loop(Looper.java:136)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at java.lang.reflect.Method.invoke(Method.java:515)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at dalvik.system.NativeStart.main(Native Method)
04-25 15:12:01.973: E/AndroidRuntime(1136): Caused by: java.lang.NullPointerException
04-25 15:12:01.973: E/AndroidRuntime(1136):     at com.example.ecologia.MainActivity.onCreate(MainActivity.java:57)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.Activity.performCreate(Activity.java:5231)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-25 15:12:01.973: E/AndroidRuntime(1136):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-25 15:12:01.973: E/AndroidRuntime(1136):     ... 11 more 

Aqui dejo algo del codigo:

activity main

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.ecologia.MainActivity" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <LinearLayout
                    android:id="@+id/Inicio"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"

                     >

                    <ImageView
                        android:id="@+id/imageView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/portada" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Ecosistemas"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    >

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/Desarrollo"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"

                     >
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

Main Activity:

package com.example.ecologia;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemSelectedListener;    

public class MainActivity extends Activity implements OnItemSelectedListener {

    public TabHost tabs;
    public Spinner SpinTipo,SpinEco;
     public WebView webEcosistemas;   

     public ArrayAdapter<CharSequence>adaTipoEcosistema;

     public ArrayAdapter<CharSequence> adaTerrestre;

     public ArrayAdapter<CharSequence> adaAcuatico;

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

        SpinTipo = (Spinner)findViewById(R.id.SpinTipo);
        SpinEco = (Spinner)findViewById(R.id.SpinEco);
        webEcosistemas = (WebView)findViewById(R.id.webEcosistemas);


        adaTipoEcosistema= ArrayAdapter.createFromResource(this,
              R.array.Ecosistema,
              android.R.layout.simple_spinner_item);
        adaTerrestre = ArrayAdapter.createFromResource(this,
                  R.array.Terrestre,
                  android.R.layout.simple_spinner_item);
        adaAcuatico  =  ArrayAdapter.createFromResource(this,
                  R.array.Acuatico,
                  android.R.layout.simple_spinner_item);

  adaTipoEcosistema.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  adaTerrestre.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  adaAcuatico.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

  SpinTipo.setAdapter(adaTerrestre);
  SpinEco.setAdapter(adaAcuatico);

  SpinTipo.setOnItemSelectedListener(this);
  SpinEco.setOnItemSelectedListener(this);

  webEcosistemas.loadUrl("file:///android_assets/desierto.html");

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

  TabHost.TabSpec spec;


  spec = tabs.newTabSpec("Inicio");

  spec.setIndicator("",getResources().getDrawable(R.drawable.inicio));
  spec.setIndicator(getString(R.string.tabInicio));
  spec.setContent(R.id.Inicio);
  tabs.addTab(spec);

  spec = tabs.newTabSpec("Ecosistemas");

  spec .setIndicator("",getResources().getDrawable(R.drawable.ecosistema));
  spec.setIndicator(getString(R.string.tabEcosistema));
  spec .setContent(R.id.Ecosistemas);
  tabs.addTab(spec);

  spec = tabs.newTabSpec("Desarrollo Sustentable");

  spec .setIndicator("",getResources().getDrawable(R.drawable.desarrollo));
  spec.setIndicator(getString(R.string.tabDesarrollo));
  spec .setContent(R.id.Desarrollo);
  tabs.addTab(spec);

  tabs.setCurrentTab(0);
}



    @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(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_settings:
            finish();
            break;
        }
        return true;


    }

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3)  {
        // TODO Auto-generated method stub
        if (arg0.getId()==R.id.SpinTipo){
            switch(SpinTipo.getSelectedItemPosition()){
            case 0:
                   SpinTipo.setAdapter(adaTerrestre);
                   webEcosistemas.loadUrl("file:///android_asset/desierto.html");

                   break;
            case 1:
                   SpinTipo.setAdapter(adaAcuatico);
                   webEcosistemas.loadUrl("file:///android_asset/oceano.html");

                   break;
            }
          }


            if (arg0.getId()==R.id.SpinEco){
                switch(SpinTipo.getSelectedItemPosition()){
                case 0:
                    switch(SpinEco.getSelectedItemPosition()){
                    case 0:
                        webEcosistemas.loadUrl("file:///android_asset/tundra.html");

                        break;
                    case 1:
                        webEcosistemas.loadUrl("file:///android_asset/taiga.html");

                        break;
                    case 2:
                        webEcosistemas.loadUrl("file:///android_asset/bosque.html");

                        break;
                    case 3:
                        webEcosistemas.loadUrl("file:///android_asset/selva.html");

                        break;

                    case 4:
                        webEcosistemas.loadUrl("file:///android_asset/desierto.html");

                        break;

                    }
                    break;
                case 1:
                    switch (SpinEco.getSelectedItemPosition()){
                    case 0:
                        webEcosistemas.loadUrl("file:///android_asset/oceano.html");

                        break;
                    case 1:
                        webEcosistemas.loadUrl("file:///android_asset/rio.html");


                    }
                    break;
                }


            }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
}

contenido1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/TEcosistemas" />

            <Spinner
                android:id="@+id/SpinTipo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/Ecosistemas" />

            <Spinner
                android:id="@+id/SpinEco"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </TableRow>
    </TableLayout>

    <WebView
        android:id="@+id/webEcosistemas"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Rafael Bautista
  • 2,603
  • 6
  • 20
  • 35
Alucard
  • 31
  • 1

0 Answers0