我按照Android开发人员的教程(http://developer.android.com/training/basics/firstapp/starting-activity.html)
显示编辑文本并创建一个新的文本视图,显示输入的文本但是。继承人我的问题。
我不希望我的textview成为我的布局,因为它遵循该指南。我创建了一个包含textview的新布局。我想在第一个活动类中将文本显示在我新创建的活动的textview中。香港专业教育学院尝试了一些方法,但继续得到fc
主要活动
package com.jembe.myfirstapp;
import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by ***** on 04/06/13.
*/
public class MainActivity extends Activity{
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/** Called when the user clicks the Send button */
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);
}
}
显示我的讯息活动
package com.jembe.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
TextView tv = (TextView) findViewById(R.id.textView);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the text view as the activity layout
setContentView(R.layout.view1);
// Get the message from the intent
Intent intent;
intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
//Display the message in textview
tv.setText(message);
}
}
答案 0 :(得分:0)
您在设置Activity的布局之前在DisplayMessageActivity
中初始化TextView,因此在setContentView
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the text view as the activity layout
setContentView(R.layout.view1);
tv = (TextView) findViewById(R.id.textView); //<<< initialize here
//.... your code here...