0

如果列表没有照片,如何显示 Toast,我正在制作一个应用程序,允许用户在其中拍摄照片并将该照片存储到自动创建的特定文件夹中,

每当我点击查看按钮时,如果用户已经拍摄了屏幕,那么它会在列表中向我显示这些照片,如果用户没有拍摄任何照片,那么就会出现异常,所以如果用户没有捕捉到任何照片,我想在这里显示一条 Toast 消息图。

日志猫:

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);                  
        }
    });

上传活动.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;
        }
4

1 回答 1

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;
    }
于 2013-07-22T07:03:19.507 回答