Android TextView.setText 崩溃

标签 android crash textview

我正在尝试为我的应用程序制作一个简单的登录屏幕,并且我已经实现了所有功能,只需在 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/

相关文章:

java - 自上次重启以来的时间 : how can i convert?

Android,如何将 ArrayList<myObject> 放入 Intent 中?

android - 播放新歌曲时收到IllegalStateException

C++ 类 Matrix,崩溃

crash - NSTableView 在添加到 SplitView 的自定义 View 中崩溃

java - 尝试构建 gradle 时出错 - androidx.appcompat :appcompat:28. +

iphone - UINavigationController setViewControllers 导致应用程序崩溃

java - 安卓/Java : Set textview with char from index of string

java - 如何将文字大小设置为双倍大小

ios - 使用 NSAttributedString 为 UITextView 中的文本着色真的很慢