Android 第二个活动屏幕不显示';t显示secondscreen.xml中定义的布局

Android 第二个活动屏幕不显示';t显示secondscreen.xml中定义的布局,android,xml,android-layout,Android,Xml,Android Layout,我是新来的,对Android编程也不熟悉。因此,在此方面的任何帮助都将不胜感激 我正在运行一个Android应用程序(基本上是developer.Android.com中给出的示例)并对其进行定制,以便在顶部添加一个显示屏幕编号的导航栏。该应用程序的想法是让用户在第一个屏幕中输入消息,并在第二个屏幕中显示消息。我已经能够在第一个屏幕中设置一个显示(1/2)的导航栏,并且在第二个屏幕的xml文件中类似地定义了一个布局,该布局应该显示导航栏和其中的(2/2)。此外,我还添加了一个“停止”按钮,稍后我

我是新来的,对Android编程也不熟悉。因此,在此方面的任何帮助都将不胜感激

我正在运行一个Android应用程序(基本上是developer.Android.com中给出的示例)并对其进行定制,以便在顶部添加一个显示屏幕编号的导航栏。该应用程序的想法是让用户在第一个屏幕中输入消息,并在第二个屏幕中显示消息。我已经能够在第一个屏幕中设置一个显示(1/2)的导航栏,并且在第二个屏幕的xml文件中类似地定义了一个布局,该布局应该显示导航栏和其中的(2/2)。此外,我还添加了一个“停止”按钮,稍后我将使用它

然而,我面临的问题是-第二个屏幕只显示从用户收到的消息,而不是我想要的布局(导航栏)。我认为我已经正确地确定了这一问题的根本原因,即以下代码:

//Create the text view 
TextView textView = new TextView(this); 
textView.setTextSize(40); 
textView.setText(message); 

// Set the text view as the activity layout
setContentView(textView);
我想我在这里使用的setContentView()方法显示了我从第一个活动收到的消息。当我注释掉这一行时,我使用以下方法获得第二个屏幕的所需格式:

setContentView(R.layout.activity_second_screen);
然而,我无法完全理解为什么以及如何将这两件事结合在一起。当setContentView(textView)未被注释时,setContentView(R.layout..)似乎无法按预期工作,我想这是正常的

先谢谢你

编辑-谢谢你,CodeMagic这么及时的回复。在我尝试您的解决方案之前,下面是关于创建的完整步骤。我有一些处理“停止”按钮的代码,但还没有实现它。所以请忽略这一点

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_screen);//Change-11/10 6:00 PM


Button buttonStop=(Button)findByViewId(R.id.buttonStop);//Change-11/10 6:00 PM
buttonStop.setOnClickListener(stopListener);//Change-11/10 6:00 PM

Intent intent = getIntent();
String message = intent.getStringExtra(FirstActivity.EXTRA_MESSAGE);
setContentView(R.layout.activity_second_screen);

// Create the text view - this code prints the message typed in first
//screen and displays on second screen, ignoring the xml layout of second screen     
TextView textView = new TextView(this); 
textView.setTextSize(40); 
textView.setText(message); 

// Set the text view as the activity layout
setContentView(textView); //change-11/11-9:00 PM
//setContentView(R.layout.activity_second_screen); 
}

我认为您需要的是在运行前将
TextView
添加到
activity\u second\u screen.xml
。然后,在
onCreate()
中,您只需添加
消息
,因为即使未显示该消息,您似乎也能正确获得该消息。所以做些类似的事情

EditText et;

public void onCreate(Bundle bundle)
{
     super.onCreate(bundle);
     setContentView(R.layout.activity_second_screen);
     et = (EditText) findViewById(R.id.editTextID); // where editTextId is 
                                                    //the id you give the 
                                                   //EditText in your layout file
     et.setText(message); //this will be done after you get your message variable
                          // I assume through an Intent
}
那你就不需要了

TextView textView = new TextView(this); 
textView.setTextSize(40); 

当您每次多次调用
setContentView()
时,它会将
视图设置为
setContentView()
中的任何内容,并覆盖以前在
setContentView()

中调用的任何内容,您可以使用
intent.putExtra(键,值)从第一个屏幕发送消息
并通过使用
intent.getExtra(key)
在第二个屏幕中获取此消息。这将返回您在第一个活动中发送的值,并将获取此值并在第二个屏幕中显示您想要的任何位置

在第一个活动“下一步”按钮中使用以下代码

意向i=新意向(第一类、第二类); i、 putExtra(“键”、“值”); 星触觉(i)

在第二个活动onCreate方法中,编写下面的代码片段

Bundle extras = getIntent().getExtras();
String ans = extras.getString("key");

您能否发布第二个
活动的完整
onCreate()
?我想我知道问题是什么,但很难说出来,也看不到我的答案。我认为这就是问题所在。