0

The AVD says "Unfortunately, Service has stopped", where service is the name of my app. Please tell me where I went wrong. I have posted the code of the two files :

MainActivity.java:

public class MainActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button start, stop;

        start = (Button) findViewById(R.id.Button1);
        stop = (Button) findViewById(R.id.Button2);

        start.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

        Intent service = new Intent(MainActivity.this, MyService.class);

        startService(service);

        }

        });

        stop.setOnClickListener(new View.OnClickListener() {

        @Override

          public void onClick(View v) {

        Intent name = new Intent(MainActivity.this, MyService.class);

        stopService(name);

        }

        });

        }

}

MyService.java:

public class MyService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
            return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();
        return 0;
    }

    @Override
    public void onDestroy() {

        super.onDestroy();

    }

}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:text="Play" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/Button1"
        android:layout_alignBottom="@+id/Button1"
        android:layout_centerHorizontal="true"
        android:text="Stop" />

</RelativeLayout>

This is what LogCat says:

04-18 18:48:48.724: E/Trace(852): error opening trace file: No such file or directory (2)
04-18 18:48:49.683: D/gralloc_goldfish(852): Emulator without GPU emulation detected.

The error message is displayed after I click on the start button.

Thanks in advance :)

wangyif2
  • 2,843
  • 2
  • 24
  • 29
Yash
  • 29
  • 7
  • Can you post the error you see in Logcat to you question as well? – petey Apr 18 '13 at 18:45
  • Have added this in manifest file? – Raghunandan Apr 18 '13 at 18:51
  • Posted it in the question. – Yash Apr 18 '13 at 18:51
  • http://stackoverflow.com/questions/11446049/error-opening-trace-file-no-such-file-or-directory-2 – Raghunandan Apr 18 '13 at 18:52
  • @Raghunandan yes I've added that in the manifest file. – Yash Apr 18 '13 at 18:55
  • So are you saying I should re-install eclipse? – Yash Apr 18 '13 at 18:56
  • thats the accepted answer in the link i provided. – Raghunandan Apr 18 '13 at 18:57
  • Assuming that you have got the error just by launching the app(even before clicking start\stop button). In this case only these below lines of code would have got executed, – Rahul Sundar Apr 18 '13 at 18:59
  • Sorry, I forgot to mention that. I get the error when I click on the start button. – Yash Apr 18 '13 at 19:01
  • kindly post the xml activity_main.xml here,and also state have you tried to access any file with the media player object? – Sanghita Apr 18 '13 at 19:03
  • Posted. No I was trying to use it earlier, not of any use now. – Yash Apr 18 '13 at 19:07
  • 1
    If I read correctly 'AlertDialogs should not be created from Service'. To achieve this Service can issue Notifications and Activity can subscribe to it and display AlertDialogs, whenever there is any notification. Even from design perspective, services should not be able to access the UI controls, their purpose is to achieve things by running in background. Also there are many threads which recommends the same .. http://stackoverflow.com/questions/7918571/how-to-display-a-dialog-from-a-service . But to be honest I am not really sure if services can display alert dialogs on their own :-( – Rahul Sundar Apr 18 '13 at 19:19

2 Answers2

1

I think your problem is here..

new AlertDialog.Builder(this).setTitle("Argh").setMessage("Watch out!").setNeutralButton("Close", null).show();

Just comment it once & try. It should work.

surender8388
  • 474
  • 3
  • 12
0

I don't think you can generate an AlertDialog in a service without any context to the activity. If you just want to know if the service is started or not, try logging it:

Log.i("MyService","Service started");

And check for it in you logcat by doing:

logcat -s "MyService"

If you want to display an AlertDialog, trying to look at this post.

Community
  • 1
  • 1
wangyif2
  • 2,843
  • 2
  • 24
  • 29