我想动态添加textview。当我尝试这样做时,我得到零点异常
这是我的代码段:
LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_main);
LayoutInflater Inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View vs = (View) Inflater.inflate(R.layout.serverd_details, null);
TextView textView = (TextView) vs.findViewById(R.id.SerText);
textView.setText("server details");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);
提前致谢...
答案 0 :(得分:0)
像这样改变你的LinearLayout
LinearLayout layout=(LinearLayout)findViewById(R.Id.your_layout_ID)
或
TextView textView = (TextView) vs.findViewById(R.id.SerText);
是SerText ID放在您的XML布局
中答案 1 :(得分:0)
你好Karthick Pandiyan,
我认为你做得很好,但在查看你的代码后,我想建议你
使用以下代码。
TextView textView = new TextView(context);
textView.setText("server details");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);
我认为它会奏效。