所以我刚开始用android开发,在开发网站上一步步实现示例代码。
前几个工作正常,但我坚持意图。
我已经浏览了代码及其与开发人员网站上的代码完全相同的副本。那么为什么当活动在 startActivity(intent) 发生变化时它会崩溃?
帮助 !
这是第一个活动:::
package com.example.Trial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
public final static String EXTRA_MESSAGE = "com.example.myapp.MESSAGE";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisaplyMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
这是它调用的活动:
package com.example.Trial;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class DisaplyMessageActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(TrialActivity.EXTRA_MESSAGE);
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="vertical" >
<EditText
android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:hint="@string/edit_message" />
<Button
android:onClick="sendMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
这是 string.xml:::
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
</resources>
致命例外追踪到:
06-18 21:30:51.377: E/AndroidRuntime(341): 在 com.example.Trial.TrialActivity.sendMessage(TrialActivity.java:23)
多谢