将动态文本视图添加到片段会使应用程序崩溃
Adding a dynamic textview to a fragmment crashes app
我一直在尝试向我的片段动态添加文本视图,但它导致应用程序崩溃,下面的代码在非片段中工作 activity 我只是无法在片段中工作。目前我的代码仅在 textview 上使用,但最终 return 很多来自数据库,因此我需要动态创建它。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list_batches, container, false);
context=getActivity();
//Dynamically create Elements
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)getActivity().findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(context);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
View theView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
theView = inflater.inflate(R.layout.fragment_list_batches, container,false);
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)theView .findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(theView.getContext);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
我一直在尝试向我的片段动态添加文本视图,但它导致应用程序崩溃,下面的代码在非片段中工作 activity 我只是无法在片段中工作。目前我的代码仅在 textview 上使用,但最终 return 很多来自数据库,因此我需要动态创建它。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.fragment_list_batches, container, false);
context=getActivity();
//Dynamically create Elements
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)getActivity().findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(context);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView
View theView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
theView = inflater.inflate(R.layout.fragment_list_batches, container,false);
LinearLayout.LayoutParams SVView = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout SV = (LinearLayout)theView .findViewById(R.id.listBatchesRelative);
TextView batchName = new TextView(theView.getContext);
int i = 1;
batchName.setId(Integer.valueOf(i));
batchName.setText("Dynamic Input view");
batchName.setLayoutParams(SVView);
SV.addView(batchName);
return theView