Я получаю следующее исключение, когда пытаюсь использовать ArrayList в моей службе onStart.
Кто-нибудь может помочь?
Спасибо, graham
Кажется, проблема здесь
@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);
}
}