我正在尝试使用TabHost创建应用程序界面。以下是我正在使用的示例:example 我一步一步地跟着它,但是我在Main类中遇到了我无法处理的错误。这是我的代码:
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
答案 0 :(得分:2)
在Java中,您不能在方法之外编写任何语句。您只能声明变量。
答案 1 :(得分:1)
您需要包含从
开始的所有代码TabHost tabHost = getTabHost();
进入方法,以}
答案 2 :(得分: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;
}
}