2013-06-01 76 views
0

我製作了一個應用程序,它將SD卡中的照片放在ViewFlipper上,但是當我啓動它時,它會突然停止。當我嘗試啓動我的android應用程序時發生錯誤(A viewFlipper)

我在eclipse中沒有錯誤(its是一個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; 
     } 
    } 
} 

這裏是logcat的:

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 
+0

確保您已在AndroidManifest中添加SD-CARD權限? –

+0

你真的需要問題中的進口和空行嗎? – Siddharth

+0

1.在Eclipse中沒有任何錯誤是毫無意義的,因爲如果您有任何錯誤,您將無法運行您的應用程序,並且在修復編譯時錯誤之前無法獲取運行時錯誤。 2.在詢問異常情況時,請始終發佈logcat堆棧跟蹤。 3.你應該學會如何閱讀logcat以及如何使用調試器,那麼這個問題將會很容易找到。 – Simon

回答

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) 

看起來你的問題是line36。確保你指的是一個非空的對象。例如,

Environment.getExternalStorageDirectory()的getPath()
- > Environment.getExternalStorageDirectory()不能爲空

imageView.setImageBitmap(BMP)。
- > ImageView的不能爲空

設置上線36斷點,看看是否有一個對象,它爲null,則找出原因,它變成空 - 也許這可能是由於資源不可用到您的應用程序

+0

我沒有找到任何東西! –

+0

你能指出我的錯誤嗎? –

+0

什麼是代碼中的第36行? – g3han

相關問題