我刚刚从developer.android.com/training/basics开始研究android应用开发,
并且我已经按照所述构建了应用程序,但是当我单击“发送”按钮时,文本不会显示,并且出现错误:不幸的是,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!不管我输入什么。
最佳答案
在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();
}