当我在AVD中运行我的Android应用程序时,它给了我一个错误

时间:2013-04-18 18:44:47

标签: android avd

AVD说“不幸的是,服务已停止”,其中服务是我的应用程序的名称。 请告诉我哪里出错了。我已经发布了两个文件的代码:

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>

这就是LogCat所说的:

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.

点击开始按钮后会显示错误消息。

提前致谢:)

2 个答案:

答案 0 :(得分:1)

我认为你的问题就在这里..

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

只需评论一次&amp;尝试。 它应该工作。

答案 1 :(得分:0)

我认为您无法在没有活动上下文的服务中生成AlertDialog。如果您只想知道服务是否已启动,请尝试记录它:

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

通过执行以下操作在logcat中检查它:

logcat -s "MyService"

如果您想显示AlertDialog,请尝试查看this帖子。

相关问题