gpt4 book ai didi

android - 尝试通过按下按钮将行添加到表中时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 17:39:43 25 4
gpt4 key购买 nike

我是安卓新手。当用户按下对话框中的“提交”按钮时,我试图从对话框中获取输入并将数据添加到我的 fragment 中的表中。我在按下提交按钮时遇到崩溃。知道如何解决这个问题吗?

public class Monitoring extends Fragment{

Button btn_add_value;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_inr_monitoring, container, false);

design_button(view);

return view;
}

public void design_button(View view){
btn_add_value = (Button)view.findViewById(R.id.btn_add_inr);
btn_add_value.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog_input = new Dialog(getActivity(), R.style.FullHeightDialog);
dialog_input.setContentView(R.layout.dialog_inr_input);

Button btn_submit = (Button) dialog_input.findViewById(R.id.dialog_submit_button);
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

//TODO retrive date and value and send to table
EditText input_value = (EditText)dialog_input.findViewById(R.id.input_inr_value);
String value = input_value.getText().toString();

DatePicker input_date = (DatePicker)dialog_input.findViewById(R.id.input_date_date);
String date = input_date.getDayOfMonth() + " / " + input_date.getMonth() + " / " + input_date.getYear();

add_value_table(v,date,value);

dialog_input.dismiss();
}
});

Button btn_cancel = (Button) dialog_input.findViewById(R.id.dialog_cancel_button);
btn_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog_input.dismiss();
}
});

dialog_input.show();
}
});
}

public void add_value_table(View view, String date, String value){

TableLayout table = (TableLayout)view.findViewById(R.id.inr_table);
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new TableRow.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));

TextView table_date = new TextView(getActivity());
table_date.setText(date);
row.addView(table_date);

TextView table_value = new TextView(getActivity());
table_date.setText(value);
row.addView(table_value);

table.addView(row);
}

}

日志:

    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TableLayout.addView(android.view.View)' on a null object reference
at com.sghhc.heartpluswarfarin.inr_monitoring.MonitoringINR$1$1.onClick(MonitoringINR.java:70)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
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:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

最佳答案

view 而不是 v 作为第一个参数传递给 add_value_table 方法,以从 fragment_inr_monitoring 访问 View :

add_value_table(view,date,value);

关于android - 尝试通过按下按钮将行添加到表中时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159460/

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