我有一个BaseActivity
,其中包含不同活动中常见的代码。我有多个扩展BaseActivity
的活动。其中一个常见的是菜单按钮,用于打开MaterialDialog
,其中包含用户可以连接的打印机列表。该列表保存在RecyclerView
中。当我在setAdapter()
中尝试BaseActivity
时,我收到以下错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
如果我没有setAdapter()
对话框打开就好了。我做错了什么?
错误输出
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
at com.app.nex.nex.BaseActivity.onSelectPrinter(BaseActivity.java:445)
at com.app.nex.nex.BaseActivity$1.onItemClick(BaseActivity.java:206)
at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1786)
at com.mikepenz.materialdrawer.DrawerBuilder$6.onClick(DrawerBuilder.java:1760)
at com.mikepenz.fastadapter.FastAdapter$1.onClick(FastAdapter.java:608)
at com.mikepenz.fastadapter.utils.EventHookUtil$1.onClick(EventHookUtil.java:72)
at android.view.View.performClick(View.java:6306)
at android.view.View$PerformClick.run(View.java:23962)
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:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
BaseActivity
private ArrayList<String> mPrinters = new ArrayList<>();
private AccountHeader headerResult = null;
private Drawer result = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
this.savedInstanceState = savedInstanceState;
//Setup DrawerBuilder that has the SetupPrinter button that calls onSelectPrinter()
new DrawerBuilder().withActivity(this).build();
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(true)
.withHeaderBackground(R.drawable.logo)
.withSavedInstance(savedInstanceState)
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.addDrawerItems(
new SectionDrawerItem().withName(R.string.side_menu_title_settings),
new PrimaryDrawerItem().withName(R.string.side_menu_0_printer).withIcon(R.drawable.fire_smoke).withIdentifier(R.integer.side_menu_0_printer),
new SectionDrawerItem().withName("Settings"),
new SecondaryDrawerItem().withName("tWO").withIcon(R.drawable.fire)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
int item = (int) drawerItem.getIdentifier();
switch (item) {
case R.integer.side_menu_0_printer:
onSelectPrinter();
break;
default:
break;
}
System.out.println("DRAWER " + drawerItem.getIdentifier());
if (drawerItem != null && drawerItem.getIdentifier() == 1) {
//startSupportActionMode(new ActionBarCallBack());
}
if (drawerItem instanceof Nameable) {
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.build();
}
protected void onSelectPrinter() {
mPrinters.add("Epson 123");
mPrinters.add("Canon 999");
mPrinters.add("HP 12322");
mPrinters.add("Brother 12344");
TestAdapter adapter = new TestAdapter(mPrinters, this);
MaterialDialog dialog =
new MaterialDialog.Builder(this)
.title(R.string.title_printer_setup)
.customView(R.layout.dialog_select_printer, true)
.negativeText(android.R.string.cancel)
.build();
RecyclerView rv = dialog.getRecyclerView();
rv.setAdapter(adapter);
dialog.show();
}
TestAdapter
public class TestAdapter extends RecyclerView.Adapter<TestAdapter.ViewHolder> {
private final String TAG = this.getClass().getSimpleName();
private ArrayList<String> mPrinters;
private Context mContext;
public TestAdapter(ArrayList<String> printers, Context context) {
mContext = context;
mPrinters = printers;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dialog_select_printer, parent, false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d(TAG, "onviewbindholder called");
holder.tvTxt.setText(mPrinters.get(position));
holder.rlLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
@Override
public int getItemCount() {
return mPrinters.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView tvTxt;
RelativeLayout rlLayout;
public ViewHolder(View itemView) {
super(itemView);
rlLayout = itemView.findViewById(R.id.layoutSelectPrinter);
tvTxt = itemView.findViewById(R.id.tvPrinterName);
}
}
}
rv_select_printer.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#80000000"
android:id="@+id/tvPrinterName"
/>
</RelativeLayout>
dialog_select_printer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutSelectPrinter"
>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvPrinters"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
答案 0 :(得分:0)
在onSelectPrinter()方法中使用它:
LayoutInflater inflater=LayoutInflater.from(getBaseContext());
View view=inflater.inflate(R.layout.dialog_select_printer,null);
MaterialDialog dialog =
new MaterialDialog.Builder(this)
.title(R.string.title_printer_setup)
.setView(view)
.negativeText(android.R.string.cancel)
.build();
RecyclerView rv = view.findViewById(R.id.rvPrinters);
rv.setAdapter(adapter);
dialog.show();