Java 主线程上出现致命错误

Java 主线程上出现致命错误,java,android,Java,Android,当我试图访问一个特定函数zipIt()时,我收到了这个致命错误。 无论我从哪里尝试访问它,我总是收到这个错误。这个函数只是将一个文件夹拉入拉链,但程序甚至不进入这个函数 private static final File INPUT_FOLDER = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); private static final String

当我试图访问一个特定函数zipIt()时,我收到了这个致命错误。 无论我从哪里尝试访问它,我总是收到这个错误。这个函数只是将一个文件夹拉入拉链,但程序甚至不进入这个函数

private static final File INPUT_FOLDER = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);

    private static final String ZIPPED_FOLDER = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES).toString();

protected void zipIt(File inputFolder, String zipFilePath) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(zipFilePath);
            ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);

            String myname =inputFolder.toString();
            ZipEntry folderZipEntry = new ZipEntry(myname);

            zipOutputStream.putNextEntry(folderZipEntry);
            File[] contents = inputFolder.listFiles();
            for (File f : contents) {

                if (f.isFile())

                    zipFile(f, zipOutputStream);

            }


            zipOutputStream.closeEntry();

            zipOutputStream.close();


        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }
    }

    protected   void zipFile(File inputFile, ZipOutputStream zipOutputStream) {
        try {  // A ZipEntry represents a file entry in the zip archive

            // We name the ZipEntry after the original file's name
            ZipEntry zipEntry = new ZipEntry(inputFile.getName());
            zipOutputStream.putNextEntry(zipEntry);
            FileInputStream fileInputStream = new FileInputStream(inputFile);

            byte[] buf = new byte[1024];

            int bytesRead;
            // Read the input file by chucks of 1024 bytes

            // and write the read bytes to the zip stream

            while ((bytesRead = fileInputStream.read(buf)) > 0) {

                zipOutputStream.write(buf, 0, bytesRead);

            }
            // close ZipEntry to store the stream to the file

            zipOutputStream.closeEntry();
            System.out.println("Regular file :" + inputFile.getCanonicalPath() + " is zipped to archive :" + ZIPPED_FOLDER);
        } catch (IOException e) {

            e.printStackTrace();

        }


    }
Logcat显示如下:

 Process: com.test.shahid, PID: 14839
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3823)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at java.io.File.fixSlashes(File.java:185)
            at java.io.File.<init>(File.java:134)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
            at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
            at com.test.shahid.MainActivity.zipIt(MainActivity.java:170)
            at com.test.shahid.MainActivity.dispatchTakePictureIntent(MainActivity.java:490)
            at com.test.shahid.MainActivity.onClickPhoto(MainActivity.java:468)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3818)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5050)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1264)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1080)
            at dalvik.system.NativeStart.main(Native Method)
05-23 09:48:22.023  14839-14839/com.test.shahid I/Process﹕ Sending signal. PID: 14839 SIG: 9
这就是我调用这个函数的地方。我调用这个函数只是为了检查,否则我从按钮和其他几个函数调用了这个函数。但什么都不管用

  public void onSync(View v) throws JSONException, IOException {
        zipIt(INPUT_FOLDER, ZIPPED_FOLDER);
        if (!isConnected()) {
            AlertDialog.Builder mBuilder = new Builder(this);
            mBuilder.setMessage("Please Enable Wifi to use this service");
            mBuilder.setTitle("Enable WIFI");
            mBuilder.setCancelable(false);
            mBuilder.setPositiveButton("Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent i = new Intent(
                                    Settings.ACTION_WIFI_SETTINGS);
                            startActivity(i);
                        }
                    }
            );
            mBuilder.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // This code might cause problem so check when
                            // device is available
                            MainActivity.this.finish();
                        }
                    }
            );
            // create alert dialog
            AlertDialog alertDialog = mBuilder.create();

            // show it
            alertDialog.show();
            tvIsConnected.setText("You are NOT conncted");
//          Intent myIntent = new
//          Intent(Settings.ACTION_WIFI_SETTINGS);
//          startActivity(myIntent);

            return;
        }

        tvIsConnected.setBackgroundColor(0xFF00CC00);
        tvIsConnected.setText("You are conncted");
        handler.sendEmptyMessage(MSG_SYNC);
    }
您的舱单中是否有“WriteExternalStorage”权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

将您的呼叫发送到zipIt()。是否可以验证是否正在向方法中传递有效路径(非空)?请突出显示MainActivity:170,因为它是冲突行。@zozelfefelfo MainActivity at 170是函数zipit()的try块。Word try位于170。@Falkon抱歉,我不明白您的问题?这是传递到INPUT_文件夹=/storage/emulated/0/Pictures的路径是的,我确实有写入外部_存储权限。如果我取消了对该函数的调用,则表示“是”,则表示所有功能都能正常工作,并且程序会保存刚刚拍摄的照片。让我添加调用它的方法。INPUT_FOLDER=/storage/emulated/0/pictures在字符串路径中,尝试使用:Environment.getExternalStorageDirectory()+“/some_foulder/file.txt”
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Environment.getExternalStorageDirectory() + "/some_foulder/file.txt"