2

我遵循了来自 android 开发者的教程(http://developer.android.com/training/basics/firstapp/starting-activity.html

这将显示编辑文本并创建一个新的文本视图,显示输入的文本但是。这是我的问题。

我不希望我的 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);

    }
}
4

1 回答 1

0

你在DisplayMessageActivity为 Activity 设置布局之前初始化 TextView 所以在 setContentView 之后在 onCreate 中初始化它

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... 
于 2013-06-04T20:44:10.947 回答