0

TabHostを使用してアプリケーションインターフェイスを作成しようとしています。これが私が使用している例です: 私はそれを段階的にたどりましたが、私が扱うことができないメインクラスでエラーが発生します。これが私のコードです:

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
4

3 に答える 3

2

Javaでは、メソッドの外部にステートメントを書き込むことはできません。宣言できるのは変数のみです。

http://www.tutorialspoint.com/java/java_basic_syntax.htm

于 2012-12-30T20:42:11.640 に答える
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;
    }


 }
于 2012-12-30T20:56:21.520 に答える
1

で始まるすべてのコードを含める必要があります

TabHost tabHost = getTabHost();

メソッドに、で終わる}

于 2012-12-30T20:49:26.563 に答える