2012-12-30 80 views
0

我試圖使用TabHost創建應用程序界面。這裏是我正在使用的一個例子:example 我一步一步地跟着它,但是我在主類中出現了我無法處理的錯誤。這裏是我的代碼:Android TabHost - setIndicator方法

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

public class Main extends TabActivity { 

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

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

TabHost tabHost = getTabHost(); 

TabSpec settings = tabHost.newTabSpec("Settings"); 

settings.setIndicator("Settings", getResources().getDrawable(R.drawable.icon_settings_tab)); 
Intent settingsIntent = new Intent(this, Settings.class); 
settings.setContent(settingsIntent); 

TabSpec recorder = tabHost.newTabSpec("Recorder"); 
recorder.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_recorder_tab)); 
Intent recorderIntent = new Intent(this, Recorder.class); 
recorder.setContent(recorderIntent); 

TabSpec player = tabHost.newTabSpec("Player"); 
player.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_player_tab)); 
Intent playerIntent = new Intent(this, Player.class); 
player.setContent(playerIntent); 

tabHost.addTab(settings); 
tabHost.addTab(recorder); 
tabHost.addTab(player); 
} 

而下面一行:

settings.setIndicator("Settings", getResources().getDrawable(R.drawable.icon_settings_tab)); 

我得到這個錯誤:

Multiple markers at this line 
- Syntax error on token ")", delete this token 
- Syntax error, insert ";" to complete FieldDeclaration 
- Syntax error on token ".", { expected 
- Return type for the method is missing 
- Syntax error, insert "}" to complete ClassBody 
- The method getDrawable(int) is undefined for the type Main 
- Syntax error on token(s), misplaced construct(s) 
- Return type for the method is missing 

回答

1

結束把所有的代碼

TabHost tabHost = getTabHost(); 

TabSpec settings = tabHost.newTabSpec("Settings"); 

settings.setIndicator("Settings", getResources().getDrawable(R.drawable.icon_settings_tab)); 
Intent settingsIntent = new Intent(this, Settings.class); 
settings.setContent(settingsIntent); 

TabSpec recorder = tabHost.newTabSpec("Recorder"); 
recorder.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_recorder_tab)); 
Intent recorderIntent = new Intent(this, Recorder.class); 
recorder.setContent(recorderIntent); 

TabSpec player = tabHost.newTabSpec("Player"); 
player.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_player_tab)); 
Intent playerIntent = new Intent(this, Player.class); 
player.setContent(playerIntent); 

tabHost.addTab(settings); 
tabHost.addTab(recorder); 
tabHost.addTab(player); 

onCreate()內方法

結果你會得到這樣的:

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



    public class Main extends TabActivity { 

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

     TabHost tabHost = getTabHost(); 

    TabSpec settings = tabHost.newTabSpec("Settings"); 

    settings.setIndicator("Settings", getResources().getDrawable(R.drawable.icon_settings_tab)); 
    Intent settingsIntent = new Intent(this, Settings.class); 
    settings.setContent(settingsIntent); 

    TabSpec recorder = tabHost.newTabSpec("Recorder"); 
    recorder.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_recorder_tab)); 
    Intent recorderIntent = new Intent(this, Recorder.class); 
    recorder.setContent(recorderIntent); 

    TabSpec player = tabHost.newTabSpec("Player"); 
    player.setIndicator("Recorder", getResources().getDrawable(R.drawable.icon_player_tab)); 
    Intent playerIntent = new Intent(this, Player.class); 
    player.setContent(playerIntent); 

    tabHost.addTab(settings); 
    tabHost.addTab(recorder); 
    tabHost.addTab(player); 
    } 

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


} 
+0

謝謝你,我只是盲目的:) – Arxas

1

您需要包括所有的代碼開始在

TabHost tabHost = getTabHost(); 

入法,用}