我正在尝试为我的应用程序制作一个简单的登录屏幕,并且我已经实现了所有功能,只需在 TextView
中显示从上一个 Activity 传递的信息即可。以下是 Activity 中的 onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
/* Get the message from the intent
Intent intent = getIntent();
int ID = intent.getIntExtra("USER_ID", -1);
*/
// Create the text view
TextView txt = (TextView) findViewById(R.id.txtMenu);
txt.setText("text"); // + Integer.toString(ID));
}
这是 xml fragment 的内容:
<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="com.rma.runner.MenuActivity$PlaceholderFragment" >
<TextView
android:id="@+id/txtMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
但是,当我使用开发人员教程中的这段代码时,它似乎可以工作,但我想以图形方式排列所有内容,然后动态更改 Activity 中的文本:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// 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(R.layout.activity_menu);
到
setContentView(R.layout.fragment_main);
关于Android TextView.setText 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24409093/