在null对象上引用RecyclerView $ LayoutManager

时间:2018-01-09 11:15:00

标签: android methods android-recyclerview nullpointerexception fragment

ViewAllProducts

    package com.example.venkateshbm.myapplication;


    import android.app.Activity;
    import android.os.Bundle;
    import android.support.v7.widget.DefaultItemAnimator;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;

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

    /**
     * Created by VenkateshBM on 08/01/18.
     */

    public class ViewAllProducts extends Activity{

        DBHelper database;
        RecyclerView recyclerView;
        RecycleProductAdapter recycler;
        List<ProductDataModel> productDataModels;

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

            productDataModels =new ArrayList<ProductDataModel>();
            recyclerView = (RecyclerView) findViewById (R.id.recycler);

                    database = new DBHelper(ViewAllProducts.this);
                    productDataModels =  database.getdata();
                    recycler =new RecycleProductAdapter(productDataModels);


                    RecyclerView.LayoutManager reLayoutManager =new LinearLayoutManager(getApplicationContext());
                    recyclerView.setLayoutManager(reLayoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(recycler);


        }
    }

        BillingFragment Code
        package com.example.venkateshbm.myapplication;

    import android.content.Context;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.net.Uri;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.AutoCompleteTextView;
    import android.widget.Toast;

    public class BillingFragment extends Fragment {

        AutoCompleteTextView fracproductname;



        public BillingFragment() {
            // Required empty public constructor
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            DBHelper dbHelper = new DBHelper(getActivity());
            View rootview = inflater.inflate(R.layout.fragment_billing,container,false);
            fracproductname = (AutoCompleteTextView)rootview.findViewById(R.id.fr_ac_product_name);
            DBHelper mydb = new DBHelper(getActivity());
            SQLiteDatabase db = mydb.getReadableDatabase();
            String myproduct [] = mydb.SelectAllData(DBHelper.ITEM_TABLE,DBHelper.ITEM_NAME,DBHelper.ITEM_CODE);
            ArrayAdapter<String> aaprodut = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_dropdown_item_1line,myproduct);
            fracproductname.setAdapter(aaprodut);
            fracproductname.setThreshold(1);
            Intent intent = new Intent(getActivity(),ViewAllProducts.class);
            startActivity(intent);

            // Inflate the layout for this fragment


            fracproductname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (fracproductname.getText() == null || fracproductname.length()<=0 ){
                        fracproductname.setError("Invalid Selection Data");

                    }
                    else
                    {
                        Toast.makeText(getActivity(),"The Data Found",Toast.LENGTH_LONG).show();
                    }
                }
            });


            return rootview;


        }

        }

    DBHelper - getdata -
    public List<ProductDataModel> getdata(){
        // DataModel dataModel = new DataModel();
        List<ProductDataModel> data=new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("select * from item_mst " ,null);
        StringBuffer stringBuffer = new StringBuffer();
        ProductDataModel productDataModel = null;
        while (cursor.moveToNext()) {
            productDataModel= new ProductDataModel();
            String name = cursor.getString(cursor.getColumnIndexOrThrow("item_name"));
            //         String country = cursor.getString(cursor.getColumnIndexOrThrow("country"));
            //         String city = cursor.getString(cursor.getColumnIndexOrThrow("city"));
            productDataModel.setProduct_name(name);

            stringBuffer.append(productDataModel);
            // stringBuffer.append(dataModel);
            data.add(productDataModel);
        }

        for (ProductDataModel mo:data ) {

            Log.i("Hellomo",""+mo.getProduct_name());
        }

        //

        return data;
    }

应用程序崩溃 - 某些数据正在获取并显示在Logcat中

01-08 15:42:13.617 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: HIBRID 228.5MG DRY SYP
01-08 15:42:13.617 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: DEWSOFT PREMIA CREAM
01-08 15:42:13.617 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: KESH KING
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: HUMAN MIXTARD
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: NOSIKIND P NASAL DROPS
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: SAKUNTHALA HERBAL HAIR OIL
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: RUSHIKESH BRING HAIR OIL
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: ROOP NISARGA FACE GEL
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: ASCORIL PLUS TAB
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: COTTON CREPE BANDEG
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: GLIWILL M2
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: RAYPLEX FORT  (PSYCO)
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: TEST CROCIN TAB
01-08 15:42:13.618 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.619 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.620 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.621 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.622 3365-3365/com.example.venkateshbm.myapplication I/Hellomo: null
01-08 15:42:13.637 3365-3365/com.example.venkateshbm.myapplication D/AndroidRuntime: Shutting down VM                                                                                         
                                                                                                                                                                             beginning of crash

01-08 15:42:13.638 3365-3365/com.example.venkateshbm.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                     Process: com.example.venkateshbm.myapplication, PID: 3365
                                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.venkateshbm.myapplication/com.example.venkateshbm.myapplication.ViewAllProducts}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                         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:5254)
                                                                                         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: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
                                                                                         at com.example.venkateshbm.myapplication.ViewAllProducts.onCreate(ViewAllProducts.java:42)
                                                                                         at android.app.Activity.performCreate(Activity.java:5990)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                         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:5254) 
                                                                                         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) 

我正在附加recycler xml标签

<ScrollView 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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.venkateshbm.myapplication.BillingFragment">
    <LinearLayout
        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"
            app:cardMaxElevation="2dp"
            app:cardElevation="5dp">
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <AutoCompleteTextView
                    android:id="@+id/fr_ac_product_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:maxLength="60"
                    android:maxLines="1"
                    android:hint="Enter Patient Name"
                    />
            </android.support.design.widget.TextInputLayout>
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
                <android.support.v7.widget.RecyclerView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="50dp"
                    android:id="@+id/recycler">
                </android.support.v7.widget.RecyclerView>
            </android.support.design.widget.TextInputLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

  

recyclerView =(RecyclerView)findViewById(R.id.recycler);

您的应用崩溃的原因是因为对象&#39; recyclerView&#39;是空的。

您确定&#39;回收商&#39;是recyclerView的正确XML标记吗?