当我尝试在Service onStart中使用ArrayList时,我收到以下异常。
有人可以帮忙吗?
由于 格雷厄姆
问题似乎在这里
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
data = intent.getExtras();
ORIG = data.getString("originator");
Toast.makeText(this, "My Service Started with passed in value " + ORIG, Toast.LENGTH_LONG).show();
if(items.contains(ORIG)){
Log.d(TAG, "Element already exists exiting ");
} else {
Log.d(TAG, "Adding Element");
items.add(ORIG);
}
这是LOGCAT中提出的异常
E/AndroidRuntime( 7496): FATAL EXCEPTION: main
E/AndroidRuntime( 7496): java.lang.RuntimeException: Unable to start service com .example.MyService@4052eed0 with Intent { cmp=com.example/.MyService (has extras) }: java.lang.NullPointerException
E/AndroidRuntime( 7496): at android.app.ActivityThread.handleServiceArgs( ActivityThread.java:2056)
E/AndroidRuntime( 7496): at android.app.ActivityThread.access$2800(Activi tyThread.java:117)
E/AndroidRuntime( 7496): at android.app.ActivityThread$H.handleMessage(Ac tivityThread.java:998)
E/AndroidRuntime( 7496): at android.os.Handler.dispatchMessage(Handler.ja va:99)
E/AndroidRuntime( 7496): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 7496): at android.app.ActivityThread.main(ActivityThrea d.java:3691)
E/AndroidRuntime( 7496): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 7496): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 7496): at com.android.internal.os.ZygoteInit$MethodAndA rgsCaller.run(ZygoteInit.java:847)
E/AndroidRuntime( 7496): at com.android.internal.os.ZygoteInit.main(Zygot eInit.java:605)
E/AndroidRuntime( 7496): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 7496): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 7496): at com.example.MyService.onStart(MyService.java:54)
E/AndroidRuntime( 7496): at android.app.Service.onStartCommand(Service.ja va:428)
E/AndroidRuntime( 7496): at android.app.ActivityThread.handleServiceArgs( ActivityThread.java:2043)
E/AndroidRuntime( 7496): ... 10 more
W/ActivityManager( 2696): Force finishing activity com.example/.ServicesDemo
E/ ( 2696): Dumpstate > /data/log/dumpstate_app_error
I/dumpstate( 7586): begin
W/ActivityManager( 2696): Activity pause timeout for HistoryRecord{408759b0 com. example/.ServicesDemo}
调用我的服务的活动
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class ServicesDemo extends Activity implements OnClickListener {
private static final String TAG = "ServicesDemo";
Button buttonStart, buttonStop;
EditText Input;
String x = ""; //test pass to my service
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
Input = (EditText) findViewById(R.id.INPUT);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
}
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonStart:
Log.d(TAG, "onClick: starting srvice");
Intent dataIntent = new Intent(ServicesDemo.this, MyService.class);
x = Input.getText().toString();
dataIntent.putExtra("originator", x);
startService(dataIntent);
break;
case R.id.buttonStop:
Log.d(TAG, "onClick: stopping srvice");
//implicit starting
stopService(new Intent(this, MyService.class));
break;
}
}
public static String getTag() {
return TAG;
}
}
我尝试使用的服务问题是onStart
package com.example;
import java.util.ArrayList;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MyService extends Service {
ArrayList<String> items;
public String ORIG = "";
private static final String TAG = "MyService";
public Bundle data = new Bundle();
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static String getTag() {
return TAG;
}
@Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
@Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Log.d(TAG, "onStart");
data = intent.getExtras();
ORIG = data.getString("originator");
Toast.makeText(this, "My Service Started with passed in value " + ORIG, Toast.LENGTH_LONG).show();
if(items.contains(ORIG)){
Log.d(TAG, "Element already exists exiting ");
} else {
Log.d(TAG, "Adding Element");
items.add(ORIG);
}
Thread initBkgdThread = new Thread(new Runnable() {
public void run() {
//print_result();
}
});
initBkgdThread.start();
}
public void print_result(String orig){
Log.d(TAG, "HELLO WORLD:" + orig);
}
}
答案 0 :(得分:1)
看起来像数组列表 的的ArrayList&LT;字符串&GT;项 未初始化,因此您在服务 MyService 的onCreate()方法中获得NullPointerException。
答案 1 :(得分:0)
我没有看到你初始化ArrayList。首先初始化数组然后使用它。其次,请记住活动的生命周期。