【问题标题】:No data in activity's layout while using Layout Inflater and view使用 Layoutinflater 和视图时活动布局中没有数据
【发布时间】:2017-05-30 17:26:53
【问题描述】:

我正在尝试学习Android做一些简单的项目,现在我不知道如何解决这个问题:

我有一个拥有 ListView 的主要活动。在我的 onClick 函数中,我向我的第二个活动发送了一个名为“名称”的参数。我抓住了这个参数,并尝试填充我的 ImageView 和 TextView。

这是我的第二个活动 onCreate 方法的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String padre=getIntent().getStringExtra("name");

    View view = getLayoutInflater().inflate(R.layout.activity_ficha, null);
    TextView textView_ficha = (TextView) view.findViewById(R.id.DescripcionFicha);
    ImageView imageView_ficha = (ImageView)view.findViewById(R.id.FotoFicha);

    if(padre.equals("cibeles")){         
        imageView_ficha.setImageResource(R.drawable.cibeles);
        textView_ficha.setText("Cibeles");
    }
    if(padre.equals("bernabeu")){
        imageView_ficha.setImageResource( R.drawable.bernabeu);
        textView_ficha.setText("Bernabeu");
    }
    setContentView(R.layout.activity_ficha);
}

我的布局代码是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:baselineAligned="false">

    <ImageView
        android:id="@+id/FotoFicha"
        android:layout_width="374dp"
        android:layout_height="277dp"
        app:layout_constraintRight_toRightOf="parent"
        app:srcCompat="@mipmap/ic_launcher"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:id="@+id/DescripcionFicha"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/FotoFicha"
        android:text="TextView" />
</RelativeLayout>

问题是,为什么我的第二个活动是空的?我只看到 ic_launcher 的图像和文本“TextView”。

提前致谢。

【问题讨论】:

  • 打印这个对象 padre 并告诉我它的内容是什么!
  • 我认为它不符合任何条件
  • 我也这么认为...我们需要见教士!
  • 在调试模式下,我可以看到 padre 有“cibeles”,并且 if 语句工作正常。
  • 作为最佳实践,使用 setcontentview 而不是布局充气器,因为你在活动布局充气器用于片段和运行时视图附加检查这个你会得到一个想法developer.android.com/training/basics/firstapp/…

标签: java android android-studio textview oncreate


【解决方案1】:

使用 setContentView(view);

而不是 setContentView(R.layout.activity_ficha);

因为通过 setContentView(R.layout.activity_ficha); 您正在添加一个带有活动的空布局,但您的数据在 view 参考的子项中

View view = getLayoutInflater().inflate(R.layout.activity_ficha, null);
TextView textView_ficha = (TextView) view.findViewById(R.id.DescripcionFicha);
ImageView imageView_ficha = (ImageView)view.findViewById(R.id.FotoFicha);

【讨论】:

  • 很高兴能帮上忙,编码愉快
【解决方案2】:

在您的布局 xml 文件中: 分别从 ImageView 和 TextView 中删除此 (app:srcCompat="@mipmap/ic_launcher") 和 (android:text="TextView")

【讨论】:

  • 如果我尝试这个,我会得到一个空白屏幕
【解决方案3】:

试试这个来获取传递的字符串包

String padre = getIntent().getExtras().getString("name");

告诉我

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多