当我尝试访问一个特定的函数zipIt()时,我收到了这个致命的错误。 从我尝试访问它的位置无关紧要我总是收到此错误。此功能只是拉链文件夹,但程序甚至不进入此功能。
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);
}
答案 0 :(得分:0)
NullPointerException
您的清单中是否有“Write_External_Storage”权限?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您是否检查了文件路径?
Environment.getExternalStorageDirectory() + "/some_foulder/file.txt"