【发布时间】: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