2017-05-27 5 views
0

Quelqu'un peut-il être en mesure de résoudre, pourquoi ce code simple ne fonctionne pas dans la version supérieure d'Android? Le MainActivity.java ressemble à ceci.TabHost ne fonctionne pas avec des versions Android plus élevées

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TabHost; 

public class MainActivity extends AppCompatActivity { 
    TabHost tabHost; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     TabHost host = (TabHost)findViewById(R.id.tabHost); 
     host.setup(); 

     //Tab 1 
     TabHost.TabSpec spec = host.newTabSpec("Tab One"); 
     spec.setContent(R.id.tab1); 
     spec.setIndicator("Tab One"); 
     host.addTab(spec); 

     //Tab 2 
     spec = host.newTabSpec("Tab Two"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("Tab Two"); 
     host.addTab(spec); 

     //Tab 3 
     spec = host.newTabSpec("Tab Three"); 
     spec.setContent(R.id.tab3); 
     spec.setIndicator("Tab Three"); 
     host.addTab(spec); 
    } 

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

    @Override 
    public boolean onOptionsItemSelected(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 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

Et le fichier activity_main.xml est comme ci-dessous.

<LinearLayout 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" 
    tools:context=".MainActivity"> 

    <TabHost 
     android:id="@+id/tabHost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="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/tab1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#ffc916" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="This is tab 1" /> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#da8200" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="This is tab 2" /> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#5b89ff" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="This is tab 3" /> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 

Cependant, l'application finale plante au démarrage. Vraiment, je me doute moins de trouver l'erreur. Toute aide est appréciée.

+0

Si elle se bloque, s'il vous plaît ajouter logcat. – Zoe

Répondre

0

Votre code simple fonctionne sans problème ici est votre code édité

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 


public class TabTest extends Activity { 

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

     TabHost host = (TabHost)findViewById(R.id.tabHost); 
     host.setup(); 

     //Tab 1 
     TabHost.TabSpec spec = host.newTabSpec("Tab One"); 
     spec.setContent(R.id.tab1); 
     spec.setIndicator("Tab One"); 
     host.addTab(spec); 

     //Tab 2 
     spec = host.newTabSpec("Tab Two"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("Tab Two"); 
     host.addTab(spec); 

     //Tab 3 
     spec = host.newTabSpec("Tab Three"); 
     spec.setContent(R.id.tab3); 
     spec.setIndicator("Tab Three"); 
     host.addTab(spec); 
    } 

} 
+0

Merci pour la solution. Donc, le problème était seulement avec AppCompatActivity ?? Pouvez-vous s'il vous plaît voir mon autre requête affichée ici https://stackoverflow.com/questions/44200150/dynamic-tabhost-crashing-in-higher-version-of-android-but-working-fine-with-2-3? noredirect = 1 # comment75425576_44200150 – Sanu