使用HostTab的Android java.lang.NullPointerException

时间:2012-08-17 20:12:32

标签: android nullpointerexception

我正在使用Android 2.1HostTab活动,但是当我编译时,我的应用程序崩溃了 错误"java.lang.NullPointerException"。我不确定,但我猜HostTabAndroid 2.1问题需要帮助,有人可以查看我的代码吗? TY

我的日志是

    08-17 19:54:08.025: D/ddm-heap(215): Got feature list request
    08-17 19:54:08.035: D/AndroidRuntime(215): Shutting down VM
    08-17 19:54:08.035: W/dalvikvm(215): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    08-17 19:54:08.035: E/AndroidRuntime(215): Uncaught handler: thread main exiting due to uncaught exception
    08-17 19:54:08.035: E/AndroidRuntime(215): java.lang.RuntimeException: Unable to start activity ComponentInfo{utec.academico/utec.academico.Academico}: java.lang.NullPointerException
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.os.Handler.dispatchMessage(Handler.java:99)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.os.Looper.loop(Looper.java:123)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at java.lang.reflect.Method.invoke(Method.java:521)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at dalvik.system.NativeStart.main(Native Method)
    08-17 19:54:08.035: E/AndroidRuntime(215): Caused by: java.lang.NullPointerException
    08-17 19:54:08.035: E/AndroidRuntime(215):  at utec.academico.Academico.onCreate(Academico.java:28)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    08-17 19:54:08.035: E/AndroidRuntime(215):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    08-17 19:54:08.035: E/AndroidRuntime(215):  ... 11 more
    08-17 19:54:08.055: I/dalvikvm(215): threadid=7: reacting to signal 3
    08-17 19:54:08.055: E/dalvikvm(215): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
    08-17 19:54:09.306: I/Process(215): Sending signal. PID: 215 SIG: 9

我的清单是:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="utec.academico"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="7"
    android:targetSdkVersion="15" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Academico">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Login"></activity>
    <activity android:name=".Horario"></activity>
    <activity android:name=".Asistencia"></activity>
    <activity android:name=".Notas"></activity>
    <activity android:name=".DetalleNotas"></activity>
    <activity android:name=".Record"></activity>
</application>

我的主要Hosttab类

public class Academico extends TabActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.academico);

    TabHost tabHost=getTabHost(); //Creamos el tabhost de la actividad
    tabHost.setup();      

    TabHost.TabSpec spec;         //Creamos el recurso para la propiedad de la pestaña
    Intent intent;                //intent para abrir la pestaña 
    Resources res= getResources();//Obtenemos los recursos

    //Establecemos el recurso de color para todos los tabs
    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.BLACK);

    //Se crea el  intent para abrir la actividad, configuramos y cargamos el tab en el contenedor tabhost
    intent = new Intent().setClass(this, Horario.class);
    spec = tabHost.newTabSpec("Horario").setContent(intent).setIndicator("Horario",res.getDrawable(R.drawable.horario));  
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Notas.class);
    spec = tabHost.newTabSpec("Notas").setContent(intent).setIndicator("  Notas  ");  
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Asistencia.class);
    spec = tabHost.newTabSpec("Asistencia").setContent(intent).setIndicator("Asistencia",res.getDrawable(R.drawable.asistencia));  
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Record.class);
    spec = tabHost.newTabSpec("Record").setContent(intent).setIndicator("  Record Académico  ");  
    tabHost.addTab(spec);        
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.academico, menu);
    return true;
}
}

然后我的HostTab xml是

<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <FrameLayout 
           android:id="@android:id/tabcontent"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent"
           android:padding="5dp"
           android:layout_weight="1">
    </FrameLayout>   
    <TabWidget
           android:id="@android:id/tabs"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_weight="0"
           android:layout_marginBottom="-4dp">
    </TabWidget>
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

我猜问题出在

 tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.BLACK);

将其移至Oncreate

的末尾