我基本上是在Android Studio(完整的初学者)上制作预算应用程序,使用显示所有购买内容的ListView等。 对于ListView,我使用了ArrayList,其中Purchase是带有购买名称,价格和日期的类。
为了在关闭应用程序时保持对应用程序的购买,我使用了一个类来编写数据并在打开时读取数据。当我的ArrayList为空时,该应用程序运行良好,但是当我的列表不为空时,该应用程序立即崩溃,并使用NPE崩溃“ FATAL EXCEPTION:main ...”:
无法启动活动ComponentInfo {com.rhstudio.budgetrescue / com.rhstudio.budgetrescue.SecondActivity}:java.lang.NullPointerException:尝试在null上调用接口方法'int java.util.List.size()'对象参考 原因:java.lang.NullPointerException:尝试在android.widget上的android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344)上的空对象引用上调用接口方法'int java.util.List.size()' .com.rhstudio.budgetrescue.SecondActivity.onCreate(SecondActivity.java:44)上的.ListView.setAdapter(ListView.java:493)
line44是这个:
itemsListView.setAdapter(adapter);
下面没有完整的代码,但我认为多数
任何帮助将不胜感激
public class SecondActivity extends AppCompatActivity implements CustomPopUp.CustomPopUpListener, AdapterView.OnItemClickListener {
private SharedPreferences sharedPref = null;
private ListView itemsListView;
private static ArrayList<Purchase> items;
private PurchaseListAdapter adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
itemsListView = (ListView) findViewById(R.id.items_list);
sharedPref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
items = FileHelper.readData(this);
adapter = new PurchaseListAdapter(this, R.layout.adapter_view_layout, items);
itemsListView.setAdapter(adapter);
itemsListView.setOnItemClickListener(this);
}
public void applyChanges(String name, String currentDate, Float value) {
Purchase purchase = new Purchase(name, currentDate, value);
items.add(purchase);
FileHelper.writeData(items, this);
adapter = new PurchaseListAdapter(this, R.layout.adapter_view_layout, items);
itemsListView.setAdapter(adapter);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
items.remove(position);
adapter.notifyDataSetChanged();
FileHelper.writeData(items, this);
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
}
}
public class FileHelper {
public static final String FILENAME = "listinfo.dat";
public static void writeData(ArrayList<Purchase> items, Context context) {
try {
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(items);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ArrayList<Purchase> readData(Context context) {
ArrayList<Purchase> itemsList = null;
try {
FileInputStream fis = context.openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
itemsList = (ArrayList<Purchase>) ois.readObject();
} catch (FileNotFoundException e) {
itemsList = new ArrayList<>();
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return itemsList;
}
}
public class PurchaseListAdapter extends ArrayAdapter<Purchase> {
private static final String TAG ="PurchaseListAdapter";
private Context mContext;
private int mResource;
public PurchaseListAdapter(Context context, int resource, ArrayList<Purchase> items) {
super(context, resource, items);
mContext = context;
mResource = resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
String name = getItem(position).getName();
String currentDate = getItem(position).getCurrentDate();
Float value = getItem(position).getValue();
String sValue = Float.toString(value);
Purchase purchase = new Purchase(name, currentDate, value);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
TextView tvName = (TextView) convertView.findViewById(R.id.textView25);
TextView tvCurrentDate = (TextView) convertView.findViewById(R.id.textView26);
TextView tvValue = (TextView) convertView.findViewById(R.id.textView27);
tvName.setText(name);
tvCurrentDate.setText(currentDate);
tvValue.setText(sValue + "€");
return convertView;
}
}