开发者

Using buttons to switch views with Android SDK

开发者 https://www.devze.com 2023-01-25 00:16 出处:网络
I\'m having trouble switching views with button presses in my Android app. The code shows no errors in Eclipse, but the app quits unexpectedly in the emulator when the button is clicked. My code is be

I'm having trouble switching views with button presses in my Android app. The code shows no errors in Eclipse, but the app quits unexpectedly in the emulator when the button is clicked. My code is below. Thanks

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.mai开发者_运维技巧nmenu);

        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);
        }
    };
}


Take a look at this code that i have here, this should help you out some.

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);
                        }
                });
    }
}

When the user clicks the "first time button" the user will be taken to the "user creation page". I believe in your code you have a few things wrong. Compare yours to what i provided and you should be able to see the differences and make the appropriate modifications. Let me know if this helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消