Cardona
Cardona

Reputation: 17

Error when creating dynamic views

Im am trying to create a dynamic object with imageviews and textviews

First I created a XML file to see what parameters I need to have to produce what I need.

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/layout_new_event"
    android:layout_width="wrap_content"
    android:layout_height="70dp"
    android:layout_marginBottom="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="20dp"
    android:background="@color/eventColor"
    android:orientation="horizontal">

    <LinearLayout
        android:id="@+id/linearLayoutEventText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/LinearLayoutEventIndicator"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView_event_from"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5sp"
                android:layout_weight=".5"
                android:background="@color/backgroundColor3"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/arrow_up_white" />

            <ImageView
                android:id="@+id/imageView_event_to"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:background="@color/backgroundColor2"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/arrow_down_white" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

Below is the java code I am using to create the above when pressing a floating button. I know that maybe there is unnecessary hierarchy in the layout, but this is part of a larger but same layout. So before I settle this I cannot proceed.

public class tabTueActivity extends Fragment {

    LinearLayout layoutNewEvent, linearLayoutEventText, linearLayoutEventVoyage, linearLayoutEventIndicator, eventLayout;

    ImageView imageViewEventFrom, imageViewEventTo, imageViewSearch;

    TextView textViewEventFrom, textViewEventTo;

    FloatingActionButton createEvent;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.tab_tue_frag, container, false);

        return rootView;
    }

    @Override
    public void onViewCreated(View rootView, Bundle savedInstanceState) {
        super.onViewCreated(rootView, savedInstanceState);

        eventLayout = (LinearLayout) rootView.findViewById(R.id.event_layout);
        createEvent = (FloatingActionButton) rootView.findViewById(R.id.add_event_button);
        layoutNewEvent = (LinearLayout) rootView.findViewById(R.id.layout_new_event);
        linearLayoutEventText = (LinearLayout) rootView.findViewById(R.id.linearLayoutEventText);
        linearLayoutEventIndicator = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventIndicator);
        imageViewEventFrom = (ImageView) rootView.findViewById(R.id.imageView_event_from);
        imageViewEventTo = (ImageView) rootView.findViewById(R.id.imageView_event_to);
        linearLayoutEventVoyage = (LinearLayout) rootView.findViewById(R.id.LinearLayoutEventVoyage);
        textViewEventFrom = (TextView) rootView.findViewById(R.id.textView_event_from);
        textViewEventTo = (TextView) rootView.findViewById(R.id.textView_event_to);
        imageViewSearch = (ImageView) rootView.findViewById(R.id.imageView_search);

        final LinearLayout.LayoutParams layoutNewEventParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutNewEventParams.setMargins(40, 40, 40, 40);

        final LinearLayout.LayoutParams linearLayoutEventTextParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        final LinearLayout.LayoutParams linearLayoutEventIndicatorParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        final LinearLayout.LayoutParams imageViewEventFromParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        imageViewEventFromParams.setMargins(0,0,0,10);
        imageViewEventFromParams.weight = (float) 0.5;

        final LinearLayout.LayoutParams imageViewEventToParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        imageViewEventToParams.weight = (float) 0.5;


        createEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Log.e("EVENT BUTTON", "Create event button pressed.....");

                layoutNewEvent.getLayoutParams().height = 140;
                layoutNewEvent.setBackgroundResource(R.color.eventColor);
                layoutNewEvent.setOrientation(LinearLayout.HORIZONTAL);

                linearLayoutEventText.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });

                linearLayoutEventText.setOrientation(LinearLayout.HORIZONTAL);

                linearLayoutEventIndicator.setOrientation(LinearLayout.VERTICAL);

                imageViewEventFrom.setBackgroundResource(R.color.backgroundColor3);
                imageViewEventFrom.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageViewEventFrom.setImageResource(R.drawable.arrow_up_white);

                imageViewEventTo.setBackgroundResource(R.color.backgroundColor2);
                imageViewEventTo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageViewEventTo.setImageResource(R.drawable.arrow_down_white);

                linearLayoutEventIndicator.addView(imageViewEventTo, imageViewEventToParams);
                linearLayoutEventIndicator.addView(imageViewEventFrom, imageViewEventFromParams);
                linearLayoutEventText.addView(linearLayoutEventIndicator, linearLayoutEventTextParams);
                layoutNewEvent.addView(linearLayoutEventText, layoutNewEventParams);

            }
        });
    }
}

and the below is the error I'm having

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.LinearLayout.getLayoutParams()' on a null object reference

Upvotes: 0

Views: 33

Answers (1)

Doron Yakovlev Golani
Doron Yakovlev Golani

Reputation: 5480

The problem is probably in this line:

layoutNewEvent.getLayoutParams().height = 140;

Your reference to the the view layoutNewEvent is null and that is the cause for the NullPointerException when you are trying to invoke getLayoutParams().

To resolve this I suggest you double check (either by debugging or just by looking at the code) that you are actually have the view with id R.id.layout_new_event in your xml. I am not sure that the xml you are inflating tab_tue_frag.xml is actually the one you put in the question.

Upvotes: 1

Related Questions