myFragmentInstance.setData(Object someData);
崩溃发生是因为它告诉我的那些视图元素在片段中找不到,它们是NULL,但如果我没有重新创建它们,一切应该都没问题。我没有旋转手机,或者内存不足。它发生在网络重新连接时,因为在网络重新连接时,我会去服务器获取新鲜数据,然后将该新数据设置到我的片段中。我有两个片段字段的照片,也许你们中的一些人知道那些数据可以告诉关于崩溃时片段状态的信息。
我使用库ButterKnife
来初始化片段和活动的字段,而不是使用findById
进行初始化,也许它有一些影响或没有影响?
这是一个简单的项目链接(仅此问题在github上):https://github.com/yozhik/Reviews/tree/master/app/src/main/java/com/ylet/sr/review
描述:
CollapsingActivity
- 带有Collapsing AppBarLayout
的活动。它将一个或两个片段加载到“fragment_content_holder
”中,并具有TabLayout
以在视图分页器中切换片段。
在活动方法onCreate()
中,我只是模拟对服务器的请求(loadData
),当加载一些虚假数据时,我会在视图分页器中显示片段,在第一次调用时,我创建了新的TabMenuAdapter
扩展FragmentPagerAdapter
,用片段填充并保存实例链接。在下一次调用中,我不会从头开始创建片段,只是用新鲜的数据填充它们。
MenuFragment1, MenuFragment2
- 两个片段。 MenuFragment1
- 有一个方法public void setupData(SomeCustomData data)
,用于设置新数据,而不是在网络重新连接时重新创建片段。
NetworkStateReceiver
- 监听网络变化并发送通知。
TabMenuAdapter
- 只是一个简单的类来保存片段。
05-11 18:11:05.088 12279-12279/com.myProjectName E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myProjectName, PID: 12279
java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:111)
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:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.yozhik.myProjectName.view.fragments.MyFinalTermsFragment.setupMyInformation(MyFinalTermsFragment.java:145)
at com.yozhik.myProjectName.view.fragments.MyFinalTermsFragment.setupWithData(MyFinalTermsFragment.java:133)
at com.yozhik.myProjectName.view.activity.MyFinalActivity.onDataLoaded(MyFinalActivity.java:742)
at com.yozhik.myProjectName.presenter.MyFinalPresenter$1.onNext(MyFinalPresenter.java:55)
at com.yozhik.myProjectName.presenter.MyFinalPresenter$1.onNext(MyFinalPresenter.java:47)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.drainNormal(ObservableObserveOn.java:200)
at io.reactivex.internal.operators.observable.ObservableObserveOn$ObserveOnObserver.run(ObservableObserveOn.java:252)
at io.reactivex.android.schedulers.HandlerScheduler$ScheduledRunnable.run(HandlerScheduler.java:109)
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:5268)
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:902)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
05-11 18:11:07.953 2155-3877/? E/WifiStateMachine: Did not find remoteAddress {192.168.200.1} in /proc/net/arp
05-11 18:11:07.966 2155-3877/? E/WifiStateMachine: WifiStateMachine CMD_START_SCAN source -2 txSuccessRate=3800.62 rxSuccessRate=4732.06 targetRoamBSSID=any RSSI=-68
05-11 18:11:07.967 2155-3877/? E/WifiStateMachine: WifiStateMachine L2Connected CMD_START_SCAN source -2 2324, 2325 -> obsolete
05-11 18:11:08.021 2155-3896/? E/ConnectivityService: Unexpected mtu value: 0, wlan0
05-11 18:11:08.579 13514-13366/? E/WakeLock: release without a matched acquire!
在方法setupData中崩溃的片段,因为data_1_txt有时为空。
public class MenuFragment1 extends Fragment {
public SomeCustomData transferedDataFromActivity;
private TextView data_1_txt;
public static MenuFragment1 newInstance(SomeCustomData data) {
Log.d("TEST", "MenuFragment1.newInstance");
MenuFragment1 fragment = new MenuFragment1();
Bundle args = new Bundle();
args.putSerializable("DATA_FROM_ACTIVITY", data);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("TEST", "MenuFragment1.onCreate");
if (getArguments() != null) {
this.transferedDataFromActivity = (SomeCustomData) getArguments().getSerializable("DATA_FROM_ACTIVITY");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d("TEST", "MenuFragment1.onCreateView");
View v = inflater.inflate(R.layout.menu_fragment_1, container, false);
data_1_txt = (TextView) v.findViewById(R.id.data_1_txt);
setupInOnCreateView();
return v;
}
protected void setupInOnCreateView() {
Log.d("TEST", "MenuFragment1.setupInOnCreateView");
//initialization of all view elements of layout with data is happens here.
setupData(transferedDataFromActivity);
}
public void setupData(SomeCustomData data) {
Log.d("TEST", "MenuFragment1.setupData");
this.transferedDataFromActivity = data;
if (transferedDataFromActivity != null) {
data_1_txt.setText(transferedDataFromActivity.Name);
}
}
}
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
android:orientation="vertical">
<TextView
android:id="@+id/data_1_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/yellow"
android:text="Test"
android:textSize="20sp" />
<include layout="@layout/description_layout" />
</LinearLayout>
onCreateView
中执行findViewById
。相反地,尝试在setupData
方法中执行findViewById
(而不是使用变量)。如果它仍然为空,则意味着你的片段已被销毁。顺便说一下,你需要特别小心地使用Thread
,因为它不具备生命周期感知能力。 - Dennis KgetMyData()
替换为你的textview.setText("your string")
。 - Pouya Danesh