我正在创建一个应用程序,您可以在其中输入存储在房间数据库中的数据,然后将其显示在recyclerView中。但是,我不断在应将输入添加到recyclerView适配器的代码上获取java.lang.NullPointerException
我已使用本教程https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#10设置了会议室数据库,并略微更改了代码以适合自己的使用。 我已经看过问题What is a NullPointerException, and how do I fix it?并了解了错误的含义,但是我不知道如何为我的代码特别解决(抱歉,我还是android studio的初学者)
input.java
public static final String EXTRA_REPLY = "com.example.android.mysugartrackersql.REPLY";
private EditText mEditInputView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
mEditInputView = findViewById(R.id.bgl_input);
final Button button = findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(mEditInputView.getText())) {
setResult(RESULT_CANCELED, replyIntent);
} else {
String input1 = mEditInputView.getText().toString();
replyIntent.putExtra(EXTRA_REPLY, input1);
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
}
InputListAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class InputListAdapter extends RecyclerView.Adapter<InputListAdapter.InputViewHolder> {
class InputViewHolder extends RecyclerView.ViewHolder {
private final TextView input1ItemView;
private InputViewHolder(View itemView) {
super(itemView);
input1ItemView = itemView.findViewById(R.id.textView);
}
}
private final LayoutInflater mInflater;
private List<Input1> mInput1s; // Cached copy of words
InputListAdapter(Context context) { mInflater = LayoutInflater.from(context); }
@Override
public InputViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = mInflater.inflate(R.layout.inputlist_item, parent, false);
return new InputViewHolder(itemView);
}
@Override
public void onBindViewHolder(InputViewHolder holder, int position) {
if (mInput1s != null) {
Input1 current = mInput1s.get(position);
holder.input1ItemView.setText(current.getInput1());
} else {
// Covers the case of data not being ready yet.
holder.input1ItemView.setText("No Input");
}
}
void setInput1s(List<Input1> input1s){
mInput1s = input1s;
notifyDataSetChanged();
}
// getItemCount() is called many times, and when it is first called,
// mInputs has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
if (mInput1s != null)
return mInput1s.size();
else return 0;
}
}
Table.java(显示recyclerView的片段)
public Table() {
// Required empty public constructor
}
private RecyclerView mRecyclerView;
private InputListAdapter mAdapter;
private InputViewModel mInputViewModel;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
InputRoomDatabase db = Room.databaseBuilder(getActivity(),InputRoomDatabase.class, "input_database")
.build();
//add recycler view
final View rootView = inflater.inflate(R.layout.fragment_table, container, false);
RecyclerView recyclerView = rootView.findViewById(R.id.recyclerview);
//set adapter
final InputListAdapter adapter1 = new InputListAdapter(getContext());
recyclerView.setAdapter(adapter1);
//set layout manager
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
//show input list in recycler view
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
mInputViewModel.getAllInput1s().observe(this, new Observer<List<Input1>>() {
@Override
public void onChanged(@Nullable final List<Input1> input1s) {
// Update the cached copy of the words in the adapter.
adapter1.setInput1s(input1s);
}
});
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_table, container, false);
return rootView;
}
}
Tabbed.java(基本片段类) (这不是完整的类,只有相关的代码)
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == INPUT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
Input1 input1 = new Input1(data.getStringExtra(input.EXTRA_REPLY));
**mInputViewModel.insert(input1);**
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
public static final int INPUT_ACTIVITY_REQUEST_CODE = 1;
这是logcat错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.mysugartracker/com.example.mysugartracker.Tabbed}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.mysugartracker.InputViewModel.insert(com.example.mysugartracker.Input1)' on a null object reference
at com.example.mysugartracker.Tabbed.onActivityResult(Tabbed.java:111)
第111行的代码中带有**标记
答案 0 :(得分:0)
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
此行位于Table.java
中,而不位于Tabbed.java
中。因此,您在另一个类中创建了mInputViewModel
,但在Tabbed.java
中使用了它。如果Table.java
扩展了Tabbed.java
,请从Table.java
删除此行
private InputViewModel mInputViewModel;
答案 1 :(得分:0)
感谢大家的帮助。我通过添加此行来解决此问题
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
在Tabbed.java中