第一个活动视图不会出现

时间:2014-04-01 16:39:38

标签: java android

我是android新手。我有两个活动Splash和MainActivity。当我启动我的应用程序时,Splash活动开始(正如它应该的那样),它会播放声音,但背景图像不会出现,几秒钟后我的MainActivity就会启动。提前谢谢!

MainActivity类的代码

Package com.example.button;

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

public class MainActivity extends Activity {
    int counter;
    Button add;
    Button sub;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    counter=0;
    add=(Button)findViewById(R.id.button1);
    sub=(Button)findViewById(R.id.button2);
    display=(TextView)findViewById(R.id.textView1);
    }


    @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;
    }
public void add(View view)
{
    counter=counter+1;
    display.setText("your total is"+counter);
}

public void sub(View view)
{
     counter--;
     display.setText("your total is"+counter);
}

}

Splash类的代码

package com.example.button;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;

public class Splash extends Activity{
MediaPlayer ourSong;
Thread timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
     ourSong=MediaPlayer.create(Splash.this,R.raw.addicted);
    ourSong.start();
             timer=new Thread();
            timer.start();                      
            run();
            //{
                        //};


    }

    public void run()
    {

        try {


            timer.sleep(5000);

        }catch(InterruptedException e){
        e.printStackTrace();

        }finally {
Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY");
            startActivity(openStartingPoint);
        }
        }               




    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
ourSong.release();
    }





}

splash.xml的代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@drawable/feather">

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

 </LinearLayout>

3 个答案:

答案 0 :(得分:0)

我认为你没有正确使用Thread。更改代码的这三行

    timer=new Thread();
    timer.start();                      
    run();

    timer=new Thread(new Runnable(){
        public void run() {
            try {
                sleep(5000);
            }catch(InterruptedException e){
                e.printStackTrace();
            }finally {
                Intent openStartingPoint=new Intent("com.example.button.MAINACTIVITY");
                startActivity(openStartingPoint);
            }
        }               
    });
    timer.start();

并从您的活动中删除run方法。 通过这种方式,您可以让启动活动在屏幕上显示5秒钟。

答案 1 :(得分:0)

将此代码用作您的启动活动:

public class SplashActivity extends Activity {
    int SPLASH_DISPLAY_LENGHT = 1000;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.splash_form);

        InitilizeUi();
    }

    private void InitilizeUi() {
        // play your sound
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent mainIntent = new Intent(SplashActivity.this, AboutActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, SPLASH_DISPLAY_LENGHT);
    }
}

答案 2 :(得分:0)

在您的splash.xml中

获取 imageview 并将图片文件放在那里。

例如android:src="file.gif"

希望这可以帮助你