【发布时间】:2018-10-21 11:46:06
【问题描述】:
我需要调用一个方法来解析 JSON 对象并设置我在 TextFields 中设置值和按钮值的值,我创建了片段,但是在创建片段之后我将如何更改文本字段值
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
//Switch to different layout accordingly
switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1: {
rootView = inflater.inflate(R.layout.fragment_main, container, false);
break;
}
case 2: {
rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
break;
}
case 3: {
rootView = inflater.inflate(R.layout.fragment_totals, container, false);
break;
}
case 4: {
rootView = inflater.inflate(R.layout.fragment_keyboard, container, false);
break;
}
case 5: {
rootView = inflater.inflate(R.layout.fragment_keylock, container, false);
break;
}
}
return rootView;
}
}
我尝试在onStart 中调用该方法,但在onCreateView 之前首先调用了 onStart 方法,因此在分配值时出现空指针异常:
@Override
public void onStart() {
super.onStart();
parseJsonResponse(response_message);
}
在 parseJsonResponse() 方法中,我设置了值
String UIFooter = ResponseHeaderObject.getString("FOOTER");
//Set Header Display
headerTextView =(TextView) findViewById(R.id.headerTextView);
headerTextView.setText(UIHeader);
这是来自 logcat 的错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.mpos.kits.smartpos.PosMainActivity.parseJsonResponse(PosMainActivity.java:366)
【问题讨论】:
-
在片段中确实有一个名为
onViewCreated的方法。如果你只用谷歌搜索你的问题的标题,你就会找到它。
标签: java android android-fragments