使用 Layout Inflater 和视图时 activity 的布局中没有数据
No data in activity's layout while using Layout Inflater and view
我正在努力学习Android做一些简单的项目,现在我不知道如何解决这个问题:
我有一个主 activity,他有一个 ListView。在我的 onClick 函数中,我向我的第二个 activity 发送了一个名为 "name" 的参数。我捕捉到了这个参数,并尝试填充我的 ImageView 和 TextView。
这是我的第二个 activity 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>
问题是,为什么我的第二个activity是空的?我只看到 ic_launcher 的图像和 "TextView" 的文本。
提前致谢。
在您的布局 xml 文件中:
分别从您的 ImageView 和 TextView 中删除此 (app:srcCompat="@mipmap/ic_launcher") 和 (android:text="TextView")
使用
setContentView(view);
而不是
setContentView(R.layout.activity_ficha);
因为通过 setContentView(R.layout.activity_ficha);
您添加了一个带有 activity 的空布局,但您的数据位于 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);
试试这个来获取传递的字符串包
String padre = getIntent().getExtras().getString("name");
让我知道
我正在努力学习Android做一些简单的项目,现在我不知道如何解决这个问题:
我有一个主 activity,他有一个 ListView。在我的 onClick 函数中,我向我的第二个 activity 发送了一个名为 "name" 的参数。我捕捉到了这个参数,并尝试填充我的 ImageView 和 TextView。
这是我的第二个 activity 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>
问题是,为什么我的第二个activity是空的?我只看到 ic_launcher 的图像和 "TextView" 的文本。
提前致谢。
在您的布局 xml 文件中: 分别从您的 ImageView 和 TextView 中删除此 (app:srcCompat="@mipmap/ic_launcher") 和 (android:text="TextView")
使用
setContentView(view);
而不是
setContentView(R.layout.activity_ficha);
因为通过 setContentView(R.layout.activity_ficha);
您添加了一个带有 activity 的空布局,但您的数据位于 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);
试试这个来获取传递的字符串包
String padre = getIntent().getExtras().getString("name");
让我知道