为什么我的启动画面不显示图像?我的、图像、画面

2023-09-12 10:38:40 作者:無法言语の爱

我已经创建了Android 0.1的工作室1闪屏,但是当我在调试模式下测试它在我的手机(Nexus S的)与USB图像不显示。为什么?

这是MainActivity

 包com.example.splash;

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

公共类MainActivity延伸活动{

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.splash);

    处理器X =新的处理程序();
    x.postDelayed(新SplashHandler(),7000);



}
类SplashHandler实现Runnable {

    公共无效的run(){


     startActivity(新意图(getApplication(),Main.class));
        MainActivity.this.finish();


    }


}


@覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

}
 

这是主要

 包com.example.splash;


进口android.app.Activity;

公共类主要扩展活动{

}
 
关于电脑花屏图片就是我开机运行了程序后就出现的画面在不运行程序的状态下,连续开机20小时都不会

这是Splash.xml

 < ?XML版本=1.0编码=UTF-8&GT?;

    < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                  机器人:方向=垂直
                  机器人:layout_width =match_parent
                  机器人:layout_height =match_parent机器人:背景=@可绘制/扑通一声>

< / LinearLayout中>
 

解决方案

不要使用应用程序上下文。要知道何时使用活动内容和应用程序上下文请查看以下尤其是答案的链接通过commonsware

When调用活动上下文或应用程序上下文?

我在后测试了code。它适用于我的设备三星Galaxy S3。唯一的变化我做了有在RelativeLayout的一个ImageView的,并设置图像同在的onCreate()。 我也用一个活动场景。除此之外,您的code是罚款。

使用处理器闪屏

 公共类飞溅延伸活动{
私有静态最终诠释SPLASH_TIME = 2 * 1000; //3秒延迟

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.splash);
    ImageView的IV =(ImageView的)findViewById(R.id.imageView1);
    iv.setBackgroundResource(R.drawable.afor);
    尝试 {
           新的处理程序()。postDelayed(新的Runnable(){

        公共无效的run(){

            意向意图=新的意图(Splash.this,
                MainActivity.class);
            startActivity(意向);

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

    }赶上(例外五){}
}
 @覆盖
公共无效onBack pressed(){
    this.finish();
    super.onBack pressed();
}
}
 

splash.xml

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直机器人:后台=#ffffaa>

< ImageView的
    机器人:ID =@ + ID / imageView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT
    机器人:layout_centerInParent =真
     />

 < / RelativeLayout的>
 

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>

解决方案

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>