次のエラーを使用してサーバーからファイルを束ねようとしてAsynchTask
います:
07-09 12:49:57.868: E/AndroidRuntime(11133): java.lang.ExceptionInInitializerError
07-09 12:49:57.868: E/AndroidRuntime(11133): Caused by: java.lang.NullPointerException
それは私をこの行に投げ込みます:
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, null, null);
何が問題なのかわからない..私のコードは
package com.soch.webservice;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import android.app.Service;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import com.soch.database.Database_Handler;
public class FileTransferService extends Service {
private static final int CORE_POOL_SIZE = 2;
private static final int MAXIMUM_POOL_SIZE = 5;
private static final int KEEP_ALIVE = 1;
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, null, null);
int FileNo = 0;
ArrayList<String> FileId = new ArrayList<String>();
ArrayList<String> ServerPath = new ArrayList<String>();
ArrayList<String> SynchPath = new ArrayList<String>();
ArrayList<String> FileNameList = new ArrayList<String>();
String FileTransferPath = "", SDcardPath = "";
@Override
public void onCreate() {
super.onCreate();
Log.d("Service Created", "Successfully");
FileId.clear();
ServerPath.clear();
SynchPath.clear();
FileNameList.clear();
SDcardPath = Environment.getExternalStorageDirectory().getPath() + "/";
// Get FileTransfer Link From The SD Card
......
// Getting A url From DB
.....
}// End onCreate
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("Service Started", "Successfully");
Log.d("fileSize",""+FileId.size());
if (FileId.size() > 0) {
ContentValues mContentValuesUpdatefileStatus = new ContentValues();
mContentValuesUpdatefileStatus.put("Status", 1);
while (FileNo < FileId.size()) {
try {
new DownloadFileFromURL().executeOnExecutor(THREAD_POOL_EXECUTOR,Integer.toString(FileNo));
} catch (Exception e) {
Log.e("File Error", " File id is " + FileId.get(FileNo) + " File path is " + ServerPath.get(FileNo) + " Synch Path is " + SynchPath.get(FileNo));
FileNo += 1;
e.printStackTrace();
}
}
}
Log.d("Service Finish", "Successfully");
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
int Count = 0;
int File_Completed = 0;
int FileNo;
String strServerPath,strSynchPath,strFileName;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
FileNo = Integer.parseInt(params[0]);
// Get File Path
strServerPath = ServerPath.get(FileNo).replaceAll(" ", "%20");
strSynchPath = SynchPath.get(FileNo);
strFileName = FileNameList.get(FileNo);
URLConnection mConnection = null;
Log.d("file", strSynchPath + strFileName);
File SynchPath = new File(strSynchPath);
if (!SynchPath.exists()) {
SynchPath.mkdirs();
}
try {
URL url = new URL(strServerPath);
mConnection = url.openConnection();
mConnection.connect();
// getting file length
int lenghtOfFile = mConnection.getContentLength();
// Output stream to write file
File outputFile = new File(SynchPath, strFileName);
FileOutputStream mFileOutputStream = new FileOutputStream(
outputFile);
InputStream mInputStream = url.openStream();
byte data[] = new byte[1024];
long File_Completed_Size = 0;
while ((Count = mInputStream.read(data)) != -1) {
File_Completed_Size += Count;
File_Completed = (int) ((File_Completed_Size * 100) / lenghtOfFile);
// writing data to file
mFileOutputStream.write(data, 0, Count);
}
// flushing output
mFileOutputStream.flush();
// closing streams
mFileOutputStream.close();
mInputStream.close();
} catch (IOException IO) {
Log.e("File Exception"," File id is " + FileId.get(FileNo) + " File path is " + strServerPath + " Synch Path is " + strSynchPath + "FileName " + strFileName);
FileNo += 1;
IO.printStackTrace();
} catch (Exception e) {
Log.e("File Exception"," File id is " + FileId.get(FileNo) + " File path is " + strServerPath + " Synch Path is " + strSynchPath + "FileName " + strFileName);
FileNo += 1;
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (File_Completed == 100) {
Log.d("File Downlode Status for " + SynchPath.get(FileNo), "" + File_Completed + "%");
FileNo += 1;
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
編集済み
private static final int CORE_POOL_SIZE = 2;
private static final int MAXIMUM_POOL_SIZE = 5;
private static final int KEEP_ALIVE = 1;
private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(3);
public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE,TimeUnit.SECONDS, sPoolWorkQueue);