Java 初始化对象后在Android中获取空对象引用错误
我的代码就是这样做的: MainActivity正在使用意向向ItemListActivity传递对象“设备”:Java 初始化对象后在Android中获取空对象引用错误,java,android,android-fragments,Java,Android,Android Fragments,我的代码就是这样做的: MainActivity正在使用意向向ItemListActivity传递对象“设备”: // When button is clicked to go to ItemListActivity car = new Car(); device = new Device(car); intent.putExtra("device", device); // in onCreate // Intent intent = getIntent(); Devic
// When button is clicked to go to ItemListActivity
car = new Car();
device = new Device(car);
intent.putExtra("device", device);
// in onCreate //
Intent intent = getIntent();
Device device = (Device) intent.getSerializableExtra("device");
View recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
//////////////////
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(this, ItemsContent.ITEMS, mTwoPane, device));
}
public static class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
...
private Device mDevice;
// Here's the problem:
Car car = mDevice.getCar(); // getCar just returns the private Car field in Device
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
ItemsContent.ItemsPage item = (ItemsContent.ItemsPage) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id);
Fragment fragment;
fragment = car;
mParentActivity.getSupportFragmentManager().beginTransaction().replace(R.id.item_detail_container, fragment).commit();
...
}
}
...
SimpleItemRecyclerViewAdapter(ItemListActivity parent,
List<ItemsContent.ItemsPage> items,
boolean twoPane, Device device) {
mValues = items;
mParentActivity = parent;
mTwoPane = twoPane;
mDevice = device;
}
}
设备对象有一个Car对象作为字段,两者都实现可序列化
现在Car也是一个fragment类,因此在ItemListActivity中,我希望有一个选项来打开通过intent传递的设备对象中的Car片段。(这是一个主/明细流活动)
ItemListActivity:
// When button is clicked to go to ItemListActivity
car = new Car();
device = new Device(car);
intent.putExtra("device", device);
// in onCreate //
Intent intent = getIntent();
Device device = (Device) intent.getSerializableExtra("device");
View recyclerView = findViewById(R.id.item_list);
assert recyclerView != null;
setupRecyclerView((RecyclerView) recyclerView);
//////////////////
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
recyclerView.setAdapter(new SimpleItemRecyclerViewAdapter(this, ItemsContent.ITEMS, mTwoPane, device));
}
public static class SimpleItemRecyclerViewAdapter
extends RecyclerView.Adapter<SimpleItemRecyclerViewAdapter.ViewHolder> {
...
private Device mDevice;
// Here's the problem:
Car car = mDevice.getCar(); // getCar just returns the private Car field in Device
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
ItemsContent.ItemsPage item = (ItemsContent.ItemsPage) view.getTag();
if (mTwoPane) {
Bundle arguments = new Bundle();
arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id);
Fragment fragment;
fragment = car;
mParentActivity.getSupportFragmentManager().beginTransaction().replace(R.id.item_detail_container, fragment).commit();
...
}
}
...
SimpleItemRecyclerViewAdapter(ItemListActivity parent,
List<ItemsContent.ItemsPage> items,
boolean twoPane, Device device) {
mValues = items;
mParentActivity = parent;
mTwoPane = twoPane;
mDevice = device;
}
}
但我真的不明白为什么会发生这种情况,因为即使我只是设置car=null,我的应用程序也会一直运行,直到我输入ItemListActivity并选择car片段。然后应用程序崩溃了,但我不明白设置car=mDevice.getCar()怎么会不允许我的应用程序从main进入ItemListActivity,所以我不明白为什么在尝试打开ItemListActivity时会出现空错误。谢谢
编辑:添加构造函数代码您声明的mDevice为空
私有设备mDevice代码>。在下一行代码中,您尝试调用该空对象上的方法Car-Car=mDevice.getCar()代码>所以你得到了一个NPE..哦,我忘了发布构造函数,但是在setUpRecyclerView中,设备是一个参数,而mDevice=device gets calledDoes并不重要,因为你写了mDevice.getCar()代码>在构造函数之外。如果您提供的代码示例不代表实际情况,则需要更新问题。编辑以在该类中添加构造函数。因此,当我调用setUpRecyclerView时,构造函数是否会先设置mDevice=device,然后继续代码?在调用getCar()之前,我应该如何分配mDevice@takendarkk“构造器不会先设置mDevice=device”否,发生在mDevice.getCar()之后因为它是在类级别而不是在方法内部编写的。只需调用抛出NPE的方法,并在最后将其放入构造函数中即可——它不需要位于类级别。