2
我是android开发中的新成员..我正在使用fragment。我现在面临的问题是 - 我无法在fragment中动态添加视图。当我尝试这样做时,我得到了一个NullPointerException ...我的代码段如下:动态地在片段中添加textview
final LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.connections_layout, container, false);
EditText editText = new EditText(getActivity());
final int i = 5;
editText.setId(i); //Set id to remove in the future.
editText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
editText.setText("Hello");
Log.d("View","Start");
try{
linearLayout.addView(editText);
}catch(Exception e){
e.printStackTrace();
}
的connection_layout.xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/connections"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000" >
</LinearLayout>
的片段是tabgroup的成员属于片段活动。我在代码片段中做错了什么T'任何形式的帮助是appreciated.Thanks提前...
我试着用它id..But这种方法也参考了的LinearLayout不成功... – SALEH
这段代码片段在哪里? –
你在哪里得到NPE? (exact line) – sandrstar