When I run my android app in the AVD it gives me an error -
the avd says "unfortunately, service has stopped", service name of app. please tell me went wrong. have posted code of 2 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 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 displayed after click on start button.
thanks in advance :)
i don't think can generate alertdialog in service without context activity. if want know if service started or not, try logging it:
log.i("myservice","service started"); and check in logcat doing:
logcat -s "myservice" if want display alertdialog, trying @ this post.
Comments
Post a Comment