我正在尝试学习Android做一些简单的项目,现在我不知道如何解决此问题:
我有一个具有ListView的主要活动。在我的onclick函数中,我将第二个活动发送到一个名为"名称"的参数。我捕获此参数,然后尝试填充我的imageview和textView。
这是我的sencond活动的代码:
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"。
预先感谢。
使用 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);
在您的布局xml文件中:删除此(app:srccompat ="@mipmap/ic_launcher"(和(android:text =" textView"(,分别从您的ImageView和TextView中
尝试以获取传递的字符串束
String padre = getIntent().getExtras().getString("name");
让我知道