android - TextView 不显示任何文本

标签 android textview

我有 2 个 TextView,但其中一个不显示任何文本。我在 LogCat 中看到它在那里,但在应用程序上它没有显示。我确定问题出在我的 xml 文件中,但我不知 Prop 体是什么。我在这里也看到了很多这样的帖子,我尝试了与他们不同的东西,但没有帮助我。这是布局

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent" >

<LinearLayout 
    android:layout_width="320dp"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_weight="10.18"
        android:background="@drawable/buttons"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/name"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:text="" />

        <TextView
            android:id="@+id/text"
            android:layout_width="280dp"
            android:layout_height="wrap_content"
            android:paddingBottom="10dp"
            android:paddingLeft="5dp"
            android:paddingTop="5dp"
            android:layout_marginTop="10dp"
            android:text="" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical" >

        <Button
            android:id="@+id/pressMe"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:background="@drawable/bg_button"
            android:gravity="center"
            android:padding="4dp"
            android:text="@string/PressMe" />

        <Button
            android:id="@+id/dontPress"
            android:layout_width="300dp"
            android:layout_height="40dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@drawable/bg_button"
            android:gravity="center"
            android:padding="4dp"
            android:text="@string/DontPressMe" />
    </LinearLayout>

</LinearLayout>

</ScrollView>

有问题的 TextView"@+id/name"

我还通过 putExtragetExtra 从另一个 Activity 中获取它们

intent.putExtra("text", stocks[position].text); 
intent.putExtra("name", stocks[position].name);

Bundle b = getIntent().getExtras();
    if (b != null) {
        name = b.getString("name");
        textView.setText(name+"");
        text = b.getString("text");
        textView.setText(text+"");

所以 text 在那里,但是 name 不见了。在第一个 Activity 中,两者都在那里。

    textView = (TextView) findViewById(R.id.name);
    textView = (TextView) findViewById(R.id.text);

最佳答案

你的Layout没有问题

但问题出在您的代码中,您必须在 oncreate 方法中找到这两个 textView。 你确实找到了两者,但问题是当你设置文本时,使用相同的文本引用

在这里你对两个 TextView 采用相同的引用。像这样改变它。

TextView txtName = (TextView)findviewbyid(R.id.name);
TextView txtText = (TextView)findviewbyid(R.id.text);

    Bundle b = getIntent().getExtras();
        if (b != null) {
            name = b.getString("name");
           //change here txtName.setText(name+"");
            text = b.getString("text");
            //change here txtText.setText(text+"");
}

关于android - TextView 不显示任何文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26927273/

相关文章:

java - AsyncTask #3 Android 在将数据发布到 tomcat 服务器时因 doInBackground 而崩溃

android - Android中的 9-patch 图像错误

android - 如果文本比 View 长,动态 TextView 不会选取框

Android:Textview 中的规则/水平线

ios - Swift 2.1 - 将信息输入到同时填充文本字段的 TextView 中?

java - 如何提供公共(public)的消息属性,仍然从 Web 项目内部访问它们?

android - listview根据选择android打开警报对话框

android - 半透明状态栏Android 4.4有偏移

Android显示html文本的最佳方式

Android::TextView::Programitic Creation with setSingleLine(false) - 不换行