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変更するだけで、次の に切り替えることはありませんlayoutActivity

 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 は、Activity を切り替えるための Intent メカニズムを提供します

すなわち使用

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

それ以外の

setContentView(R.layout.selectscreen);

コードの残りの部分は正常に動作する必要があります。

于 2013-08-15T12:35:38.820 に答える
1

finish()最初のアクティビティを 1 回だけ使用する場合に使用できます。

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

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

Manifest.xml で 2 番目のアクティビティが定義されていることを確認してください。

<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 に答える