第二个活动不加载

时间:2011-05-31 05:52:41

标签: android android-activity

我是Android开发的新手,我试图在第一次活动延迟一段时间后加载第二项活动,但尚未取得成功。

这是我的splashactivity代码

package com.test.android.app;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class SplashActivity extends Activity{

    LinearLayout linearLayout;
    Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("SplashActivity onCreate method called up");

        linearLayout = new LinearLayout(this);


        ImageView image = new ImageView(this);
        image.setImageResource(R.drawable.rc_logo);
        image.setAdjustViewBounds(true);
        image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //linearLayout.setFrame(30,30,image.getWidth(),image.getHeight());
     // Add the ImageView to the layout and set the layout as the content view
        linearLayout.addView(image);
        linearLayout.setGravity(Gravity.CENTER);
        setContentView(linearLayout);


        timer = new Timer();

        timer.schedule(new LoadHomeScreenTask(this), 5*1000);

    }
    protected void onStart()
    {
        super.onStart();
        Log.d("SplashActivity","onStart called up");

    }

    protected void onRestart()
    {
        super.onRestart();
        Log.d("SplashActivity","onRestart called up");
    }

    protected void onResume()
    {
        super.onResume();

        Log.d("SplashActivity","onResume called up");
    }

    protected void onPause()
    {
        super.onPause();

        Log.d("SplashActivity","onPause called up");
    }

    protected void onStop()
    {
        super.onStop();

        Log.d("SplashActivity","onStop called up");
    }

    protected void onDestroy()
    {
        super.onDestroy();
        Log.d("SplashActivity","onDestroy method called up");
    }

    public void startHomeActivity()
    {
        Log.d("SplashActivity","startActivity called up");

        //Intent intent = new Intent(SplashActivity.this,HomeScreenActivity.class);

        //HomeScreenActivity homeScreenActivity = new HomeScreenActivity();
        startActivity(new Intent(SplashActivity.this,HomeScreenActivity.class));
       finish();
    }

}

class LoadHomeScreenTask extends TimerTask {
    SplashActivity splashActivity;
    public LoadHomeScreenTask(SplashActivity splashActivity)
    {
        this.splashActivity = splashActivity;
    }
    public void run() {
        Log.d("LoadHomeScreenTask"," run method called up");
        this.splashActivity.startHomeActivity();
        this.splashActivity.timer.cancel();
    }
}

以下是我的第二项活动

package com.test.android.app;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class HomeScreenActivity extends Activity {

LinearLayout linearLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("HomeScreenActivity", "oncreate called up");
        linearLayout = new LinearLayout(this);
        linearLayout.setBackgroundColor(Color.GREEN);

        ImageView image = new ImageView(this);
        image.setImageResource(R.drawable.hanuman);
        image.setAdjustViewBounds(true);
        image.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        //linearLayout.setFrame(30,30,image.getWidth(),image.getHeight());
     // Add the ImageView to the layout and set the layout as the content view
        linearLayout.addView(image);
        linearLayout.setGravity(Gravity.CENTER);

        setContentView(linearLayout);


    }
    protected void onStart()
    {
        super.onStart();
        Log.d("HomeScreenActivity","onStart method called up");

    }

    protected void onRestart()
    {
        super.onRestart();
        Log.d("HomeScreenActivity","onRestart method called up");
    }

    protected void onResume()
    {
        super.onResume();
        Log.d("HomeScreenActivity","onResume method called up");
    }

    protected void onPause()
    {
        super.onPause();
        Log.d("HomeScreenActivity","onPause method called up");
    }

    protected void onStop()
    {
        super.onStop();
        Log.d("HomeScreenActivity","onStop method called up");
    }

    protected void onDestroy()
    {
        super.onDestroy();
        Log.d("HomeScreenActivity","onDestroy method called up");
    }



}

这是logchat

05-31 11:14:01.509: DEBUG/SplashActivity(798): onStart called up
05-31 11:14:01.509: DEBUG/SplashActivity(798): onResume called up
05-31 11:14:01.740: INFO/ActivityManager(52): Displayed activity com.rajcomics.android.comicsapp/.SplashActivity: 1065 ms (total 1065 ms)
05-31 11:14:06.503: DEBUG/LoadHomeScreenTask(798):  run method called up
05-31 11:14:06.503: DEBUG/SplashActivity(798): startActivity called up
05-31 11:14:06.509: INFO/ActivityManager(52): Starting activity: Intent { cmp=com.rajcomics.android.comicsapp/.HomeScreenActivity }

请帮我找出解决方案。

1 个答案:

答案 0 :(得分:0)

尝试以下


public class SplashActivity extends Activity {
protected int _splashTime = 3000;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            finish();
            startActivity(new Intent(SplashActivity.this,HomeScreenActivity.class));
        }
    }, _splashTime);
  }
}