【发布时间】:2015-03-19 14:42:25
【问题描述】:
我知道这个问题已经被多次提出并解决了,但是我看到的解决方案都没有帮助我,所以又来了:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home_screen, container, false);
try {
userInfo = (TextView) rootView.findViewById(R.id.showData);
user myUser = new user();
userInfo.setText(myUser.getUserInfo());
}
catch(Exception e){
System.out.println("ERROR: "+e.getMessage());
}
return rootView;
}
这是我的片段(Android Studio 创建的默认片段,稍作修改)。对应的 XML 布局是这样的:
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".HomeScreen$PlaceholderFragment">
<TextView android:id="@+id/showData"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
我得到的错误:
03-19 08:23:09.119 29125-29125/com.app.myAPP I/System.out﹕ ERROR: null
03-19 08:23:12.296 29125-29125/com.app.myAPP I/System.out﹕ ERROR: null
TextView 是在 OnCreateView() 而不是 onCreate() 中创建的,这似乎在大多数情况下会导致 null 返回。
感谢任何帮助。
【问题讨论】:
-
请贴出完整的java代码,到目前为止还不足以说明问题所在
标签: android android-fragments android-studio oncreate