如果列表中没有Photo,如何显示Toast,我正在创建一个应用程序,允许用户拍摄照片并将该照片存储到自动创建的特定文件夹中,
每当我点击“查看”按钮时,如果用户已经拍摄了屏幕,那么它会在列表中显示这些照片,如果用户没有拍摄任何照片然后获取“异常”,那么这里我想要显示一个Toast消息,如果用户有未拍摄任何照片。
logcat的:
07-22 06:48:28.248: E/AndroidRuntime(841): FATAL EXCEPTION: main
07-22 06:48:28.248: E/AndroidRuntime(841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example/com.example.UploadActivity}: java.lang.NullPointerException
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.os.Looper.loop(Looper.java:137)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-22 06:48:28.248: E/AndroidRuntime(841): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 06:48:28.248: E/AndroidRuntime(841): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 06:48:28.248: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-22 06:48:28.248: E/AndroidRuntime(841): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-22 06:48:28.248: E/AndroidRuntime(841): at dalvik.system.NativeStart.main(Native Method)
07-22 06:48:28.248: E/AndroidRuntime(841): Caused by: java.lang.NullPointerException
07-22 06:48:28.248: E/AndroidRuntime(841): at com.example.UploadActivity.getSD(UploadActivity.java:125)
07-22 06:48:28.248: E/AndroidRuntime(841): at com.example.UploadActivity.onCreate(UploadActivity.java:106)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.Activity.performCreate(Activity.java:5104)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-22 06:48:28.248: E/AndroidRuntime(841): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-22 06:48:28.248: E/AndroidRuntime(841): ... 11 more
SingleAngelActivity.java:
Button captureButton = (Button) findViewById(R.id.btnCapture);
Log.d(SingleAngelActivity.LOG_TAG, "captureButton :: " + captureButton);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPicture);
Log.d(SingleAngelActivity.LOG_TAG, "mCamera.takePicture :: " + mCamera);
}
});
Button viewButton = (Button) findViewById(R.id.btnView);
Log.d(SingleAngelActivity.LOG_TAG, "SingleAngelActivityButton :: " + viewButton);
viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentNewEvent = new Intent(SingleAngelActivity.this, UploadActivity.class);
startActivity(intentNewEvent);
}
});
UploadActivity.java:
private List <String> getSD()
{
List <String> it = new ArrayList <String>();
String string = "/mnt/sdcard/Pictures/MyNewImages/";
File f = new File (string+ SingleAngelActivity.customFolder+ "/");
Log.d(UploadActivity.LOG_TAG, "KEY_TITLE :::--- " + f);
File[] files = f.listFiles ();
Log.d(UploadActivity.LOG_TAG, "getListImages() :::--- " + files);
for (int i = 0; i <files.length; i++)
{
File file = files[i];
Log.d(UploadActivity.LOG_TAG, "i<files.length() :::--- " + i);
Log.d("Count",file.getPath());
it.add (file.getPath());
}
Log.d(UploadActivity.LOG_TAG, "List <String> it :::--- " + it);
return it;
}
答案 0 :(得分:2)
仅在安装了SD卡,路径正确且没有其他问题时才有效。它只是检查文件夹是否有任何文件
尝试以下代码
private List <String> getSD()
{
List <String> it = new ArrayList <String>();
String string = "/mnt/sdcard/Pictures/MyNewImages/";
File f = new File (string+ SingleAngelActivity.customFolder+ "/");
Log.d(UploadActivity.LOG_TAG, "KEY_TITLE :::--- " + f);
File[] files = f.listFiles ();
Log.d(UploadActivity.LOG_TAG, "getListImages() :::--- " + files);
if(files == null)
Toast.MakeText(this,"No photo",700).show();
else
for (int i = 0; i <files.length; i++)
{
File file = files[i];
Log.d(UploadActivity.LOG_TAG, "i<files.length() :::--- " + i);
Log.d("Count",file.getPath());
it.add (file.getPath());
}
Log.d(UploadActivity.LOG_TAG, "List <String> it :::--- " + it);
return it;
}