私はイメージ、R.id.imageStep1
をカスタマイズして新しいレイアウトを作ろうとしています。 layout.addView(guideLayout)
にエラーが表示されます。カスタネットの値xml(java)で新しいレイアウトを作成するには?
これは私のJavaクラスです:私が開始された場合
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.guide_base);
ViewGroup layout = (ViewGroup) findViewById(R.id.guide_base);
ViewGroup guideLayout = (ViewGroup) findViewById(R.id.guide_layout);
Intent intent = getIntent();
ImageView image;
image = (ImageView) findViewById(R.id.guideImage1);
String guideImage = intent.getStringExtra("guideImage");
int resId = guideLayout.getResources().getIdentifier(guideImage, "drawable", null);
image.setImageResource(resId);
layout.addView(guideLayout);
String guideTitle = intent.getStringExtra("guideTitle");
String guideText = intent.getStringExtra("guideText");
}
はなぜguideLayout
nullである:
guideLayout = (ViewGroup) findViewById(R.id.guide_layout);
これはカスタマイズされた値を持つ新しいXMLファイルを追加するための正しい方法であり、 ?
guide_base.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/guide_base">
</LinearLayout>
guide_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/guide_layout">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
app:srcCompat="@drawable/one_black_36dp"
android:layout_marginLeft="8dp"
android:id="@+id/imageStep1"
android:contentDescription="Steps to follow"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="6dp" />
<TextView
android:text="TextView:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/titleStep1"
android:textColor="@color/colorGreenPrimary"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:textSize="20sp"
android:layout_alignBottom="@+id/imageStep1"
android:layout_toRightOf="@+id/imageStep1"
android:layout_toEndOf="@+id/imageStep1"
android:textStyle="bold"/>
<TextView
android:id="@+id/textGuide1"
android:contentDescription="Guide Step 1"
android:layout_alignLeft="@+id/titleStep1"
android:layout_alignStart="@+id/titleStep1"
android:layout_below="@id/titleStep1"
android:layout_height="wrap_content"
android:textColor="@color/colorGreenSecondary"
android:textSize="16sp"
android:layout_width="330dp"
android:text="Si una persona está despierta pero menos alerta de lo usual, hágale una serie de preguntas sencillas, como: ¿Cuál es su nombre? ¿Qué día es? ¿Cuántos años tiene?" />
<ImageView
app:srcCompat="@drawable/persona_desmayada"
android:id="@+id/guideImage1"
android:scaleType="centerCrop"
android:contentDescription="zzz"
android:layout_height="125dp"
android:layout_width="145dp"
android:layout_marginTop="25dp"
android:layout_below="@+id/textGuide1"
android:layout_centerInParent="true" />
</RelativeLayout>
LogCat:
java.lang.RuntimeException:活性ComponentInfo {com.alertavecinoを開始できません。 alertavecino/com.alertavecino.alertavecino.guide}:java.lang.NullPointerException:仮想メソッド 'android.view.ViewGroup $を呼び出そうとしていますLayoutParams nullオブジェクトリファレンスのandroid.view.ViewLayoutParams() '
XMLファイルを追加できますか? 使用: イメージ=(ImageView)findViewById(R.id.imageview_Main); の代わりに イメージ=(ImageView)guideLayout.findViewById(R.id.imageview_Main); – fbwnd
XMLにR.id.guide_baseはありません。 私はあなたが何をしようとしているのか分かりません。 使用しています setContentView(R.layout.guide_base); 私はそれがguide_layoutを持っていないと思います。 その場合、それを検出せず、nullを返します。 あなたがしようとしていることを説明してください。私は答えを提案しようとします。 – fbwnd
私はguide_base.xmlを持っていますので、あなたがそれを見ることができるように投稿してください。 – lorena