Android在活动之间切换

时间:2013-08-15 12:29:20

标签: android android-activity screen switch-statement

我要做的是一个主屏幕,它会停留5秒钟并进入活动状态。当我点击activity1中的按钮时,我会进行活动。我已经多次尝试点击按钮但没有切换发生。     主屏幕(5秒)= Main_Activity     活动1 = selectpets.java     活性2 = fishtank.java

onclick听众似乎问题我不知道它有什么问题

     Main Activity Code
package com.set.petshome;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
    Button fButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Delay Code after 5 seconds
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setContentView(R.layout.selectscreen); //where <next> is you target      activity :)
                }
            }, 5000);   
    }
//Delay End
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

现在选择代码

package com.set.petshome;

import android.app.Activity;
import android.content.*;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
public class SelectPetsScreen extends Activity  {
    Button fButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.selectscreen);

      //Button Fishtank Listener Start

        fButton = (Button) findViewById(R.id.button1);

          //Listening to button event
           fButton.setOnClickListener(new View.OnClickListener() {

                public void onClick(View arg0) {
                    //Starting a new Intent
                    Intent nextScreen = new Intent(getApplicationContext(),  fishtank.class);
                    startActivity(nextScreen);

                }
            });     
        //Button Fishtank Listener End

    }
   }

Fishtank类代码

package com.set.petshome;

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

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



        }

    }

顺便说一下,点击
之后,应用程序中没有任何错误也没有切换     非常感谢你

4 个答案:

答案 0 :(得分:4)

此处您不会切换到下一个Activity,只需更改当前layout的{​​{1}}

Activity

而不是 new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); 您需要使用setContentView()

Intent

由于您实际上没有进入下一个Intent i = new Intent(MainActivity.this, SelectPetsScreen.this); startActivity(i); (java文件),因此未设置Activity

修改

这就是你正在做的事情

onClick()

这就是你应该做的。请注意public class MainActivity extends Activity { Button fButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Delay Code after 5 seconds new Handler().postDelayed(new Runnable() { @Override public void run() { setContentView(R.layout.selectscreen); //where <next> is you target activity :) } }, 5000); } 函数

中的差异
run()

答案 1 :(得分:1)

避免这样做。

 setContentView(R.layout.activity_main);
        //Delay Code after 5 seconds
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                setContentView(R.layout.selectscreen); //where <next> is you target      activity :)
                }
            }, 5000);   

Google为您提供了切换活动的Intent机制

即。使用

startActivity(new Intent(this, yourSecondActivity.class));

而不是

setContentView(R.layout.selectscreen);

您的代码的其余部分必须正常工作。

答案 2 :(得分:1)

如果您只想使用firts Activity一次,则可以使用finish()

mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {

 startActivity(new Intent().setClass(MainActivity.this, SelectPetsScreen .class).setData(getIntent().getData()));
        finish();
    }
}, 5000);

确保您在Manifest.xml中定义了第二个Activity:

<activity android:name="x.x.SelectPetsScreen"
        android:theme="@style/NoTitle"
        android:screenOrientation="nosensor"
        android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>

答案 3 :(得分:0)

我能够在Maxim Shoustin和大家的帮助下解决它: 将第二个Activity添加到Manifest.xml,即SelectPetsScreen 非常感谢你