获取java.lang.NullPointerException:尝试从空数组读取

时间:2018-08-09 10:20:02

标签: android android-recyclerview

我正在尝试在Fragment中创建RecyclerView并得到以下异常。 我已经尝试了一切,但无法找出我所缺少的 这是我的代码。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.code2hack.truckcalculator, PID: 15257
                  java.lang.NullPointerException: Attempt to read from null array
                      at android.widget.TableRow.measureNullChild(TableRow.java:183)
                      at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1127)
                      at android.widget.TableRow.onMeasure(TableRow.java:113)
                      at android.view.View.measure(View.java:23169)
                      at android.widget.TableRow.getColumnsWidths(TableRow.java:313)
                      at android.widget.TableLayout.findLargestCells(TableLayout.java:506)
                      at android.widget.TableLayout.measureVertical(TableLayout.java:471)
                      at android.widget.TableLayout.onMeasure(TableLayout.java:437)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
                      at android.view.View.measure(View.java:23169)
                      at android.support.v7.widget.RecyclerView$LayoutManager.measureChildWithMargins(RecyclerView.java:8994)
                      at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1585)
                      at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
                      at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
                      at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
                      at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3225)
                      at android.view.View.measure(View.java:23169)
                      at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
                      at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
                      at android.view.View.measure(View.java:23169)
                      at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212)
                      at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:739)
                      at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:91)
                      at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1361)
                      at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:809)
                      at android.view.View.measure(View.java:23169)
                      at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1535)
                      at android.widget.LinearLayout.measureVertical(LinearLayout.java:825)
                      at android.widget.LinearLayout.onMeasure(LinearLayout.java:704)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6749)
                      at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
E/AndroidRuntime:     at com.android.internal.policy.DecorView.onMeasure(DecorView.java:716)
                      at android.view.View.measure(View.java:23169)
                      at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2719)
                      at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1572)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1856)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7184)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:761)
                      at android.view.Choreographer.doFrame(Choreographer.java:696)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
                      at android.os.Handler.handleCallback(Handler.java:873)
                      at android.os.Handler.dispatchMessage(Handler.java:99)
                      at android.os.Looper.loop(Looper.java:193)
                      at android.app.ActivityThread.main(ActivityThread.java:6669)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的Fragment类

package com.code2hack.truckcalculator.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
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 com.code2hack.truckcalculator.R;
import com.code2hack.truckcalculator.adapter.MonthlyListAdapter;
import com.code2hack.truckcalculator.model.DayData;
import com.code2hack.truckcalculator.model.Expense;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

public class MonthlyRecordFragment  extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.monthly_record,container,false);
        setRecyclerView(view);
        return view;
    }

    public void setRecyclerView(View view){
        RecyclerView recyclerView = view.findViewById(R.id.dailylist);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        MonthlyListAdapter adapter = new MonthlyListAdapter(getActivity(),getList());
        recyclerView.setAdapter(adapter);
    }
    public List<DayData> getList(){
        List<DayData> dayData = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();

        for(int i=0;i<220;i++){
            DayData data = new DayData();
            data.setDate(calendar.getTime());
            calendar.add(Calendar.DATE,1);
            data.setFrom("kkkkkkkk");
            data.setTo("bbbbbbbb");
            List<Expense> expenses = new ArrayList<>();
            for(int j=0;j<4;j++){
                Expense expense = new Expense();
                expense.setName("Name " + (j+1));
                expense.setPrice(new Random().nextInt() * 2000);
                expenses.add(expense);
            }
            data.setData(expenses);
            dayData.add(data);
        }
        return dayData;
    }
}

还有我的适配器类

package com.code2hack.truckcalculator.adapter;

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.TableLayout;
import android.widget.TextView;

import com.code2hack.truckcalculator.R;
import com.code2hack.truckcalculator.model.DayData;
import com.code2hack.truckcalculator.model.Expense;

import java.text.SimpleDateFormat;
import java.util.List;

public class MonthlyListAdapter extends RecyclerView.Adapter<MonthlyListAdapter.DailyViewHolder> {
    private SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
    private List<DayData> data;
    LayoutInflater inflater;
    Context context;
    public MonthlyListAdapter(Context context, List<DayData> data){
        this.context = context;
        this.data = data;
        this.inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public DailyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.daily_data_item,parent,false);
        DailyViewHolder viewHolder = new DailyViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull DailyViewHolder holder, int position) {
        DayData record = data.get(position);
        holder.setData(record);
    }

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

    class DailyViewHolder extends RecyclerView.ViewHolder {
        private View view;
        public DailyViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
        }
        public void setData(DayData data){
            String date = dateFormat.format(data.getDate());
            String sourceDestination = data.getFrom() + " -> " + data.getTo();

            TextView dateView = view.findViewById(R.id.record_date);
            dateView.setText(date);

            TextView destination = view.findViewById(R.id.destination);
            destination.setText(sourceDestination);

            TableLayout table = view.findViewById(R.id.datatable);
            table.removeAllViews();

            for(Expense expense :data.getData()) {
                View row = inflater.inflate(R.layout.expence_display, table, false);

                TextView textView = row.findViewById(R.id.key);
                textView.setText(expense.getName());

                TextView valueTag = row.findViewById(R.id.value);
                valueTag.setText(String.valueOf(expense.getPrice()));
                table.addView(row);
            }

        }
    }
}

monthly_record.xml

<?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:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/dailylist">
    </android.support.v7.widget.RecyclerView>
</RelativeLayout>

我找不到我想念的东西。 任何帮助将不胜感激。任何人都请帮助我。

2 个答案:

答案 0 :(得分:1)

可能是这行:

for(int j=0;i<4;i++){

应为j<4j++

答案 1 :(得分:0)

我在布局中使用了嵌套的TabelRow,这就是为什么它会引发错误。

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableRow>
    <TextView
        android:id="@+id/key"
        android:layout_column="1"
        android:layout_weight="0.7"
        android:gravity="end"
        android:layout_marginRight="5dp"
        android:text="Simple Text : "/>
    <TextView
        android:id="@+id/value"
        android:text="4000000"
        android:layout_gravity="right"
        android:layout_width="match_parent"/>
    </TableRow>
</TableRow>

我删除了嵌套的TableRow,它工作正常。