首页 > 解决方案 > RecyclerView 没有 LayoutManager android.support.v7.widget.RecyclerView

问题描述

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.liliyu.easyresumebuilder, PID: 11145
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.liliyu.easyresumebuilder/com.example.liliyu.easyresumebuilder.MainActivity}: android.view.InflateException: Binary XML file line #10: RecyclerView has no LayoutManager android.support.v7.widget.RecyclerView{df7f7c9 VFED..... ......I. 0,0-0,0 #7f070061 app:id/main_activity1}, adapter:null, layout:null, context:com.example.liliyu.easyresumebuilder.MainActivity@2857d33
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_activity1"
    tools:context="com.example.liliyu.easyresumebuilder.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/spacing_medium"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/user_name_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/spacing_small"
                android:layout_alignParentLeft="true"
                android:layout_toLeftOf="@+id/user_picture"
                android:gravity="center_vertical">

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="@dimen/caption_text_size"
                    tools:text="Your name" />

            </LinearLayout>

            <TextView
                android:id="@+id/email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_below="@+id/user_name_layout"
                android:layout_toLeftOf="@+id/user_picture"
                tools:text="Your email" />

        </RelativeLayout>                

    </LinearLayout>

</android.support.v7.widget.RecyclerView>

MainActivity.java

@SuppressWarnings("ConstantConditions")
public class MainActivity extends AppCompatActivity {

    private BasicInfo basicInfo;
    private RecyclerView recyclerView  = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadData();
        setupUI();
    }

    private void setupUI() {
        setContentView(R.layout.activity_main);
        recyclerView  = (RecyclerView) findViewById(R.id.main_activity1);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        setupBasicInfo();

    }

    private void setupBasicInfo() {
        ((TextView) findViewById(R.id.name)).setText(TextUtils.isEmpty(basicInfo.name)
                ? "Your name"
                : basicInfo.name);
        ((TextView) findViewById(R.id.email)).setText(TextUtils.isEmpty(basicInfo.email)
                ? "Your email"
                : basicInfo.email);

        ImageView userPicture = (ImageView) findViewById(R.id.user_picture);
        if (basicInfo.imageUri != null) {
            ImageUtils.loadImage(this, basicInfo.imageUri, userPicture);
        } else {
            userPicture.setImageResource(R.drawable.user_ghost);
        }

        findViewById(R.id.edit_basic_info).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, BasicInfoEditActivity.class);
                intent.putExtra(BasicInfoEditActivity.KEY_BASIC_INFO, basicInfo);
                startActivityForResult(intent, REQ_CODE_EDIT_BASIC_INFO);
            }
        });
    }

      private void loadData() {
       connectAndGetApiData();
       basicInfo = basicInfo == null ? new BasicInfo() : basicInfo;
      }


    public void connectAndGetApiData() {
        if (retrofit == null) {
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        ResumeService resumeService=retrofit.create(ResumeService.class);
        Call<Resume> call =resumeService.getResume();
        try {
            call.enqueue(new Callback<Resume>() {
                @Override
                public void onResponse(Call<Resume> call, Response<Resume> response) {
                    recyclerView.setAdapter(new ResumeAdapter(getApplicationContext(),response.body()));

                }

                @Override
                public void onFailure(Call<Resume> call, Throwable throwable) {
                    System.out.println(throwable.toString());
                }
            });
        }catch(Throwable e){
            e.printStackTrace();
        }

    }
}

标签: javaandroidandroid-recyclerview

解决方案


您不能将子项放在RecyclerViewXML 中的 a 中。看起来你目前拥有的就是你想要在你的ViewHolder. 这不是怎么做的。

创建一个新的布局 XML 文件并将包含在您的内容中的内容移动RecyclerView到该文件中。

   <?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main_activity1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">   

    //delete all elements 
    </android.support.v7.widget.RecyclerView

>

推荐阅读