使用按钮通过Android SDK切换视图

时间:2010-11-15 18:36:42

标签: java android eclipse

我在Android应用中按下按钮切换视图时遇到问题。代码在Eclipse中没有显示错误,但是当单击按钮时,应用程序在模拟器中意外退出。我的代码如下。感谢

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button go = (Button)findViewById(R.id.goButton);
        go.setOnClickListener(mGoListener);     
    }

    private OnClickListener mGoListener = new OnClickListener() {
    public void onClick(View v) {           
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setClassName("android.taboo.Activities", "android.taboo.Activities.MainMenu");
            startActivity(intent);
        }
    };
}

public class MainMenu extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);

        TextView quickStart = (TextView)findViewById(R.id.quickStart);
        quickStart.setOnClickListener(mQuickStartListener);

        TextView gameSetup = (TextView)findViewById(R.id.gameSetup);
        gameSetup.setOnClickListener(mGameSetupListener);

        TextView settings = (TextView)findViewById(R.id.settings);
        settings.setOnClickListener(mSettingsListener);

        TextView wordEntry = (TextView)findViewById(R.id.wordEntry);
        wordEntry.setOnClickListener(mWordEntryListener);
    }

    //Listeners for MainMenu navigation buttons
    private OnClickListener mQuickStartListener = new OnClickListener() {
        public void onClick(View v) {
          setContentView(R.layout.quickstart);
        }
    };
    private OnClickListener mGameSetupListener = new OnClickListener() {
        public void onClick(View v) {
          setContentView(R.layout.gamesetup);
        }
    };
    private OnClickListener mSettingsListener = new OnClickListener() {
        public void onClick(View v) {
          setContentView(R.layout.settings);
        }
    };
    private OnClickListener mWordEntryListener = new OnClickListener() {
        public void onClick(View v) {
          setContentView(R.layout.word);
        }
    };
}

1 个答案:

答案 0 :(得分:1)

看看我在这里的代码,这应该可以帮到你。

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class SmartApp extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.intro);

        final Button firstTimeButton = (Button) findViewById(R.id.firstTimeButton);
        firstTimeButton.setOnClickListener(
                new View.OnClickListener()
        {
                        @Override
                        public void onClick(View v)
                        {
                                // TODO Auto-generated method stub
                                Intent userCreationIntent = new Intent(v.getContext(), UserCreation.class);
                                startActivityForResult(userCreationIntent, 0);
                        }
                });
    }
}

当用户点击“第一次按钮”时,用户将被带到“用户创建页面”。我相信你的代码有一些错误。将您与我提供的内容进行比较,您应该能够看到差异并进行适当的修改。如果这有帮助,请告诉我!