创建动态视图时出错

时间:2018-02-15 17:37:31

标签: android

我正在尝试使用imageviews和textviews创建动态对象

首先,我创建了一个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"
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>

以下是我按下浮动按钮时用来创建上述代码的java代码。我知道布局中可能存在不必要的层次结构,但这是更大但相同布局的一部分。所以在我解决这个问题之前,我无法继续。

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

            }
        });
    }
}

以下是我有

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

1 个答案:

答案 0 :(得分:1)

问题可能出在这一行:

layoutNewEvent.getLayoutParams().height = 140;

您对视图layoutNewEvent的引用为空,当您尝试调用NullPointerException时,这是getLayoutParams()的原因。

要解决此问题,我建议您仔细检查(通过调试或仅查看代码)您实际上在xml中拥有ID为R.id.layout_new_event的视图。我不确定您正在膨胀的tab_tue_frag.xml xml实际上是您提出问题的那个。

相关问题