我有一个应用程序来辩论,为此我使用了java和xml。应用程序显示了一个列表,其中包含用户添加的关于应用程序中发布的主题的论点。当用户想辩论时,他点击一个图像视图,显示一个警告对话框,询问他是否真的想反驳。到目前为止,一切都运行得很完美。当用户按下“ok I want to counter”(警报对话框内的按钮)时,问题出现,因为当这样做时,它返回以下错误:“Java .lang.空指针异常:尝试在空对象引用”“上调用接口方法”void Fragments.sendArgument.sendArgument(java.lang.String).“我应该将参数作为参数发送到另一个片段,以便在此新片段中显示。我正在使用接口将存储参数的变量发送到另一个片段。我将给您留下部分代码,以便您可以帮助我。问候
回收视图
public class adapterArguments extends RecyclerView.Adapter<adapterArguments.ViewHolder> {
private List<ListArguments> data;
private LayoutInflater inflater;
private Context context;
public String position_selected,argument;
public static String argument_aux;
private sendArgument sendArgument;
public adapterArguments(List<ListArguments> itemList, Context context,sendArgument argument) {
this.data = itemList;
this.inflater = LayoutInflater.from(context);
this.context = context;
this.sendArgument = argument;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.compontents_arguments_recyclerview,null);
return new adapterArguments.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.arguments.setText(data.get(position).getArguments());
holder.name_user.setText(data.get(position).getName_user());
holder.date.setText(data.get(position).getDate_arguments());
holder.position.setText(data.get(position).getPosition());
position_selected = holder.position.getText().toString();
if(position_selected.equals("A favor")){
holder.position.setBackgroundColor(Color.parseColor("#073FD7"));
}
else{
holder.position.setBackgroundColor(Color.parseColor("#FC0C1E"));
}
holder.addContrarguments.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(v.getContext());
alertDialog.setTitle("Añadir un Contrargumento");
alertDialog.setMessage("¿Quieres añadir un contrargumento a este texto?");
alertDialog.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
argument = holder.arguments.getText().toString();
sendArgument.sendArgument(argument);
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
fragmentManager = ((AppCompatActivity)context).getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new contrargument());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
alertDialog.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog customAlertDialog = alertDialog.create();
customAlertDialog.show();
}
});
}
@Override
public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView arguments,name_user,date,position;
ImageButton addContrarguments;
public ViewHolder(@NonNull View itemView) {
super(itemView);
arguments = itemView.findViewById(R.id.arguments_posted);
name_user = itemView.findViewById(R.id.name_arguments);
转接器
package Fragments;
public interface sendArgument {
void sendArgument (String Argument);
}
碎片
public class contrargument extends Fragment implements sendArgument {
public String name_user_post_argument,argument_posted;
TextView text_contrarguent,argument,valoration_argument;
Button post_contrargument;
ImageView increace_valoration,decreace_valoration,back_fragment;
public int valoration;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_contrargument, container, false);
argument = view.findViewById(R.id.text_argument);
text_contrarguent = view.findViewById(R.id.contrargument_post);
post_contrargument = view.findViewById(R.id.post_contrargumment);
name_user_post_argument = perfil_user.name_aux;
increace_valoration = view.findViewById(R.id.increace_valoration);
decreace_valoration = view.findViewById(R.id.decreace_valoration);
valoration_argument = view.findViewById(R.id.valoration_argument);
back_fragment = view.findViewById(R.id.returned);
valoration = 0;
increace_valoration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
valoration = valoration + 1;
valoration_argument.setText(String.valueOf(valoration));
}
});
decreace_valoration.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(valoration > 0){
valoration = valoration - 1;
valoration_argument.setText(String.valueOf(valoration));
}
else if (valoration == 0 ){
valoration_argument.setText(String.valueOf(0));
}
else{
//pass
}
}
});
return view;
}
@Override
public void sendArgument(String Argument) {
Toast.makeText(getActivity(), "Got: " + Argument, Toast.LENGTH_SHORT).show();
}
}
1条答案
按热度按时间vm0i2vca1#
这里你不需要接口,你可以直接把数据传递给你的片段,就像这样。
在你的contrargument中,获取你片段的onViewCreated中的sent参数。