Android应用程序在ArrayList中插入元素时崩溃

时间:2018-07-03 11:23:54

标签: android arraylist android-recyclerview recyclerview-layout

我正在创建一个应用程序,单击该按钮时应在其中将一个项目添加到ArrayList中,但应用程序崩溃。调试后,我发现语句 TaskList.add(td); 导致应用程序崩溃。请提出我该如何解决?

下面提供了代码:

MainScreen.java:

public class MainScreen extends AppCompatActivity {


    View myView;

    public static TextView text_container;
    private RecyclerView list_container;
    private RecyclerView.Adapter list_container_adapter;
    private RecyclerView.LayoutManager list_container_layout_manager;
    private static ArrayList<TaskData> TaskList = new ArrayList<TaskData>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        myView = findViewById(R.id.my_view);
        list_container = (RecyclerView)findViewById(R.id.list_container);
        list_container.setHasFixedSize(true);
        list_container_layout_manager = new LinearLayoutManager(this);
        list_container.setLayoutManager(list_container_layout_manager);
        list_container_adapter = new ListContainerAdapter(MainScreen.this,TaskList);
        list_container.setAdapter(list_container_adapter);
        list_container.setItemAnimator(new DefaultItemAnimator());
        TaskData td = new TaskData("Dhrumil",15,8,1997,12,30,5,"Academics",true);
        TaskList.add(td);
    }

TaskData.java:

public class TaskData {
    public String task,type;
    public int date,month,year,hour,minute,priority;
    public boolean alert;  

/*Getters and Setters are included in original code but not here due to longer length*/

    public TaskData(String task, int date, int month, int year, int hour, int minute, int priority, String type, boolean alert){
        this.task = task;
        this.date = date;
        this.month = month;
        this.year = year;
        this.hour = hour;
        this.minute = minute;
        this.priority = priority;
        this.type = type;
        this.alert = alert;
    }

}

ListContainerAdapter.java:

public class ListContainerAdapter extends RecyclerView.Adapter<ListContainerAdapter.MyViewHolder> {
    private ArrayList<TaskData> TaskList;
    private String alertStatus;
    private Context context;
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView taskView,deadlineView,priorityView,typeView,alertStatusView;
        public MyViewHolder(View v) {
            super(v);
            taskView = (TextView) v.findViewById(R.id.taskView);
            deadlineView = (TextView) v.findViewById(R.id.deadlineView);
            priorityView = (TextView) v.findViewById(R.id.priorityView);
            typeView = (TextView) v.findViewById(R.id.typeView);
            alertStatusView = (TextView) v.findViewById(R.id.alertStatusView);
        }
    }

    public ListContainerAdapter(Context context, ArrayList<TaskData> TaskList) {
        this.context = context;
        this.TaskList = TaskList;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_container_element, parent, false);

        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        TaskData taskData = TaskList.get(position);
        holder.taskView.setText(taskData.task);
        String newDate = taskData.date+"/"+taskData.month+"/"+taskData.year+"\t"+taskData.hour+":"+taskData.minute;
        holder.deadlineView.setText(newDate);
        holder.priorityView.setText(taskData.priority);
        holder.typeView.setText(taskData.type);
        if(taskData.alert){
            alertStatus = "ON";
        }
        else{
            alertStatus = "OFF";
        }
        holder.alertStatusView.setText(alertStatus);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return TaskList.size();
    }



}

错误日志:

    --------- beginning of crash
07-03 17:04:13.675 2477-2477/com.bismuth.deadline E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.bismuth.deadline, PID: 2477
    android.content.res.Resources$NotFoundException: String resource ID #0x5
        at android.content.res.Resources.getText(Resources.java:335)
        at android.widget.TextView.setText(TextView.java:4555)
        at com.bismuth.deadline.ListContainerAdapter.onBindViewHolder(ListContainerAdapter.java:53)
        at com.bismuth.deadline.ListContainerAdapter.onBindViewHolder(ListContainerAdapter.java:13)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
        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:19857)
        at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
        at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        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:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6083)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
        at android.view.View.measure(View.java:19857)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2275)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1366)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1619)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
        at android.view.Choreographer.doCallbacks(Choreographer.java:686)
        at android.view.Choreographer.doFrame(Choreographer.java:621)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
07-03 17:04:13.677 1611-1625/system_process W/ActivityManager:   Force finishing activity com.bismuth.deadline/.MainScreen

3 个答案:

答案 0 :(得分:2)

  

android.content.res.Resources $ NotFoundException:字符串资源ID#0x5

您正在尝试通过Int方法设置setText()

当您尝试在Int方法中设置setText()时,会将int的值设为resource ID,并且没有String resource带有这样的{{1} } int文件中的值,因此它将抛出R.java

因此您需要将Resources$NotFoundException中的int转换为

使用此

string

代替这个

holder.priorityView.setText(String.valueOf(taskData.priority));

答案 1 :(得分:2)

由于您的Priority字段是int类型,因此您出错了。要将int设置为TextView,您必须将int转换为String

答案 2 :(得分:1)

首先,请尝试遵循Java中的命名约定。在您的代码中,您尝试更新适配器的方式是错误的。首先,您应该像这样创建适配器的实例,

ListContainerAdapter adapter = new ListContainerAdapter(MainScreen.this,TaskList);
recyclerView.adapter = adapter

然后在您的适配器代码中,创建如下所示的方法,

public void updateAdapter(TaskData data){
this.arrayList.add(data)
notifyDatasetChanged()
}

现在,每当需要更新适配器时,请致电以下

TaskData data = TaskData()
adapter.updateAdapter(data)

您完成了。