3

I'm building an application in which I use a service to create a background process. but I have a problem. I use an EditText where the user enters a URL, then I will use that url in the service for check the connection to a website every X minutes. But if the user closes the application, the background process crash because the value of the EditText became Null, and logcat give me null pointer exceptions. I pass the value from the activity to the service with an intent, MainActivity code below:

public void onClickReadWebPage(View v)
    {


        if(address.getText().toString().trim().length() == 0)
        {
            Toast.makeText(getApplicationContext(), "URL String empty.", Toast.LENGTH_LONG).show();
        }
        else
        {
            time = String.valueOf(address.getText());
            i=new Intent(MainActivity.this,MyService.class);
            p=PendingIntent.getService(MainActivity.this, 0, i, 0);
            i.putExtra("Address", time);                
            //start the service from here //MyService is your service class name
            startService(i);
        }

and this is the service code:

    @Override
    public void onStart(Intent intent, int startId) 
    {   
        Address = intent.getStringExtra("Address");
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(Address);

        if(report == "false")
        {
            //do my stuff
        }
        else
        {
            //do my stuff
        }
    }

Any suggestion?

Giacomo De Bacco
  • 723
  • 6
  • 26
  • is not your problem ( at the moment ) - but really looks wrong: if(report == "false") – ligi Aug 05 '14 at 08:45

1 Answers1

0

Just save the value of ediText in SharedPreferences and then retrieve them in your service

Community
  • 1
  • 1
nobalG
  • 4,544
  • 3
  • 34
  • 72