Android中从活动实例化IntentService时出现Java空指针异常
我正在创建一个应用程序,其中包含一个活动和一个将从此活动调用的意图服务。 Intent服务的目的是将文件从android设备发送到远程服务器。该文件由应用程序创建 问题是:当我试图从活动中创建的方法实例化意图服务时,我得到了一个空指针异常 这是Eclipse显示的错误:Android中从活动实例化IntentService时出现Java空指针异常,java,android,buffer-overflow,Java,Android,Buffer Overflow,我正在创建一个应用程序,其中包含一个活动和一个将从此活动调用的意图服务。 Intent服务的目的是将文件从android设备发送到远程服务器。该文件由应用程序创建 问题是:当我试图从活动中创建的方法实例化意图服务时,我得到了一个空指针异常 这是Eclipse显示的错误: 08-07 03:09:18.720: E/AndroidRuntime(1182): FATAL EXCEPTION: main 08-07 03:09:18.720: E/AndroidRuntime(1182): Proc
08-07 03:09:18.720: E/AndroidRuntime(1182): FATAL EXCEPTION: main
08-07 03:09:18.720: E/AndroidRuntime(1182): Process: com.viruni.authentication, PID: 1182
08-07 03:09:18.720: E/AndroidRuntime(1182): java.lang.RuntimeException: Unable to instantiate service com.viruni.authentication.DataUploader: java.lang.NullPointerException
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.app.ActivityThread.access$1800(ActivityThread.java:135)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.os.Handler.dispatchMessage(Handler.java:102)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.os.Looper.loop(Looper.java:136)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-07 03:09:18.720: E/AndroidRuntime(1182): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 03:09:18.720: E/AndroidRuntime(1182): at java.lang.reflect.Method.invoke(Method.java:515)
08-07 03:09:18.720: E/AndroidRuntime(1182): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-07 03:09:18.720: E/AndroidRuntime(1182): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-07 03:09:18.720: E/AndroidRuntime(1182): at dalvik.system.NativeStart.main(Native Method)
08-07 03:09:18.720: E/AndroidRuntime(1182): Caused by: java.lang.NullPointerException
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.content.ContextWrapper.getContentResolver(ContextWrapper.java:99)
08-07 03:09:18.720: E/AndroidRuntime(1182): at com.viruni.authentication.DataUploader.<init>(DataUploader.java:25)
08-07 03:09:18.720: E/AndroidRuntime(1182): at java.lang.Class.newInstanceImpl(Native Method)
08-07 03:09:18.720: E/AndroidRuntime(1182): at java.lang.Class.newInstance(Class.java:1208)
08-07 03:09:18.720: E/AndroidRuntime(1182): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
08-07 03:09:18.720: E/AndroidRuntime(1182): ... 10 more
调用启动意向服务的代码是:
Intent iservice = new Intent(Viewer1.this, DataUploader.class);
startService(iservice);
Viewer1是我要从中调用服务的活动。DataUploader是我试图启动的Intent服务
有人能帮我调试这个错误吗。我确信活动的任何其他部分都没有错误
注意:我正在从我创建的方法调用startService()方法
这是我的数据上传器类:
public class DataUploader extends IntentService{
String encoded_string;
String android_id;
public DataUploader() {
super("Empty Constructor");
android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
}
public DataUploader(String name) {
super("DataUploader");
}
@Override
protected void onHandleIntent(Intent intent) {
String filename = intent.getStringExtra("filename");
Log.d("Testing","File name : " + filename);
//doFileUpload();
}
public int doFileUpload() {
int serverResponseCode = 0;
String uploadServerUri = "my_URL string";
Date d = new Date();
d.getTime();
File file = new File(path to the file, "r");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uploadServerUri);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
Log.i("FileUpload", "FileUpload: Time : " + d.getTime());
return 0;
}
}
做一些小的修正 Intent-iservice=newintent(Viewer1.this,DataUploader.class);
这是startService(iservice) 发布您的DataUploader服务代码…同时发布您的完整stacktrace。这里有一个嵌套的“起因”异常。我更喜欢以文字而不是图片的形式发布。我已经试过了。但是没有改变!出现了相同的错误:(
public class DataUploader extends IntentService{
String encoded_string;
String android_id;
public DataUploader() {
super("Empty Constructor");
android_id = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
}
public DataUploader(String name) {
super("DataUploader");
}
@Override
protected void onHandleIntent(Intent intent) {
String filename = intent.getStringExtra("filename");
Log.d("Testing","File name : " + filename);
//doFileUpload();
}
public int doFileUpload() {
int serverResponseCode = 0;
String uploadServerUri = "my_URL string";
Date d = new Date();
d.getTime();
File file = new File(path to the file, "r");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uploadServerUri);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}
Log.i("FileUpload", "FileUpload: Time : " + d.getTime());
return 0;
}
}