java - 主线程上的 fatal error

标签 java android

当我尝试访问一个特定函数 zipIt() 时,我收到此 fatal error 。 无论我尝试从哪里访问它,我总是收到此错误。这个函数只是压缩一个文件夹,但程序甚至不进入这个函数。

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

这是与该函数关联的函数和变量。

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

        }


    }

这是我调用这个函数的地方。我调用这个函数只是为了检查我是否从按钮和其他几个函数调用了这个函数。但没有任何效果。

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

最佳答案

NullPointerException

您的 list 中有“WriteExternalStorage”权限吗?

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

检查文件路径了吗?

Environment.getExternalStorageDirectory() + "/some_foulder/file.txt"

关于java - 主线程上的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23821452/

相关文章:

java - 在 tomcat docker 上部署 java REST 服务

Java 可见性和同步 - Thinking in Java 示例

java - ResourceResponse.setCharacterEncoding() 不起作用

android - 从支持的设备中排除 Nexus 7

java - 安卓 Pjsip : Audio Conference Call

ScrollView 中的 Android ListView

android - 如何刷新我的gridView?

java - IClasspathEntry 的绝对文件系统路径

java - 没有任何实现和变量的抽象类是有效的接口(interface)吗?

java - 使用 Firebase 数据库获取每个子项的最后一个子项