gpt4 book ai didi

android - 尝试在空对象引用(Fragment)上调用虚拟方法 'android.content.res.Resources$Theme android.content.Context.getTheme()'

转载 作者:行者123 更新时间:2023-12-02 18:30:59 26 4
gpt4 key购买 nike

这是 fragment 的java文件代码:

@SuppressLint({"NewApi", "ResourceAsColor"})
public void createCardView(String id, String date, final String order) {
flag = true;
cardView = new CardView(context);
cardView.setLayoutParams(layoutParamsCard);
cardView.setElevation(5);
relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(layoutParamsRelative);
textView = new TextView(context);
orderID = new TextView(context);
textView.setLayoutParams(layoutParamsTextView);
textView.setText("Order ID:");
textView.setTextColor(Color.parseColor("#1e1e1e"));
textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
orderID.setLayoutParams(layoutParamsTextViewID);
orderID.setText(id);
orderID.setTextColor(Color.parseColor("#646464"));
orderID.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
orderDate = new TextView(context);
orderDate.setText(date);
orderDate.setLayoutParams(layoutParamsOrderDate);
viewOrder = new Button(context);
viewOrder.setText("View Order");
viewOrder.setTextColor(Color.parseColor("#ffffff"));
viewOrder.setBackgroundColor(Color.parseColor("#326432"));
viewOrder.setLayoutParams(layoutParamsViewOrder);
viewOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.view_order_dialog,null);
dialogBuilder.setView(dialogView);
final TextView orderTextView = dialogView.findViewById(R.id.orderTextView);
final Button close = dialogView.findViewById(R.id.close);
orderTextView.setText(order);
dialogBuilder.setTitle("Order ID: " + orderID.getText().toString());
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
});
cardView.addView(relativeLayout);
relativeLayout.addView(textView);
relativeLayout.addView(orderID);
relativeLayout.addView(viewOrder);
relativeLayout.addView(orderDate);
gridLayout.addView(cardView);
}

我无法从 fragment 启动自定义对话框 View 。当我打开该 fragment 时,它会使应用程序崩溃。我使用此代码动态创建卡片并为每张卡片创建一个 OnClickListner。

最佳答案

您的 Activity 上下文在此行中为空:

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity()); // getActivity() is getting null here

为了避免崩溃,您应该包装代码并检查您的 Activity 上下文是否不为空,如下所示:

if(getActivity() != null){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getLayoutInflater();
final View dialogView = inflater.inflate(R.layout.view_order_dialog,null);
dialogBuilder.setView(dialogView);
final TextView orderTextView = dialogView.findViewById(R.id.orderTextView);
orderTextView.setText(order);
final AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}

关于android - 尝试在空对象引用(Fragment)上调用虚拟方法 'android.content.res.Resources$Theme android.content.Context.getTheme()',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288555/

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