问题是当我将示例列表设置为空时,空指针会插入。我想要的只是在空的recyclerview中启动我的应用程序,然后单击按钮添加到列表时,我将添加和添加项目。
我尝试了以下代码:
@Override
public int getItemCount() {
if (mExampleList == null)
return 0;
else
return mExampleList.size();
}
但是当我添加项目时出现错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.ArrayList.add(int, java.lang.Object)' on a null object reference
at com.example.admin.test3.MainScreen.insertItem(MainScreen.java:63)
at com.example.admin.test3.MainScreen$1.onClick(MainScreen.java:44)
MainScreen Java代码
package com.example.admin.test3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;
public class MainScreen extends AppCompatActivity {
private ArrayList<ExampleItem> mExampleList;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private Button buttonInsert;
private Button buttonRemove;
private EditText textAmount;
private EditText textDetail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
// createExampleList();
buildRecyclerView();
buttonInsert = findViewById(R.id.buttonInsert);
buttonRemove = findViewById(R.id.buttonRemove);
textAmount = findViewById(R.id.textInsert);
textDetail = findViewById(R.id.textRemove);
buttonInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
insertItem();
}
});
buttonRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// int position = Integer.parseInt(textRemove.getText().toString());
removeItem();
}
});
}
public void insertItem()
{
String amount = String.valueOf(textAmount.getText());
String details = String.valueOf((textDetail.getText()));
mExampleList.add(0, new ExampleItem(R.drawable.entertainment, "Entertainment", details, "₱ " + amount));
mAdapter.notifyDataSetChanged();
}
public void removeItem()
{
//mExampleList.remove(position);
// mAdapter.notifyItemRemoved(position);
}
public void createExampleList()
{
mExampleList = new ArrayList <>();
mExampleList.add(new ExampleItem(R.drawable.ic_library_add_black_24dp, "", "", "ADD EXPENSES"));
}
public void buildRecyclerView()
{
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new ExampleAdapter(mExampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
}
}
回收站视图Java代码的示例适配器
package com.example.admin.test3;
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.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class ExampleAdapter extends RecyclerView.Adapter<ExampleAdapter.ExampleViewHolder> {
private ArrayList<ExampleItem> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder
{
public ImageView mImageView;
public TextView mLabel;
public TextView mDetails;
public TextView mAmount;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageIcon);
mLabel = itemView.findViewById(R.id.textLabel);
mDetails = itemView.findViewById(R.id.textDetails);
mAmount = itemView.findViewById(R.id.textAmount);
}
}
public ExampleAdapter(ArrayList<ExampleItem> exampleList)
{
mExampleList = exampleList;
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(view);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
ExampleItem currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getmImageResource());
holder.mLabel.setText(currentItem.getMlabel());
holder.mDetails.setText(currentItem.getMdetails());
holder.mAmount.setText(currentItem.getMamount());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
示例项目Java代码
package com.example.admin.test3;
public class ExampleItem {
private int mImageResource;
private String mlabel;
private String mdetails;
private String mamount;
public ExampleItem(int imageResource, String label, String details, String amount)
{
mImageResource = imageResource;
mlabel = label;
mdetails = details;
mamount = amount;
}
public int getmImageResource()
{
return mImageResource;
}
public String getMlabel() {
return mlabel;
}
public String getMdetails() {
return mdetails;
}
public String getMamount() {
return mamount;
}
}
主屏幕XML代码
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#77dd77" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="Pocket"
android:textColor="#b4ecb4"
android:textSize="18dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="Budget"
android:textColor="#03c03c "
android:textSize="18dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/textView8"
app:layout_constraintStart_toEndOf="@+id/textView8"
app:layout_constraintTop_toTopOf="@+id/textView8"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/textView9"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="3dp"
android:gravity="center"
android:shadowColor="#808080"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="January"
android:textColor="#FEFEFE"
android:textSize="26dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView10" />
<View
android:id="@+id/view4"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="3dp"
android:background="#50fefefe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view2" />
<View
android:id="@+id/view2"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="8dp"
android:background="#62fefefe"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView9" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="Expense"
android:textAllCaps="true"
android:textColor="#fefefe"
android:textSize="12dp"
app:layout_constraintBottom_toBottomOf="@+id/view4"
app:layout_constraintStart_toStartOf="@+id/view4"
app:layout_constraintTop_toTopOf="@+id/view4" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:text="Income"
android:textAllCaps="true"
android:textColor="#fefefe"
android:textSize="12dp"
app:layout_constraintBottom_toBottomOf="@+id/view2"
app:layout_constraintStart_toStartOf="@+id/view2"
app:layout_constraintTop_toTopOf="@+id/view2" />
<TextView
android:id="@+id/expnsAmount"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:gravity="right"
android:hint="₱ 0"
android:text="₱ 0"
android:textColor="#fefefe"
android:textColorHint="#fefefe"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/view4"
app:layout_constraintEnd_toEndOf="@+id/view4"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toTopOf="@+id/view4" />
<TextView
android:id="@+id/incmeAmount"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:gravity="right"
android:hint="₱ 0"
android:text="₱ 0"
android:textColor="#fefefe"
android:textColorHint="#fefefe"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="@+id/view2"
app:layout_constraintEnd_toEndOf="@+id/view2"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="@+id/view2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Balance"
android:textAllCaps="true"
android:textColor="#696969"
android:textSize="15dp"
app:layout_constraintBottom_toBottomOf="@+id/textView17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view" />
<TextView
android:id="@+id/textView17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="right"
android:text="₱ 0"
android:textColor="#77dd77"
android:textSize="23dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView4"
app:layout_constraintTop_toBottomOf="@+id/view" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="150dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="10dp"
android:padding="4dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView17">
</android.support.v7.widget.RecyclerView>
<EditText
android:hint="amount"
android:id="@+id/textInsert"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:inputType="number"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/recyclerView" />
<Button
android:id="@+id/buttonInsert"
android:layout_width="143dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="Insert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textRemove" />
<EditText
android:hint="details"
android:id="@+id/textRemove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textInsert" />
<Button
android:id="@+id/buttonRemove"
android:layout_width="130dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="44dp"
android:text="Remove"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textRemove" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
我想要做的是,当我启动该应用程序时,recyclerview为空,并且当我将项目添加到null的recyclerview中时,将没有错误地添加项目
答案 0 :(得分:1)
我不知道它是如何工作的,但也许会对您有帮助
df.pivot(index='date', columns='direction', values='pct').fillna(0).plot(kind='area')
//主要是每次单击按钮时都会调用适配器
AddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Arraylist yourItem=new ArrayList();
yourItem.add(add what you want);
//-------------Now call again your adapter to add items--------------
adapter= new Recycler_activity_main_adapter( yourItem,
context, myListner);
recyclerView.setAdapter(adapter);
尝试一下,让我知道它是否对您有用
答案 1 :(得分:0)
取消createExampleList();
中的onCreate
注释,因为您在其中实例化了mExampleList
,并且由于评论而为空
或者如果您要清空,而不仅仅是在ArrayList
中实例化onCreate
mExampleList = new ArrayList <>();