带有CardView的RecyclerView中的setLayoutManager NullPointException

时间:2016-02-25 01:11:17

标签: javascript android xml nullpointerexception android-recyclerview

我尝试使用RecyclerView但是我得到了这个错误。该应用程序总是崩溃试图打开BuscarPachangaMenu。

ERROR:

 FATAL EXCEPTION: main
                                                     Process: com.example.developer.pachanya, PID: 2210
                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.developer.pachanya/com.example.developer.pachanya.BuscarPachangaMenu}: java.lang.NullPointerException
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                         at android.os.Looper.loop(Looper.java:136)
                                                         at android.app.ActivityThread.main(ActivityThread.java:5001)
                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                                                         at dalvik.system.NativeStart.main(Native Method)
                                                      Caused by: java.lang.NullPointerException
                                                         at com.example.developer.pachanya.BuscarPachangaMenu.onCreate(BuscarPachangaMenu.java:77)
                                                         at android.app.Activity.performCreate(Activity.java:5231)
                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                         at android.os.Looper.loop(Looper.java:136) 
                                                         at android.app.ActivityThread.main(ActivityThread.java:5001) 
                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
                                                         at dalvik.system.NativeStart.main(Native Method) 

cardXML:

<?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="match_parent"
    android:padding="16dp" >

    <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/info"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        card_view:cardCornerRadius="15dp"
        card_view:cardUseCompatPadding="true"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.developer.pachanya.BuscarPachangaMenu">

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

            <TextView
                android:id="@+id/nombre_pachanga"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="Pachanga"
                android:textSize="14sp"
                android:layout_alignStart="@+id/fechaB" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:id="@+id/textnombrepach"
                android:layout_alignStart="@+id/fecha_pach"
                android:layout_below="@+id/nombre_pachanga"
                android:layout_toStartOf="@+id/boton_masOpciones" />

            <TextView
                android:id="@+id/fecha_pach"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="Fecha"
                android:layout_alignBottom="@+id/fechaB"
                android:layout_marginStart="58dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:id="@+id/textfechapach"
                android:layout_below="@+id/fecha_pach"
                android:layout_alignStart="@+id/textnombrepach"
                android:layout_toStartOf="@+id/boton_masOpciones" />

            <ImageView
                android:id="@+id/boton_masOpciones"
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:src="@drawable/usuarios"
                android:layout_alignBottom="@+id/textView1numb"
                android:layout_toStartOf="@+id/textView1numb" />

            <ImageView
                android:id="@+id/fechaB"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:contentDescription="logo calendario"
                android:src="@android:drawable/ic_menu_my_calendar"
                android:layout_marginTop="55dp"
                android:layout_below="@+id/nombre_pachanga" />

            <EditText
                android:id="@+id/textView1numb"
                android:layout_width="50dp"
                android:layout_height="20dp"
                android:gravity="center"
                android:text="20"
                android:clickable="false"
                android:cursorVisible="false"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:inputType="number"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="38dp"
                android:layout_alignParentTop="true"
                android:layout_above="@+id/comartirB" />


            <ImageView
                android:layout_width="30dp"
                android:layout_height="wrap_content"
                android:id="@+id/comartirB"
                android:src="@drawable/compartir"
                android:layout_alignBottom="@+id/textfechapach"
                android:layout_toEndOf="@+id/boton_masOpciones" />

        </RelativeLayout>

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

</LinearLayout>

RecycleXML:

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />

</RelativeLayout>

BuscarPachangaMenu:

 public class BuscarPachangaMenu extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    ImageView retroceder, siguiente;
    Intent intent;
    Context context;
    private static RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    private static ArrayList<PachangaModel> data;
    private static RecyclerView recyclerView;

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

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());


        try {
            BufferedReader mostrarD = new BufferedReader(new InputStreamReader(openFileInput("pachangas.txt")));
            String[] nameArray = null;
            String[] numJArray = null;
            String[] fechArray = null;
            String[] lugArray = null;
            String[] desArray = null;
            String[] motArray = null;
            int i = 0;
            String nomb = mostrarD.readLine();
            while (nomb != null) {
                String numJ = mostrarD.readLine();
                String fech = mostrarD.readLine();
                String lug = mostrarD.readLine();
                String des = mostrarD.readLine();
                String mot = mostrarD.readLine();

                nameArray[i]=nomb;
                numJArray[i]=numJ;
                fechArray[i]=fech;
                lugArray[i]=lug;
                desArray[i]=des;
                motArray[i]=mot;

                nomb = mostrarD.readLine();
            }

            data = new ArrayList<PachangaModel>();
            for (int id = 0; id < nameArray.length; id++) {
                data.add(new PachangaModel(
                        nameArray[id],
                        numJArray[id],
                        fechArray[id],
                        lugArray[id],
                        desArray[id],
                        motArray[id]
                ));
            }
            adapter = new PachangaAdapter(data);
            recyclerView.setAdapter(adapter);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

适配器:

public class PachangaAdapter  extends RecyclerView.Adapter<PachangaAdapter.MyViewHolder> {

    private ArrayList<PachangaModel> dataSet;

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView textViewNomb;
        TextView textViewJugad;
        TextView textViewFech;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.textViewNomb = (TextView) itemView.findViewById(R.id.textnombrepach);
            this.textViewJugad = (TextView) itemView.findViewById(R.id.textView1numb);
            this.textViewFech = (TextView) itemView.findViewById(R.id.textfechapach);
        }
    }

    public PachangaAdapter(ArrayList<PachangaModel> data) {
        this.dataSet = data;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.content_buscar_pachanga_menu, parent, false);


        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

        TextView textViewNomb = holder.textViewNomb;
        TextView textViewJugad = holder.textViewJugad;
        TextView textViewFech = holder.textViewFech;

        textViewNomb.setText(dataSet.get(listPosition).getNomPach());
        textViewJugad.setText(dataSet.get(listPosition).getJug());
        textViewFech.setText(dataSet.get(listPosition).getFech());
    }

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

我怎么解决这个???我需要帮助。

spanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspanspan。对不起,这不要留下我的帖子:S

1 个答案:

答案 0 :(得分:0)

必须移动回收

<android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />

到活动xml。

将数组更改为Arraylist。

try {
            BufferedReader mostrarD = new BufferedReader(new InputStreamReader(openFileInput("pachangas.txt")));
            ArrayList<String> nameArray = new ArrayList<String>();
            ArrayList<String> numJArray = new ArrayList<String>();
            ArrayList<String> fechArray = new ArrayList<String>();
            ArrayList<String> lugArray = new ArrayList<String>();
            ArrayList<String> desArray = new ArrayList<String>();
            ArrayList<String> motArray = new ArrayList<String>();

            String nomb = mostrarD.readLine();
            while (nomb != null) {
                String numJ = mostrarD.readLine();
                String fech = mostrarD.readLine();
                String lug = mostrarD.readLine();
                String des = mostrarD.readLine();
                String mot = mostrarD.readLine();

                nameArray.add(nomb);
                numJArray.add(numJ);
                fechArray.add(fech);
                lugArray.add(lug);
                desArray.add(des);
                motArray.add(mot);

                nomb = mostrarD.readLine();

            }

            data = new ArrayList<PachangaModel>();
            for (int id = 0; id < nameArray.size(); id++) {
                data.add(new PachangaModel(
                        nameArray.get(id),
                        numJArray.get(id),
                        fechArray.get(id),
                        lugArray.get(id),
                        desArray.get(id),
                        motArray.get(id)
                ));
            }

            adapter = new PachangaAdapter(data);
            rv.setAdapter(adapter);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

这就是我希望能帮助大家的一切。