gpt4 book ai didi

java - 订单 Activity 因空点引用而崩溃

转载 作者:行者123 更新时间:2023-12-02 02:01:51 29 4
gpt4 key购买 nike

我制作了订单食品应用程序。当我单击订单 Activity 应用程序时,应用程序崩溃并在下面的 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

公共(public)类 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

最佳答案

您没有提及哪一行,但我最好的猜测是 code is null 可能会检查这一点。Java 使用 .equals().hashCode() 来验证 switch 语句中的字符串,因此如果 code 为 null,您将收到此错误

关于java - 订单 Activity 因空点引用而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464978/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com