6

我想要做的是一个主屏幕停留 5 秒并进入活动 1。当我点击活动 1 中的按钮时,我会进入活动 2。我尝试了很多次单击按钮,但没有发生切换。主屏幕(5 秒)=Main_Activity Activity1=selectpets.java Activity2=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;
    }
}

现在是 Selectpets 代码

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

    }
   }

鱼缸类代码

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

4 回答 4

4

在这里你永远不会切换到下一个Activity,只是改变layout当前的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);

由于您实际上并没有进入下一个Activity(java 文件),因此您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()功能的不同

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() {
            Intent i = new Intent(MainActivity.this, SelectPetsScreen.this);
            startActivity(i);
            }
        }, 5000);   
}
于 2013-08-15T12:32:15.920 回答
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);

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

于 2013-08-15T12:35:38.820 回答
1

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 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>
于 2013-08-15T12:38:03.933 回答
0

在 Maxim Shoustin 和大家的帮助下,我能够通过以下方式解决它:向 Manifest.xml 添加第二个 Activity,即 SelectPetsScreen 非常感谢

于 2013-08-15T17:15:40.887 回答