我正在尝试学习Android做一些简单的项目,现在我不知道如何解决这个问题:
我有一个主要的活动,谁有一个ListView。在我的onClick函数中,我向第二个活动发送一个名为"name“的参数。我捕捉到这个参数,并尝试填充我的ImageView和TextView。
这是我的sencond活动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“。
提前谢谢。
发布于 2017-05-30 17:31:50
使用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);发布于 2017-05-30 17:31:05
在您的布局xml文件中:从您的app:srcCompat=和TextView中分别删除它(android:text=“@mipmap/ic_launcher”)和(android:text="TextView")
发布于 2017-05-30 17:33:56
尝试这个以获得传递的字符串包
String padre = getIntent().getExtras().getString("name");让我知道
https://stackoverflow.com/questions/44268397
复制相似问题