我制作了订单食品应用程序。在这种情况下,当我单击订单激活应用程序时崩溃并在OrderStatus中给出了三个错误。下面的java是logcat和OrderStatus actitvity。单击图标后需要显示订单状态。
Logcat错误
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.example.sharma.digimenuserver.Common.Common.convertCodeToStatus(Common.java:16)
at com.example.sharma.digimenuserver.OrderStatus$1.onBindViewHolder(OrderStatus.java:78)
at com.example.sharma.digimenuserver.OrderStatus$1.onBindViewHolder(OrderStatus.java:64)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:122)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6673)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6714)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5647)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5913)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1767)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:356)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
07-22 17:30:08.181 3099-3109/com.example.sharma.digimenuserver I/art: Debugger is no longer active
07-22 17:34:22.748 3099-3141/com.example.sharma.digimenuserver I/FirebaseAuth: [FirebaseAuth:] Loading module via
OrderStatus.java
公共类OrderStatus扩展了AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter<Request,OrderViewHolder> adapter;
FirebaseDatabase db;
DatabaseReference requests;
MaterialSpinner spinnner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order_status);
db = FirebaseDatabase.getInstance();
requests=db.getReference("Requests");
recyclerView = (RecyclerView)findViewById(R.id.listOrders);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
loadOrders();
}
private void loadOrders() {
FirebaseRecyclerOptions<Request> options =
new FirebaseRecyclerOptions.Builder<Request>()
.setQuery(requests, Request.class).build();
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(options) {
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.order_layout, parent, false);
return new OrderViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull OrderViewHolder holder, int position, @NonNull Request model) {
//do binding stuff
holder.txtOrderId.setText(adapter.getRef(position).getKey());
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
holder.txtOrderAddress.setText(model.getAddress());
holder.txtOrderPhone.setText(model.getPhone());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onClick(View view, int position, boolean isLongClick) {
}
});
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
adapter.startListening();
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle().equals(Common.UPDATE))
showUpdateDialog(adapter.getRef(item.getOrder()).getKey(),adapter.getItem(item.getOrder()));
else if(item.getTitle().equals(Common.DELETE))
deleteOrder(adapter.getRef(item.getOrder()).getKey());
return super.onContextItemSelected(item);
}
private void deleteOrder(String key) {
requests.child(key).removeValue();
}
private void showUpdateDialog(String key, final Request item) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(OrderStatus.this);
alertDialog.setTitle("Update Order");
alertDialog.setMessage("Please Choose Status");
LayoutInflater inflater = this.getLayoutInflater();
final View view = inflater.inflate(R.layout.update_order_layout,null);
spinnner = (MaterialSpinner)findViewById(R.id.statusSpinner);
spinnner.setItems("Placed","On my way","Shipped");
alertDialog.setView(view);
final String localKey = key;
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
item.setStatus(String.valueOf(spinnner.getSelectedIndex()));
requests.child(localKey).setValue(item);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
}
}
**三行得到错误**
switch (code) {
case "0":
return "Placed";
case "1":
return "On my way";
default:
return "Shipped";
}
holder.txtOrderStatus.setText(Common.convertCodeToStatus(model.getStatus()));
adapter = new FirebaseRecyclerAdapter<Request, OrderViewHolder>(options) {
https://send.firefox.com/download/80a23ccfae/#W_ricyMLLaYtPkPg_UGNFA
答案 0 :(得分:1)
您没有提到哪一行,但是我最好的猜测是code
为null可能会对此进行检查。
Java同时使用.equals()
和.hashCode()
来验证switch语句中的字符串,因此,如果代码为null,则会出现此错误
答案 1 :(得分:1)
该错误发生在“ convertCodeToStatus()”过程中,所以我认为您的“ model.getStatus()”返回NULL。