如何修复android模拟器错误:您的应用程序已停止工作

时间:2012-05-20 02:40:42

标签: android android-intent android-emulator

我刚刚开始研究Android应用程序开发,并且正在阅读其中一个教程。我按照所有说明直到最后,没有编译错误。但是当我启动模拟器并尝试使用我的应用程序时,它给了我以下错误:

您的应用已停止工作

有什么方法可以解决这个问题吗?

以下是所有文件的代码:

MyFirstApp.java

package enigma.tutorial.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MyFirstApp extends Activity {
public final static String EXTRA_MESSAGE = "enigma.tutorial.myfirstapp.MESSAGE";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

/**Called when the user selects the Send button*/
public void sendMessage(View view){
    //Do something in response to button
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
 }
}

DisplayMessageActivity.java

package enigma.tutorial.myfirstapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    //Get the message from the intent
    Intent intent = getIntent();
    String message = intent.getStringExtra(MyFirstApp.EXTRA_MESSAGE);

    //Create the text view
    TextView textView = new TextView(this);
    textView.setTextSize(40);
    textView.setText(message);

    setContentView(textView);
 }
}

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<EditText  android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message" />
<Button android:id="@+id/button_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="sendMessage" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您可能没有将此添加到您的清单文件中:

<activity
android:name="DisplayMessageActivity"
android:label="DisplayMessageActivity">
</activity>