내 Activity의 onCreate에서 findViewById를 사용할 때마다 NullPointerException이 발생합니다. 예를 들어 :onCreate에서 findViewById를 호출 할 때 NullPointerException이 발생했습니다.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
TextView mTextView = (TextView) findViewById(R.id.textview);
}
그 문제는 내가 그들을 찾으려고 내가 널 포인터를 얻을 이유가있을 때 뷰가 완전히로드되지 않을 수 있습니다 그것에 의하여 수 있음을 읽었습니다. 내 활동의 조각의 onCreateView에서 findViewById를 요구하는 경우, 모든 작동합니다 :
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
TextView mTextView = (TextView) rootView.findViewById(R.id.textview);
}
그러나, 나는이 조각 클래스 외부 mTextView를 사용해야합니다. findViewById가 Activity의 onCreate에서 사용되는 Android의 공식 문서에서 많은 예제를 보았습니다. 내가 도대체 뭘 잘못하고있는 겁니까?
조각에 setContentView를 사용하고 onCreate에서 레이아웃을 조작하지 않는 것이 좋습니다. http://developer.android.com/guide/components/fragments.html –
어디에서 레이아웃을 조작해야합니까? – iguarna