为什么在片段内设置 textview 文本会导致崩溃?
Why does setting textview text inside a fragment result in a crash?
我正在尝试让 TextView 始终显示当前日期。但是,setText 方法使用 NullPointerExeption 使程序崩溃...截至目前,日期不是问题 - 我仅使用常规文本对其进行测试。我不知道把我的代码放在哪里最好,所以我决定使用 onActivityCreated。发生了什么事,还有什么我可以做的来使我的代码更好吗? *注意,textview 在 fragment_page 中,而不是 fragment_page 2,因为这是滑动选项卡布局中的第一个选项卡。
片段Class
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
if(mPage < 2) {
view = inflater.inflate(R.layout.fragment_page, container, false);
} else {
view = inflater.inflate(R.layout.fragment_page_2, container, false);
}
return view;
}
//@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
/*Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
*/
TextView textView = (TextView) getView().findViewById(R.id.current_date);
textView.setText("sadFace"); //null pointer exeption on this line
}
}
片段XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp" >
<ImageView
android:layout_width="239dp"
android:id="@+id/logo"
android:layout_height="100dp"
android:src="@drawable/logo"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/current_date"
android:text="Today is January 1st, 2015"
android:layout_toRightOf="@id/logo"
android:gravity="center"
android:layout_alignBottom="@id/logo"/>
</RelativeLayout>
</LinearLayout>
onCreateView
创建并 returns 与片段关联的视图层次结构。
把代码块移到onCreateView
里,如果current_date只在fragment_page
里,那么你也应该把它们移到IF
条件里。
View view;
if(mPage < 2) {
view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView)view.findViewById(R.id.current_date);
textView.setText("sadFace");
} else {
view = inflater.inflate(R.layout.fragment_page_2, container, false);
}
问题出在行
TextView textView = (TextView) getView().findViewById(R.id.current_date);
它自己工作,但导致空指针。我将 getView()
更改为 getActivity()
,现在它完美运行了。
我正在尝试让 TextView 始终显示当前日期。但是,setText 方法使用 NullPointerExeption 使程序崩溃...截至目前,日期不是问题 - 我仅使用常规文本对其进行测试。我不知道把我的代码放在哪里最好,所以我决定使用 onActivityCreated。发生了什么事,还有什么我可以做的来使我的代码更好吗? *注意,textview 在 fragment_page 中,而不是 fragment_page 2,因为这是滑动选项卡布局中的第一个选项卡。
片段Class
public class PageFragment extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private int mPage;
public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view;
if(mPage < 2) {
view = inflater.inflate(R.layout.fragment_page, container, false);
} else {
view = inflater.inflate(R.layout.fragment_page_2, container, false);
}
return view;
}
//@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
/*Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, yyyy");
*/
TextView textView = (TextView) getView().findViewById(R.id.current_date);
textView.setText("sadFace"); //null pointer exeption on this line
}
}
片段XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp" >
<ImageView
android:layout_width="239dp"
android:id="@+id/logo"
android:layout_height="100dp"
android:src="@drawable/logo"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/current_date"
android:text="Today is January 1st, 2015"
android:layout_toRightOf="@id/logo"
android:gravity="center"
android:layout_alignBottom="@id/logo"/>
</RelativeLayout>
</LinearLayout>
onCreateView
创建并 returns 与片段关联的视图层次结构。
把代码块移到onCreateView
里,如果current_date只在fragment_page
里,那么你也应该把它们移到IF
条件里。
View view;
if(mPage < 2) {
view = inflater.inflate(R.layout.fragment_page, container, false);
TextView textView = (TextView)view.findViewById(R.id.current_date);
textView.setText("sadFace");
} else {
view = inflater.inflate(R.layout.fragment_page_2, container, false);
}
问题出在行
TextView textView = (TextView) getView().findViewById(R.id.current_date);
它自己工作,但导致空指针。我将 getView()
更改为 getActivity()
,现在它完美运行了。