Java 带工具栏的RecyclerView

Java 带工具栏的RecyclerView,java,android,android-layout,android-fragments,android-studio,Java,Android,Android Layout,Android Fragments,Android Studio,我对材料设计是新手,每次我尝试使用RecyclerView时,都会发现一切都错了 有人知道问题出在哪里吗 主要活动类 import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; public clas

我对材料设计是新手,每次我尝试使用RecyclerView时,都会发现一切都错了

有人知道问题出在哪里吗

主要活动类

 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;

 public class MainActivity extends ActionBarActivity {

    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar= (Toolbar) findViewById(R.id.toool);
        setSupportActionBar(toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
 }  
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.ImageView;

import android.widget.TextView;

import java.util.Collections;

import java.util.List;

public class MyRecyeAdapter extends RecyclerView.Adapter<MyRecyeAdapter.myholder> {

    List<info> data = Collections.emptyList();
    private LayoutInflater inflater;

    public MyRecyeAdapter(Context context, List<info> data) {

        inflater = LayoutInflater.from(context);
        this.data = data;

    }

    @Override
    public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom, parent, false);
        myholder holder = new myholder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(myholder holder, int position) {

        info current = data.get(position);

        holder.imageProfile.setImageResource(current.iconId);

        holder.UsersName.setText(current.Names);
    }

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

    public class myholder extends RecyclerView.ViewHolder {

        ImageView imageProfile;
        TextView UsersName;

        public myholder(View itemView) {

            super(itemView);
            imageProfile = (ImageView) itemView.findViewById(R.id.imageView);
            UsersName = (TextView) itemView.findViewById(R.id.textView);

        }
    }

}
主要活动布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <include
        android:id="@+id/toool"
        layout="@layout/app_tool" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.hothyfa.recyclerandmore.Recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toool"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/rere" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recye"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primarycolor" >  
    </android.support.v7.widget.Toolbar>
工具栏布局

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <include
        android:id="@+id/toool"
        layout="@layout/app_tool" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.hothyfa.recyclerandmore.Recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toool"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/rere" />

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recye"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primarycolor" >  
    </android.support.v7.widget.Toolbar>
并重新构建应用程序

错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment



                            at 


          app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)

                                at 
              \\\\\\\android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.hothyfa.recyclerandmore.MainActivity.onCreate(MainActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
    
尝试编辑此文件
尝试编辑此

好的,我在
回收商的
onCreateView
中找到了问题

recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye);
将其更改为:

recyclerView= (RecyclerView) view.findViewById(R.id.recye);

好的,我在
回收器的
onCreateView
中发现了这个问题

recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye);
将其更改为:

recyclerView= (RecyclerView) view.findViewById(R.id.recye);


试试这个:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

</FrameLayout>

请在任何问题中使用knw

尝试以下方法:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

</FrameLayout>


请在任何问题中执行lemme knw

无错误java.lang.RuntimeException:无法启动活动组件信息{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.MainActivity}:android.view.InflateException:二进制XML文件行#19:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)处膨胀类片段时出错java.lang.RuntimeException:无法启动activity ComponentInfo{com.example.hothifa.recyclerandmore/com.example.hothifa.recyclerandmore.MainActivity}:android.view.InflateException:二进制XML文件行#19:在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)处膨胀类片段时出错工具栏有自己的布局工具栏没有问题,但当我添加RecyclerView时,它会启动错误…我将添加工具栏布局并感谢您无论如何工具栏有自己的布局工具栏没有问题,但当我添加RecyclerView时,它会启动错误…我将添加工具栏布局并感谢您 anyway@HothyfaAl-khateb,我现在发现了问题,我更新了答案,请检查。它工作了,但没有Recyclerview,只有工具栏和此消息08-17 10:33:00.143 2431-2431/?E/Recyclerview﹕ 没有连接适配器;跳过布局它终于起作用了…谢谢谢谢谢谢谢谢谢谢:)谢谢you@HothyfaAl-khateb,我现在发现了问题,我更新了答案,请检查。它工作了,但没有回收器查看工具栏和此消息08-17 10:33:00.143 2431-2431/?E/Re自行车视图﹕ 没有连接适配器;跳过布局它终于起作用了…谢谢谢谢谢谢谢谢:)谢谢