Splashscreen访问drawable时出现Nullpointer异常

时间:2011-09-30 08:46:35

标签: android nullpointerexception forceclose

我建立了一个闪屏,然后显示5秒,然后进入我的主要活动。在这个启动画面中,我有一个boolen,它检查应用程序是否第一次打开,如果是,它会将一些内容放入我的数据库中。但到目前为止,我得到了一个N​​ullPointerException。

我不明白为什么它不起作用。应该在我的眼里。这也很奇怪,因为我在demoapp中测试了没有错误的代码。

所以这是我的SplashScreenActivity.java的代码

    package de.retowaelchli.filterit;


    import org.apache.http.util.ByteArrayBuffer;
    import android.app.Activity;
    import android.content.ContextWrapper;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.preference.PreferenceManager;
    import android.widget.ImageView;

    import de.retowaelchli.filterit.database.DBAdapter;
    import de.retowaelchli.filterit.database.SmileyDBAdapter;

    public class SplashScreenActivity extends Activity {

        //Variablen definieren
        public static final String KEY_APP_IS_INITIALIZED = "APP_INITIALIZED";

        public SmileyDBAdapter mDbHelper;
        private DBAdapter db;

        ImageView iv;
        ContextWrapper context;
        ByteArrayBuffer baf;

       /** Called when the activity is first created. */
       @Override
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.splash);

          //Datenbank filling
          db = new DBAdapter(this);
          db.open();
          db.close();
          mDbHelper = new SmileyDBAdapter(this);
          startupcheck();

          //Splash-Screen Animation
          Thread splashThread = new Thread() {

              @Override
              public void run() {
                try {
                   int waited = 0;
                    while (waited < 5000) {
                          sleep(100);
                          waited += 100;
                            }
                        } 
                        catch (InterruptedException e) {
                            // do nothing
                        } 
                    finally {
                        finish();
                        Intent i = new Intent();
                        i.setClassName("de.retowaelchli.filterit",
                                  "de.retowaelchli.filterit.StartseiteActivity");
                        startActivity(i);
                        }
                     }
                  };
                  splashThread.start();
       }

       //Hier wird getestet ob das App bereits die Datenbankeinträge besitzt
       private void startupcheck(){
       SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(SplashScreenActivity.this);
              boolean appIsInitialized = prefs.getBoolean(KEY_APP_IS_INITIALIZED, false);
                if (appIsInitialized == false){

                    //Hier werden die Smiley gefüllt
                    devil();  <--THIS IS GIVING ME A NULLPOINTEREXCEPTION


                   //Hier wird eingetragen das die Datenbank gefüllt wurde
                    Editor editor = prefs.edit();
                    editor.putBoolean(KEY_APP_IS_INITIALIZED, true);
                    editor.commit();                
                }

             }

       /** Hier sind alle Datenbank Einträge definiert**/
       private void devil(){

            int drawableID = context.getResources().getIdentifier("devil", "drawable", getPackageName());
            iv.setImageResource(drawableID);

            String info = String.valueOf(drawableID);

            mDbHelper.open();

            mDbHelper.createSmiley("You received a satanic message", info);

            mDbHelper.close();
        }
}

可能它有一些事情要做,drawable没有加载,但我无法想象为什么它没有加载....

所以,这是我的LogCat:

09-30 10:44:04.808: INFO/ActivityManager(1760): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=de.retowaelchli.filterit/.SplashScreenActivity } from pid 3193
09-30 10:44:04.838: VERBOSE/HtcAppUsageStats(1760): (launch app, package): (Filter It, de.retowaelchli.filterit)
09-30 10:44:04.858: INFO/ActivityManager(1760): Start proc de.retowaelchli.filterit for activity de.retowaelchli.filterit/.SplashScreenActivity: pid=9074 uid=10150 gids={}
09-30 10:44:04.868: INFO/SuperPowerSvc(16326): [SuperPowerSvc] App launched: [de.retowaelchli.filterit]
09-30 10:44:04.918: ERROR/jdwp(9074): Failed sending reply to debugger: Broken pipe
09-30 10:44:04.918: DEBUG/dalvikvm(9074): Debugger has detached; object registry had 1 entries
09-30 10:44:05.038: VERBOSE/levels(6925): ::Bundle[{icon-small=17302166, present=true, scale=100, level=75, technology=Li-ion, status=3, voltage=3965, plugged=2, health=2, temperature=270}]
09-30 10:44:05.548: DEBUG/Database(9074): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, flag = 6
09-30 10:44:05.548: DEBUG/Database(9074): dbopen(): path = /data/data/de.retowaelchli.filterit/databases/filterit, free size = 689
09-30 10:44:05.578: DEBUG/AndroidRuntime(9074): Shutting down VM
09-30 10:44:05.578: WARN/dalvikvm(9074): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074): FATAL EXCEPTION: main
09-30 10:44:05.628: ERROR/AndroidRuntime(9074): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.retowaelchli.filterit/de.retowaelchli.filterit.SplashScreenActivity}: java.lang.NullPointerException
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.os.Looper.loop(Looper.java:143)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread.main(ActivityThread.java:4196)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at java.lang.reflect.Method.invokeNative(Native Method)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at java.lang.reflect.Method.invoke(Method.java:507)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at dalvik.system.NativeStart.main(Native Method)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074): Caused by: java.lang.NullPointerException
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at de.retowaelchli.filterit.SplashScreenActivity.devil(SplashScreenActivity.java:109)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at de.retowaelchli.filterit.SplashScreenActivity.startupcheck(SplashScreenActivity.java:76)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at de.retowaelchli.filterit.SplashScreenActivity.onCreate(SplashScreenActivity.java:40)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
09-30 10:44:05.628: ERROR/AndroidRuntime(9074):     ... 11 more
09-30 10:44:05.628: WARN/ActivityManager(1760):   Force finishing activity de.retowaelchli.filterit/.SplashScreenActivity
09-30 10:44:06.128: WARN/ActivityManager(1760): Activity pause timeout for HistoryRecord{40a95900 de.retowaelchli.filterit/.SplashScreenActivity}
09-30 10:44:06.228: WARN/ResourceType(1760): Skipping entry 0x7f040005 in package table 0 because it is not complex!

Thx为您提前帮助!

2 个答案:

答案 0 :(得分:3)

我没有看到你从onCreate()开始你的ImageView iv的任何地方。

答案 1 :(得分:1)

 ImageView iv;

因为你没有对此进行初始化并且说iv.setImageResource(drawableID);就像null.setImageResource(drawableID);