Android中使用setText方法的Java NullPointerExcpetion错误

时间:2017-08-15 04:41:31

标签: java android xml nullpointerexception

我对Android开发很新。我正在尝试将卡片视图放在底部工作表中。底部工作表很好,但我找不到任何方法使卡片视图工作。

主要活动中没有错误,但无论如何我都会发布。

public class MainActivity extends AppCompatActivity {

    Button eventListButton;
    private BottomSheetBehavior mBottomsheetbehvior;
    RecyclerView rv;
    LinearLayoutManager llm;
    Context context;
    public List<Events> events;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        events = new ArrayList<Events>();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find Button
        eventListButton = (Button) findViewById(R.id.eventlistbutton);

        //Find bottom sheet
        android.support.v4.widget.NestedScrollView bottomsheet =
                (android.support.v4.widget.NestedScrollView) findViewById(R.id.bottomsheet);

        //Add bottomsheet behavior to view
        mBottomsheetbehvior = BottomSheetBehavior.from(bottomsheet);
        mBottomsheetbehvior.setHideable(true);
        mBottomsheetbehvior.setPeekHeight(400);
        mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_HIDDEN);
        eventListButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_EXPANDED) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                    eventListButton.setText("Hide");
                }
                else if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_COLLAPSED) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_HIDDEN);
                    eventListButton.setText("Event List Button");
                }
                else if(mBottomsheetbehvior.getState() == BottomSheetBehavior.STATE_HIDDEN) {
                    mBottomsheetbehvior.setState(BottomSheetBehavior.STATE_EXPANDED);
                    eventListButton.setText("Peek");
                }
            }
        });

        //Bottomsheet callbacks
        mBottomsheetbehvior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {
                if (newState == BottomSheetBehavior.STATE_EXPANDED) {
                    eventListButton.setText("Peek");
                }
                else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
                    eventListButton.setText("Hide");
                }
                else if (newState == BottomSheetBehavior.STATE_HIDDEN) {
                    eventListButton.setText("Event List Button");
                }
            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

        //Recyler View
        rv = (RecyclerView) findViewById(R.id.rv);
        rv.setHasFixedSize(true);

        llm = new LinearLayoutManager(context);

        String[] participants = {"Tamim","Chiharu","Azmain","Miyoshi"};

        events.add(new Events(1302719286,"U22 project",participants,new LatLng(37.652832,219.839478),"Saizeriya"));
        events.add(new Events(1502419296,"U22 project",participants,new LatLng(32.652832,19.839478),"Hiroo"));
        events.add(new Events(1901719266,"U22 project",participants,new LatLng(19.652832,39.839478),"Canada"));
        events.add(new Events(1204219286,"U22 project",participants,new LatLng(100.652832,13.839478),"Tokyo Monorail"));
        RVAdapter adapter = new RVAdapter(events);
        rv.setAdapter(adapter);
        rv.setLayoutManager(new LinearLayoutManager(context));
    }
}

这是RV适配器,这是最大的问题:

public class RVAdapter extends RecyclerView.Adapter<RVAdapter.EventViewHolder>{

    List<Events> events;
    Calculations calculations = new Calculations();
    RVAdapter(List<Events> events){
        this.events = events;
    }

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

    @Override
    public EventViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_view, viewGroup, false);
        EventViewHolder pvh = new EventViewHolder(v);
        return pvh;
    }
    @Override
    public void onBindViewHolder(EventViewHolder eventViewHolder, int i) {
        eventViewHolder.eventName.setText(events.get(i).getName());
        eventViewHolder.eventLocation.setText(events.get(i).getLocationName());
        eventViewHolder.eventDate.setText(calculations.UnixTimeConverter(events.get(i).getUnixTimeStamp())[0]);
        eventViewHolder.eventTime.setText(calculations.UnixTimeConverter(events.get(i).getUnixTimeStamp())[1]);
        eventViewHolder.eventParticipants.setText(calculations.ParticipantConcatenation(events.get(i).getParticipants()));

    }
    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    public static class EventViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView eventName;
        TextView eventTime;
        TextView eventLocation;
        TextView eventDate;
        TextView eventParticipants;


        EventViewHolder(View itemView) {
            super(itemView);
            cv = (CardView)itemView.findViewById(R.id.CardViewItem);
            eventName = (TextView)itemView.findViewById(R.id.event_name);
            eventTime = (TextView)itemView.findViewById(R.id.event_name);
            eventDate = (TextView)itemView.findViewById(R.id.event_date);
            eventLocation = (TextView)itemView.findViewById(R.id.event_location);

        }

    }

}

我认为XML没有任何问题,但这里是代码:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/bgLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:text="EventList Button"
                    android:id="@+id/eventlistbutton"
                    />

        </RelativeLayout>
    </ScrollView>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/bottomsheet"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="true"
        android:background="@color/colorPrimaryDark"
        app:layout_behavior="@string/bottom_sheet_behavior"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.RecyclerView
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/rv"
                >

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

        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>



</android.support.design.widget.CoordinatorLayout>

这是卡片视图:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/CardViewItem"
    >
    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@color/cardbackground"
            >
            <TextView
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="2"
                tools:text="Dick Museum Tour"
                android:textSize="18sp"
                android:padding="@dimen/cardmargin"
                android:textStyle="bold"
                android:textColor="@color/white"
                android:id="@+id/event_name"
                />
            <TextView
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:textSize="18dp"
                android:padding="@dimen/cardmargin"
                android:textStyle="bold"
                android:textColor="@color/white"
                tools:text="8/24"
                android:textAlignment="viewEnd"
                android:id="@+id/event_date"
                />

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/card_view_height"
            android:background="@color/cardbackground2"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                tools:text="Time:"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:gravity="center_vertical"
                android:paddingLeft="@dimen/cardmargin"
                android:id="@+id/event_time"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                tools:text="Location:"
                android:textColor="@color/white"
                android:textSize="18sp"
                android:gravity="center_vertical"
                android:paddingLeft="@dimen/cardmargin"
                android:clickable="true"
                android:id="@+id/event_location"
                />
            <HorizontalScrollView
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/event_participants"
                    android:orientation="horizontal"
                    >
                </LinearLayout>

            </HorizontalScrollView>

        </LinearLayout>

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

这是我正在处理的错误:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.android.gatheraround, PID: 15361
                                                                                  java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                      at com.example.android.gatheraround.RVAdapter.onBindViewHolder(RVAdapter.java:42)
                                                                                      at com.example.android.gatheraround.RVAdapter.onBindViewHolder(RVAdapter.java:17)
                                                                                      at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6400)
                                                                                      at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6433)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5377)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5640)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5482)
                                                                                      at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5478)
                                                                                      at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2215)
                                                                                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1542)
                                                                                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1502)
                                                                                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
                                                                                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3625)
                                                                                      at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3067)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                                      at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1083)
                                                                                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:615)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.support.v4.widget.NestedScrollView.measureChildWithMargins(NestedScrollView.java:1417)
                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                                      at android.support.v4.widget.NestedScrollView.onMeasure(NestedScrollView.java:482)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:713)
                                                                                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:785)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
                                                                                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
                                                                                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5535)
                                                                                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
                                                                                      at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2615)
                                                                                      at android.view.View.measure(View.java:17547)
                                                                                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
                                                                                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
                                                                                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
                                                                                    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:10
08-15 13:25:27.997 15361-15369/com.example.android.gatheraround W/art: Suspending all threads took: 10.661ms
08-15 13:30:25.011 15361-15361/com.example.android.gatheraround I/Process: Sending signal. PID: 15361 SIG: 9

我有另外两个类Events和People,它们只有getter和setter方法以及构造函数,没有什么比这更复杂的了,所以我没有在这里发布它。

0 个答案:

没有答案