空对象引用上的'java.lang.Class java.lang.Object.getClass()'

时间:2018-06-23 07:01:58

标签: android

##我已经创建了带有两个标签的标签活动。     1. Registration_Tab.java,2. 2. Registration_Details.java与Main_Activity.java关联     现在,我在registration_details.xml中创建了Recycler View并为其创建了一个适配器。但是当我运行Main_Activity.java时,它给了我错误。 ##

Registration_Details.java

 import android.app.Application;
                import android.content.Context;
                import android.os.Bundle;
                import android.support.v4.app.Fragment;
                import android.support.v7.widget.LinearLayoutManager;
                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.ArrayList;
                import java.util.List;

                public class Registration_Details extends Fragment {

                    RecyclerView recyclerView;
                    List<String> list;
                    MyAddapter myAddapter;
                    Context context;
                    View rootView;
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                             Bundle savedInstanceState) {
                         rootView = inflater.inflate(R.layout.registration_details, container, false);

                        return rootView;
                    }

                    @Override
                    public void onActivityCreated(Bundle savedInstanceState) {
                        super.onActivityCreated(savedInstanceState);


                        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recycler_view);
                        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
                    enter code here
                        list = new ArrayList<>();

                        for (int i = 0; i < 10; i++) {
                            list.add(" Well Don..! You Have Done It Suyash");
                        }


                        myAddapter = new MyAddapter(this.getActivity(), list);
                        recyclerView.setAdapter(myAddapter);
                    }
                }

MyAddapter.java

   import android.content.Context;
import android.support.annotation.NonNull;
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;

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

    Context context;
    List<String> list;

    public MyAddapter(Context context, List<String> list){

        this.context = context;
        this.list = list;

    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row, viewGroup, false);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

        myViewHolder.textView.setText(list.get(i));

    }

    @Override
    public int getItemCount( ) {

        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        TextView textView;

       public MyViewHolder(@NonNull View itemView) {
           super(itemView);

           textView =(TextView)itemView.findViewById(R.id.tv);


       }
   }

}

给出错误

    06-23 11:54:42.149 20871-20871/? E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.example.suyash.shivrai_tech, PID: 20871
        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Class java.lang.Object.getClass()' on a null object reference
            at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:396)
            at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:391)
            at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:105)
            at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1010)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1158)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1092)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1622)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:733)
            at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:95)
            at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1536)
            at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:803)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6613)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
            at com.android.internal.policy.DecorView.onMeasure(DecorView.java:875)
            at android.view.View.measure(View.java:22112)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2426)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1508)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1821)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1396)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6773)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:974)
            at android.view.Choreographer.doCallbacks(Choreographer.java:786)
            at android.view.Choreographer.doFrame(Choreographer.java:721)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:960)
            at android.os.Handler.handleCallback(Handler.java:790)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6501)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

row.xml

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

    <TextView
        android:id="@+id/tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sample Row"
        android:textSize="18dp"
        android:padding="10dp"
        android:textColor="#0000"
        android:textStyle="bold"
        />


</android.support.constraint.ConstraintLayout>

2 个答案:

答案 0 :(得分:0)

以此替换您的代码

                import android.app.Application;
                import android.content.Context;
                import android.os.Bundle;
                import android.support.v4.app.Fragment;
                import android.support.v7.widget.LinearLayoutManager;
                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.ArrayList;
                import java.util.List;

                public class Registration_Details extends Fragment {

                    RecyclerView recyclerView;
                    List<String> list;
                    MyAddapter myAddapter;
                    Context context;
                    View rootView;
                    @Override
                    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                             Bundle savedInstanceState) {
                         rootView = inflater.inflate(R.layout.registration_details, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view);
                        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
                    enter code here
                        list = new ArrayList<>();

                        for (int i = 0; i < 10; i++) {
                            list.add(" Well Don..! You Have Done It Suyash");
                        }


                        myAddapter = new MyAddapter(this.getActivity(), list);
                        recyclerView.setAdapter(myAddapter);
                        return rootView;
                    }

                    @Override
                    public void onActivityCreated(Bundle savedInstanceState) {
                        super.onActivityCreated(savedInstanceState);

                    }
                }

答案 1 :(得分:0)

onCreateView方法中输入此代码,

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
myAddapter = new MyAddapter(list, this);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(myAddapter);

我认为这对您有帮助。