指定的子项已有父项。

时间:2018-02-19 18:10:22

标签: android

创建动态视图时出现问题,用户第一次按下浮动按钮时会显示布局,但第二次按下时会显示Logcat中的错误并且应用暂停。

代码:

InputStream in = ApplicationProperties.class.getClassLoader()
    .getResourceAsStream("application.properties");

JavaPropsMapper mapper = new JavaPropsMapper();

try {
  BasicPOJO myProperties =  mapper.readValue(in,
      BasicPOJO .class);

  LOGGER.debug("Loaded properties {}", myProperties); //myProperties.test is null here

} catch (IOException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
}

logcat的:

import android.os.Bundle;

import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;

import android.text.Layout;
import android.util.Log;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class tabTueActivity extends Fragment {

    int i;

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

        final LinearLayout layoutNewEvent = new LinearLayout(getActivity());
        final LinearLayout linearLayoutEventText = new LinearLayout(getActivity());
        final LinearLayout linearLayoutEventIndicator = new LinearLayout(getActivity());
        final LinearLayout linearLayoutEventVoyage = new LinearLayout(getActivity());
        final ImageView imageViewEventFrom = new ImageView(getActivity());
        final ImageView imageViewEventTo = new ImageView(getActivity());
        final ImageView imageViewSearch = new ImageView(getActivity());
        final TextView textViewEventTo = new TextView(getActivity());
        final TextView textViewEventFrom = new TextView(getActivity());

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

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

                i++;

                Log.e("EVENT BUTTON", "New event created.....");

                layoutNewEvent.setId(i);
                layoutNewEvent.setBackgroundColor(getResources().getColor(R.color.eventColor));
                layoutNewEvent.setOrientation(LinearLayout.HORIZONTAL);

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

                        Log.e("LAYOUT EVENT","Layout Event Pressed.....");

                    }
                });

                linearLayoutEventText.setOrientation(LinearLayout.HORIZONTAL);
                linearLayoutEventText.setLayoutParams(linearLayoutEventTextParams);

                linearLayoutEventIndicator.setOrientation(LinearLayout.VERTICAL);
                linearLayoutEventIndicator.setLayoutParams(linearLayoutEventIndicatorParams);

                imageViewEventFrom.setBackgroundColor(getResources().getColor(R.color.backgroundColor3));
                imageViewEventFrom.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageViewEventFrom.setImageResource(R.drawable.arrow_up_white);
                imageViewEventFrom.setLayoutParams(imageViewEventFromParams);

                imageViewEventTo.setBackgroundColor(getResources().getColor(R.color.backgroundColor2));
                imageViewEventTo.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageViewEventTo.setImageResource(R.drawable.arrow_down_white);
                imageViewEventTo.setLayoutParams(imageViewEventToParams);

                linearLayoutEventIndicator.addView(imageViewEventFrom);
                linearLayoutEventIndicator.addView(imageViewEventTo);
                linearLayoutEventText.addView(linearLayoutEventIndicator);
                layoutNewEvent.addView(linearLayoutEventText);
                eventLayout.addView(layoutNewEvent, layoutNewEventParams);
            }
        });
    }
}

我知道这类错误有多篇帖子,我试图实施任何内容,但我错过了一些内容。

2 个答案:

答案 0 :(得分:0)

您尝试添加已存在的视图。

 eventLayout.addView(layoutNewEvent, layoutNewEventParams);

你应该创建一个新视图

LinearLayout newView = new LinearLayout(context);

答案 1 :(得分:0)

当您尝试添加先前已添加并附加到父视图的视图的同一实例时,您将收到此错误。

因为它正如预期的那样为第一个工作。每次按下onClick时都必须创建新视图。因此,最好将以下代码行移至onClick并尝试

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

           final LinearLayout layoutNewEvent = new LinearLayout(getActivity());
            final LinearLayout linearLayoutEventText = new LinearLayout(getActivity());
            final LinearLayout linearLayoutEventIndicator = new LinearLayout(getActivity());
            final LinearLayout linearLayoutEventVoyage = new LinearLayout(getActivity());
            final ImageView imageViewEventFrom = new ImageView(getActivity());
            final ImageView imageViewEventTo = new ImageView(getActivity());
            final ImageView imageViewSearch = new ImageView(getActivity());
            final TextView textViewEventTo = new TextView(getActivity());
            final TextView textViewEventFrom = new TextView(getActivity());
            i++
            .
            .
            .