wait()函数运行不正常

时间:2013-04-04 23:28:25

标签: java android eclipse wait

我是使用wait()的新手;特征。我以为我做得对,但它不会运行。通过查看示例,我无法弄清楚出错的地方。有人可以帮帮我吗?提前谢谢!

public class MainActivity
    extends Activity
{
    private ImageView splash;


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


    private void initialize()
    {
        splash.setVisibility(View.VISIBLE);
        try
        {
            wait(5000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        mainMenu();

    }


    private void mainMenu()
    {
        setContentView(R.layout.mainmenu);

    }

logcat的:

04-04 23:08:40.075: E/Trace(2555): error opening trace file: No such file or directory (2)
04-04 23:08:48.345: E/AndroidRuntime(2555): FATAL EXCEPTION: main
04-04 23:08:48.345: E/AndroidRuntime(2555): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.MainActivity}: java.lang.NullPointerException
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.os.Looper.loop(Looper.java:137)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at dalvik.system.NativeStart.main(Native Method)
04-04 23:08:48.345: E/AndroidRuntime(2555): Caused by: java.lang.NullPointerException
04-04 23:08:48.345: E/AndroidRuntime(2555):     at edu.virginia.hotncolduva.MainActivity.initialize(MainActivity.java:24)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at edu.virginia.hotncolduva.MainActivity.onCreate(MainActivity.java:19)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.Activity.performCreate(Activity.java:5104)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 23:08:48.345: E/AndroidRuntime(2555):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 23:08:48.345: E/AndroidRuntime(2555):     ... 11 more
04-04 23:12:09.646: E/AndroidRuntime(2639): FATAL EXCEPTION: main
04-04 23:12:09.646: E/AndroidRuntime(2639): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.MainActivity}: java.lang.NullPointerException
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.os.Looper.loop(Looper.java:137)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at dalvik.system.NativeStart.main(Native Method)
04-04 23:12:09.646: E/AndroidRuntime(2639): Caused by: java.lang.NullPointerException
04-04 23:12:09.646: E/AndroidRuntime(2639):     at edu.virginia.hotncolduva.MainActivity.initialize(MainActivity.java:24)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at edu.virginia.hotncolduva.MainActivity.onCreate(MainActivity.java:19)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.Activity.performCreate(Activity.java:5104)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 23:12:09.646: E/AndroidRuntime(2639):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 23:12:09.646: E/AndroidRuntime(2639):     ... 11 more
04-04 23:16:21.095: E/AndroidRuntime(2737): FATAL EXCEPTION: main
04-04 23:16:21.095: E/AndroidRuntime(2737): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.MainActivity}: java.lang.NullPointerException
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.os.Looper.loop(Looper.java:137)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at dalvik.system.NativeStart.main(Native Method)
04-04 23:16:21.095: E/AndroidRuntime(2737): Caused by: java.lang.NullPointerException
04-04 23:16:21.095: E/AndroidRuntime(2737):     at edu.virginia.hotncolduva.MainActivity.initialize(MainActivity.java:27)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at edu.virginia.hotncolduva.MainActivity.onCreate(MainActivity.java:20)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.Activity.performCreate(Activity.java:5104)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 23:16:21.095: E/AndroidRuntime(2737):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 23:16:21.095: E/AndroidRuntime(2737):     ... 11 more
04-04 23:20:05.727: E/AndroidRuntime(2833): FATAL EXCEPTION: main
04-04 23:20:05.727: E/AndroidRuntime(2833): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.virginia.hotncolduva/edu.virginia.hotncolduva.MainActivity}: java.lang.NullPointerException
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.os.Looper.loop(Looper.java:137)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at java.lang.reflect.Method.invoke(Method.java:511)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at dalvik.system.NativeStart.main(Native Method)
04-04 23:20:05.727: E/AndroidRuntime(2833): Caused by: java.lang.NullPointerException
04-04 23:20:05.727: E/AndroidRuntime(2833):     at edu.virginia.hotncolduva.MainActivity.initialize(MainActivity.java:27)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at edu.virginia.hotncolduva.MainActivity.onCreate(MainActivity.java:20)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.Activity.performCreate(Activity.java:5104)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-04 23:20:05.727: E/AndroidRuntime(2833):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-04 23:20:05.727: E/AndroidRuntime(2833):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您应该考虑采用不同的方法,而不是在主线程中休眠。为什么不使用基于1s计时器的处理程序,然后向UI线程发送消息以更改启动画面(alpha或其他),并且一旦时间过去,发送消息以使用a加载下一个内容视图淡出动画。

对于您的NPE,请确保您的视图实际上与布局文件R.layout.splash中的某些内容相关联。

查看代码后,您不会将splash对象分配给布局中的实际视图。像这样分配:

splash = (ImageView)findViewById(R.id.youridhere);