Android / Java错误:“java.lang.IllegalStateException:指定的子节点已经有父节点”

时间:2017-07-16 22:30:17

标签: java android

我正在建造一个简单的回收商,并试图测试我到目前为止所拥有的东西。 (Android的新手)我调查过它我认为错误在于我的“MainActivity.java”或适配器“MyAdapter.java”,但我无法解决它。任何输入都会很棒。我在logcat中收到此错误:

07-16 15:15:55.743 2997-2997/com.example.duncan.recyclerviewproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.duncan.recyclerviewproject, PID: 2997
                                                                                  java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
                                                                                      at android.view.ViewGroup.addViewInner(ViewGroup.java:3949)
                                                                                      at android.view.ViewGroup.addView(ViewGroup.java:3799)
                                                                                      at android.view.ViewGroup.addView(ViewGroup.java:3740)
                                                                                      at android.support.v7.widget.RecyclerView$5.addView(RecyclerView.java:727)
                                                                                      at android.support.v7.widget.ChildHelper.addView(ChildHelper.java:107)
                                                                                      at android.support.v7.widget.RecyclerView$LayoutManager.addViewInt(RecyclerView.java:7921)
                                                                                      at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7879)
                                                                                      at android.support.v7.widget.RecyclerView$LayoutManager.addView(RecyclerView.java:7867)
                                                                                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
                                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)
                                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3625)
                                                                                      at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3354)
                                                                                      at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3886)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1197)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:434)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
                                                                                      at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
                                                                                      at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                      at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                      at android.view.View.layout(View.java:16067)
                                                                                      at android.view.ViewGroup.layout(ViewGroup.java:5051)
                                                                                      at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2122)
                                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1879)
                                                                                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1090)
                                                                                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5982)
                                                                                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:781)
                                                                                      at android.view.Choreographer.doCallbacks(Choreographer.java:592)
                                                                                      at android.view.Choreographer.doFrame(Choreographer.java:560)
                                                                                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:767)
                                                                                      at android.os.Handler.handleCallback(Handler.java:739)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:135)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:5484)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:984)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)

我的MainActivity.java:

package com.example.duncan.recyclerviewproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {


    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;

    private List<ListItem> listItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        listItems = new ArrayList<>();

        for(int i = 0; i<=10; i++){
            ListItem listItem = new ListItem(
                    "heading " + (i+1),
                    "Lorem ipsum dummy text"
            );

            listItems.add(listItem);
        }

        adapter = new MyAdapter(listItems, this);

        recyclerView.setAdapter(adapter);
    }
}

MyAdapter.java:

package com.example.duncan.recyclerviewproject;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Duncan on 7/16/17.
 */

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private List<ListItem> listItems;
    private Context context;

    public MyAdapter(List<ListItem> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item, parent);
        return new ViewHolder(v);

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ListItem listItem = listItems.get(position);

        holder.textViewHead.setText(listItem.getHead());
        holder.textViewDesc.setText(listItem.getDesc());
    }

    @Override
    public int getItemCount() {
        return listItems.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        public TextView textViewHead;
        public TextView textViewDesc;

        public ViewHolder(View itemView) {
            super(itemView);

            textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
            textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);


 }
}

}

我创建了2个XML文件

list_items.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:padding="16dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textViewHead"
                android:text="Heading"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textViewDesc"
                android:text="Body"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </android.support.v7.widget.CardView>

</LinearLayout>

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context="com.example.duncan.recyclerviewproject.MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="784dp"
        android:layout_height="1127dp"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp"></android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:1)

尝试改变

View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent);

View v = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);