0

I am trying to make a new layout customizing the image of it, R.id.imageStep1. I am getting an error at layout.addView(guideLayout).

This is my java class:

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");
}

Why is guideLayout null if I initiated in:

guideLayout = (ViewGroup) findViewById(R.id.guide_layout);

Is this the correct method to add a new xml file with values customized?

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: Unable to start activity ComponentInfo{com.alertavecino.alertavecino/com.alertavecino.alertavecino.guide}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
lorena
  • 55
  • 3
  • Can you please add you XML file? use : image = (ImageView) findViewById(R.id.imageview_Main); instead of image = (ImageView) guideLayout.findViewById(R.id.imageview_Main); – fbwnd Nov 27 '16 at 15:57
  • You don't have R.id.guide_base in your XML. I'm not sure I understand what you are trying to do. You are using setContentView(R.layout.guide_base); Which I guess doesn't have guide_layout in it. In that case, it will not find it and will return null. Please explain what you are trying to do and I will try to suggest an answer. – fbwnd Nov 27 '16 at 16:02
  • I do have an guide_base.xml, let me post it so you can see it. – lorena Nov 27 '16 at 16:04
  • 1
    You need to inflate the xml. – K Neeraj Lal Nov 27 '16 at 16:10

2 Answers2

0

Try using LayoutInflater to call other view.

private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;
inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);
parent = (ViewGroup) findViewById(R.id.mainxml);


addbutton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
    view = inflater.inflate(R.layout.other, null);
    parent.addView(view);
 }
});

Try this. Hope this helps :)

UserDev
  • 3
  • 7
0

In setContentView(R.layout.guide_base); you are declaring the Layout of the activity. In your layout you have only guide_base which in that case

    ViewGroup guideLayout = (ViewGroup) findViewById(R.id.guide_layout);

will return null, as it doesn't exists in the guide_base layout. Why don't you include the guide_layout layout inside the guide_base?

In case you want to reuse it in other placse. you can edit your guide_base to:

<?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">
        <include layout="@layout/guide_layout"
          android:id="@+id/guide_layout"/>
</LinearLayout>

as described in: https://developer.android.com/training/improving-layouts/reusing-layouts.html

Or to use inflator to create the guide_layout view. see How to inflate one view with a layout

Community
  • 1
  • 1
fbwnd
  • 687
  • 6
  • 12
  • You can use @Malik Kawee answer in case you choose to inflate the view. – fbwnd Nov 27 '16 at 16:13
  • This lets me inflate it with a new layout but how can I change the attribute of the XML new layout? – lorena Nov 27 '16 at 16:32
  • After you inflate it, use ImageView image; image = (ImageView) findViewById(R.id.guideImage1); String guideImage = intent.getStringExtra("guideImage"); int resId = guideLayout.getResources().getIdentifier(guideImage, "drawable", null); image.setImageResource(resId); That should work. If you are inflating the view, Use image = (ImageView) layout.findViewById(R.id.guideImage1); instead. – fbwnd Nov 27 '16 at 16:57