0

我不熟悉使用 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);

    }

日志猫:

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
4

1 回答 1

0

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

至于您的 NPE,请确保您的视图实际上链接到您的布局文件中的某些内容R.layout.splash

查看您的代码后,您没有将启动对象分配给布局中的实际视图。像这样分配:

splash = (ImageView)findViewById(R.id.youridhere);
于 2013-04-04T23:32:49.843 回答