Hi,

I am creating service which will run in the background and contain a
blocking list  (implemented using ArrayList) which may be used later
by other activites.

My Problem:-


>From my activity Services Demo I pass data to my service using

dataIntent.putExtra("originator", x);

this is fine but when I try to store this data in my Service in an
ArrayList called Items I get an exception and I cannot figure out
why.

The problem is in my Service code MyService :

                if(items.contains(ORIG)){
                        Log.d(TAG, "Element already exists exiting ");
                } else {
                        Log.d(TAG, "Adding Element");
                    items.add(ORIG);

                }
Please help.

EXCEPTION
java.lang.RuntimeException: Unable to start service ...... with
intent

caused by: java.lang.NullPointerException

Best Regards,

Graham

CODE Eclipse

Activity -> ServicesDemo
Service -> MyService

ACTIVITY 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;
}
}
-----------------------------------<end ACTIVITY>


--Service MyService ---------
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);


        }

}

--------------end of service-----------

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to