2012-10-24 2 views
2

나는 안드로이드 개발에 새로운 편이다. 나는 조각으로 작업하고있다. 나는 지금 직면하고있다. 나는 조각을 동적으로보기를 추가 할 수 없다. NullPointerException가 ... 내 코드는 다음과 같다 :조각에 동적으로 textview 추가하기

<?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의 구성원은 다음과 같습니다

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 파일이 . 내 코드 발췌 문장에서 내가 뭘 잘못하고 있니? 티?

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

mContainer = inflater.inflate(R.layout.connections_layout, null); 
LinearLayout linearLayout = mContainer.findViewById(R.id.connections); 
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(); 
} 

return mContainer; 

가}

+0

내가 그 접근 방식도했다 그 id..But으로있는 LinearLayout을 참조하려고 : 도움의 어떤 종류 – SALEH

+0

이 코드 스 니펫은 어디에 있습니까? –

+0

NPE는 어디서 구할 수 있습니까? (정확한 행) – sandrstar

답변

3

이 조각의 onCreateView에 시도 ... 사전에 appreciated.Thanks입니다 실패했습니다 ...
+0

에 있음을 의미합니까? 작동합니까? –

관련 문제