2015-09-07 4 views
-1

PS. Я новичок в программировании на Android и Java.устарел TabActivity в android нет ошибок, но приложение, к сожалению, остановилось при нажатии кнопки

У меня эта проблема в моем приложении, она падает, когда я нажимал кнопку моего намерения, которая обрабатывает активность вкладок (устаревшая), ошибок нет с некоторыми предупреждениями, но я не могу понять, что я делаю неправильно.

вот код.

InformationActivity.class

import android.app.TabActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabHost.TabSpec; 

public class InformationActivity extends TabActivity 
{ 
     /** Called when the activity is first created. */ 

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

       // create the TabHost that will contain the Tabs 
       TabHost tabHost = (TabHost)findViewById(R.id.tabhost); 


       TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
       TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 


      // Set the Tab name and Activity 
      // that will be opened when particular Tab will be selected 
       tab1.setIndicator("Tab1"); 
       tab1.setContent(new Intent(this,OfflineTab.class)); 

       tab2.setIndicator("Tab2"); 
       tab2.setContent(new Intent(this,OnlineTab.class)); 

       /** Add the tabs to the TabHost to display. */ 
       tabHost.addTab(tab1); 
       tabHost.addTab(tab2); 


     } 
} 

OfflineTab.class

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.TextView; 

public class OfflineTab extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     TextView tv=new TextView(this); 
     tv.setTextSize(25); 
     tv.setGravity(Gravity.CENTER_VERTICAL); 
     tv.setText("Contains R.E.D. e - Kit Offline module"); 

     setContentView(tv); 
    } 
} 

OnlineTab.class

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.TextView; 

public class OnlineTab extends Activity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     TextView tv=new TextView(this); 
     tv.setTextSize(25); 
     tv.setGravity(Gravity.CENTER_VERTICAL); 
     tv.setText("This Is Tab2 Activity"); 

     setContentView(tv); 
    } 
} 

activity_information.xml

<?xml version="1.0" encoding="utf-8"?> 

<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
     android:id="@+id/tabhost"> 

    <LinearLayout 
      android:id="@+id/LinearLayout01" 
      android:orientation="vertical" 
      android:layout_height="fill_parent" 
      android:layout_width="fill_parent"> 

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

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

    </LinearLayout> 

</TabHost> 

ответ

1

Журнал сказал: java.lang.RuntimeException: Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'

Так просто не определить новый идентификатор для tabhost:

activity_information.xml

<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 

android:id="@android:id/tabhost" 

android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

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

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

</TabHost> 

onCraete() из InformationActivity

@Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_information); 
      // create the TabHost that will contain the Tabs 

      TabHost tabHost = getTabHost(); // <- get tabhost by this 


      TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
      TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 


     // Set the Tab name and Activity 
     // that will be opened when particular Tab will be selected 
      tab1.setIndicator("Tab1"); 
      tab1.setContent(new Intent(this,OfflineTab.class)); 

      tab2.setIndicator("Tab2"); 
      tab2.setContent(new Intent(this,OnlineTab.class)); 

      /** Add the tabs to the TabHost to display. */ 
      tabHost.addTab(tab1); 
      tabHost.addTab(tab2); 

} 

И это сработало! enter image description here

+0

Thank You Very Much! он работал и на меня. –

+0

о нет! проблема возникает снова после нескольких попыток :(Что я сделал не так? –

+0

Я пробовал снова и снова. В моем проекте не было никаких проблем. Не могли бы вы показать нам журнал, когда вы раздавите? –

 Смежные вопросы

  • Нет связанных вопросов^_^