我刚开始从developer.android.com/training/basics学习android app开发, 并且我已经构建了应用程序,但是当我单击“发送”按钮时,文本将不会显示,并且错误:不幸的是,FirstApp已停止工作。
以下是DisplayMessageActivity类:
package com.example.firstapp;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
//get the message from intent
Intent intent=getIntent();
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//create the text view
TextView textView = (TextView) findViewById(R.id.m);
textView.setTextSize(40);
textView.setText(message);
super.onCreate(savedInstanceState);
//set the text view as activity layout
setContentView(textView);
// Show the Up button in the action bar.
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
以下是activity_display_message.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=".DisplayMessageActivity" >
<TextView android:id="@+id/m"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
以前,activity_display_message中的TextView是这样的:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="@string/hello_world" />
这样在我按下Send按钮后,输出总是Hello World!不管我输入什么。
答案 0 :(得分:1)
在setContentView()
之前,findViewById()
方法返回 null
像这样改变你的onCreate()
方法......
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
//create the text view
TextView textView = (TextView) findViewById(R.id.m);
textView.setTextSize(40);
//get the message from intent
Intent intent=getIntent();
if(intent != null) {
String message=intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
if(message != null) {
textView.setText(message);
}
}
// Show the Up button in the action bar.
setupActionBar();
}
答案 1 :(得分:1)
请拥有“activity_display_message.xml”,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
请点击“activity_hello_world_main.xml”:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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: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" >
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_send"
android:onClick="sendMessage" />
</LinearLayout>
请像这样使用“DisplayMessageActivity.Java”:
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(HelloWorldMainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
请像这样:“HelloWorldMainActivity.java”:
public class HelloWorldMainActivity extends Activity {
public static final String EXTRA_MESSAGE = "com.example.helloworld.MESSAGE";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world_main);
}
public void sendMessage(View view) {
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);
}
}
让strings.xml看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Hello World !!!</string>
<string name="edit_message">Enter a message</string>
<string name="btn_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">DisplayMessageActivity</string>
<string name="hello_world">Hello world!</string>
</resources>