Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

android - why my splash screen don't show the images?

I've created one splash screen with android studio 0.1, but when I test it on my phone(nexus s) in debugging mode with usb the image isn't show.. why?

this is the MainActivity

package com.example.splash;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);

    Handler x = new Handler();
    x.postDelayed(new SplashHandler(), 7000);



}
class SplashHandler implements Runnable{

    public void run(){


     startActivity(new Intent(getApplication(), Main.class));
        MainActivity.this.finish();


    }


}


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

}

this is Main

package com.example.splash;


import android.app.Activity;

public class Main extends Activity {   

}

this is Splash.xml

< ?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:orientation="vertical"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" android:background="@drawable/splash">

</LinearLayout>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Don't use a application context. To know when to use activity context and application context pls check the link below especially the answer by commonsware

When to call activity context OR application context?

I tested your code in the post. It works on my device samsung galaxy s3. Only Change i made was having a imageview in the RelativeLayout and set the image for the same in onCreate(). I also used a activity context. Other than that your code is fine.

Splash screen using handler

public class Splash extends Activity {
private static final int SPLASH_TIME = 2 * 1000;// 3 seconds delay

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ImageView iv= (ImageView) findViewById(R.id.imageView1);
    iv.setBackgroundResource(R.drawable.afor);
    try {
           new Handler().postDelayed(new Runnable() {

        public void run() {

            Intent intent = new Intent(Splash.this,
                MainActivity.class);
            startActivity(intent);

            Splash.this.finish();
        }     
    }, SPLASH_TIME);
        }

    } catch(Exception e){}
}
 @Override
public void onBackPressed() {
    this.finish();
    super.onBackPressed();
}
}

splash.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:background="#ffffaa">

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
     />

 </RelativeLayout>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...