0

我制作了一个应用程序,它从 SD 卡中获取照片并将它们放在一个ViewFlipper,但是当我启动它时,它突然停止了。

我在 Eclipse 中没有错误(我是一个 android 应用程序)。

这是java代码:

public class Images extends Activity implements OnClickListener  {
    static Button btnNext;
    static Button btnBack;
    static ViewFlipper Flipper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    setContentView(R.layout.images);
    final File f = new File(Environment.getExternalStorageDirectory().getPath()+"/images");
    btnNext= (Button) findViewById(R.id.btn_plus);
    btnBack= (Button) findViewById (R.id.btn_minus);
    Flipper=(ViewFlipper) findViewById (R.id.flipper);

    btnNext.setOnClickListener(this);
    btnBack.setOnClickListener(this);

    final int imageCount = f.listFiles().length;
      for (int count = 0; count < imageCount - 1; count++) {
       ImageView imageView = new ImageView(this);
       Bitmap bmp = BitmapFactory.decodeFile(f.listFiles()[count].getAbsolutePath());
       imageView.setImageBitmap(bmp);
      Flipper.addView(imageView);
      }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        switch (arg0.getId()){

        case(R.id.btn_minus):
         Flipper.showPrevious();
        break;

        case(R.id.btn_plus):
         Flipper.showNext();    
        break;  
        }
    }
}

这是日志猫:

06-01 08:22:29.418: E/AndroidRuntime(912): FATAL EXCEPTION: main
06-01 08:22:29.418: E/AndroidRuntime(912): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tendarius.voicebrowser/com.tendarius.voicebrowser.Images}: java.lang.NullPointerException
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.os.Looper.loop(Looper.java:137)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-01 08:22:29.418: E/AndroidRuntime(912):  at java.lang.reflect.Method.invokeNative(Native Method)
06-01 08:22:29.418: E/AndroidRuntime(912):  at java.lang.reflect.Method.invoke(Method.java:511)
06-01 08:22:29.418: E/AndroidRuntime(912):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-01 08:22:29.418: E/AndroidRuntime(912):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-01 08:22:29.418: E/AndroidRuntime(912):  at dalvik.system.NativeStart.main(Native Method)
06-01 08:22:29.418: E/AndroidRuntime(912): Caused by: java.lang.NullPointerException
06-01 08:22:29.418: E/AndroidRuntime(912):  at com.tendarius.voicebrowser.Images.onCreate(Images.java:36)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.Activity.performCreate(Activity.java:5104)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-01 08:22:29.418: E/AndroidRuntime(912):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-01 08:22:29.418: E/AndroidRuntime(912):  ... 11 more
4

1 回答 1

0
06-01 08:22:29.418: E/AndroidRuntime(912): Caused by: java.lang.NullPointerException
06-01 08:22:29.418: E/AndroidRuntime(912):  at com.tendarius.voicebrowser.Images.onCreate(Images.java:36)

看起来你的问题在第 36 行。确保您引用的对象不为空。例如,

Environment.getExternalStorageDirectory().getPath()
-> Environment.getExternalStorageDirectory() 不能为空

imageView.setImageBitmap(bmp);
-> imageView 不能为空

在第 36 行设置断点并查看是否有一个为 null 的对象,然后找出它变为 null 的原因 - 可能是由于您的应用程序不可用的资源

于 2013-06-01T10:29:29.870 回答