下面是我所拥有的片段的代码,我正在尝试在视图中设置textView的文本,我的印象是,只要你先对视图进行充气,它就不会返回null,因为它有已创建。根据片段生命周期。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View loginFragmentView = inflater.inflate(R.layout.login_layout, container, false);
TextView imei = (TextView) loginFragmentView.findViewById(R.id.txt_device_id);
imei.setText("IMEI: ");
return loginFragmentView;
}
这确实导致应用程序因nullPointerException崩溃,有没有人知道如何解决这个问题。是否有一个我可以覆盖的方法只会被称为片段生命周期已完成的方法。
我也尝试将它放在onResume()中,因为它在生命周期结束时是正确的,但结果相同。有什么想法吗?
修改
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Your device ID:"
android:id="@+id/txt_id_title"
android:layout_marginTop="15dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Fetching ID..."
android:id="@+id/txt_device_id"
android:layout_below="@+id/txt_id_title"
android:layout_centerHorizontal="true" />
</RelativeLayout>
错误日志
05-22 19:40:02.230 2799-2799/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: alertssystem.rrr.lsa13.rrr_alerts, PID: 2799
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.widget.TextView.toString()' on a null object reference
at alertssystem.rrr.lsa13.rrr_alerts.login_layout_fragment.onCreateView(login_layout_fragment.java:24)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
答案 0 :(得分:0)
您的问题是因为您根据片段lifycycle过早地声明TextView,因此,不是在onCreateView
声明它,而是在onViewCreated
声明它,所以它将是:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View loginFragmentView = inflater.inflate(R.layout.login_layout, container, false);
return loginFragmentView;
}
@Override
public void onViewCreated(View loginFragmentView, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView imei = (TextView) loginFragmentView.findViewById(R.id.txt_device_id);
imei.setText("IMEI: ");
}
答案 1 :(得分:0)
查看你的stacktrace我猜你的loginFragmentView没有带有id txt_device_id 的textview类型的子项,所以检查你是否在inflate方法中传递包含带有id的textview的正确布局名称试图找到