簡體   English   中英

將動態textview添加到碎片崩潰應用程序

[英]Adding a dynamic textview to a fragmment crashes app

我一直試圖將textview動態添加到片段中,但這導致應用程序崩潰,下面的代碼在非片段活動中有效,我只是無法使其在片段中有效。 目前,我的代碼僅在textview上使用,但最終它將從數據庫返回許多內容,因此為什么我需要動態創建它。

@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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM