【问题标题】:Android Switch between activitiesAndroid 在活动之间切换
【发布时间】:2013-08-15 12:29:20
【问题描述】:

我想要做的是一个主屏幕停留 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);



        }

    }

顺便说一句,应用程序中没有错误,只是点击后没有切换
非常感谢

【问题讨论】:

  • 您正在更改setContentView()。这不会启动您的 SelectPetsScreen 活动,因此您将永远不会分配给它的按钮单击侦听器。因为你还在MainActivity :)
  • 您是否向 Manifest.xml 添加了第二个 Activity?如果是,请尝试添加到 SelectPetsScreen:`android:launchMode="singleTask"`

标签: android android-activity screen switch-statement


【解决方案1】:

在这里,您永远不会切换到下一个Activity,只需更改当前Activitylayout

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

你需要使用Intent而不是setContentView()

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

【讨论】:

  • 是的,我知道我想从活动 1 切换到活动 2,并且主屏幕更改为你正在谈论的活动 1。你提到的代码我知道这不会改变活动。再看看你会看到您的代码我已经使用了它,但没有将 activity1 更改为 2:D
  • 不,在您的MainActivity 中,您没有这样做。再看我的回答。您没有使用Intent 来启动下一个Activity,这是您设置onClickListener 的位置。您只是更改layout 而不是Activity
  • 现在我明白了,但是在更改代码后系统 UI 在 MainActivity 上停止(崩溃)
  • 我不能告诉你为什么不知道错误是什么。 Fatal Exception 的 logcat 中的内容是什么,之后引用您的包的第一行是什么?
【解决方案2】:

如果你只想使用第一次活动,你可以使用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>

【讨论】:

  • @user1928775 您是否在 Manifest.xml 中添加了第二个 Activity?
  • @user1928775 尝试添加到 SelectPetsScreen: in manifest.xml: `android:launchMode="singleTask"`
  • 感谢 Maxim Shoustin 和在 manifest.xml 中添加 SelectPetsScreen 后工作的所有人
【解决方案3】:

避免这样做。

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

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

【讨论】:

    【解决方案4】:

    在 Maxim Shoustin 和大家的帮助下,我通过以下方式解决了这个问题: 将第二个 Activity 添加到 Manifest.xml,即 SelectPetsScreen 非常感谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      相关资源
      最近更新 更多