我在Fragments
上遇到了麻烦。我有四个片段处于肖像模式。
设置片段
public void setCorrectNavigationItem(int id) {
if (id == R.id.nav_auftragsbilder) {
fragment = new AuftragsbilderFragment();
id = R.id.nav_auftragsbilder;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (id == R.id.nav_auftragskorrektur) {
fragment = new AuftragskorrekturFragment();
id = R.id.nav_auftragskorrektur;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (id == R.id.nav_lagerplatz) {
fragment = new LagerplatzFragment();
id = R.id.nav_lagerplatz;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} else if (id == R.id.nav_biegenstatus) {
fragment = new BiegestatusFragment();
id = R.id.nav_biegenstatus;
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
_navigationView.setCheckedItem(id);
_selectedMenuItem = id;
SharedPreferences.Editor editor = menuCheck.edit();
editor.putInt("id", id);
editor.commit();
//Fragment öffnen
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
}
}
因此,在片段设置方法中,我现在必须为每个不同的片段设置方向。我必须将'BiegestatusFragment'更改为 landscape ,在那里我正在调用一种扫描条形码的方法,在意图结果上,我在Fragment中设置了参数。
这是我的条形码扫描
Activity
//Biegestatus Begleitschein-Barcodes
public void makeBiegestatusBegleitscheinBarcode() {
_lastAction = ACTION_BIEGESTATUS_BEGLEITSCHEIN_BARCODE;
if (!checkCameraPermission(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
} else {
Intent intent = new Intent(this, ContinuousCaptureActivity.class);
startActivityForResult(intent, 1);
}
}
意图结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null) {
ArrayList<String> begleitscheine = data.getStringArrayListExtra("begleitscheine");
if (resultCode == 1 && begleitscheine != null) {
//HERE IT IS NULL
((BiegestatusFragment) fragment).setBegleitscheine(begleitscheine);
} else {
Toast.makeText(this, "Scannen abgebrochen", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "Unbekannter Fehler aufgetreten, Entwickler kontaktieren.", Toast.LENGTH_LONG).show();
}
}
onCreateView片段
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_biegestatus, container, false);
_cardEdit = (EditText) v.findViewById(R.id.workerEdit);
_scrollViewArticles = (ScrollView) v.findViewById(R.id.scrollViewArticles);
_searchImageLayout = (LinearLayout) v.findViewById(R.id.searchImageLayout);
_personalNummer = (EditText) v.findViewById(R.id.workerEdit);
_progressBar = (LinearLayout) v.findViewById(R.id.progressBar);
_maschinenPicker = (NumberPicker) v.findViewById(R.id.maschinenPicker);
_pickerHolder = (LinearLayout) v.findViewById(R.id.pickerHolder);
_scanBegleitscheinBtn = (Button) v.findViewById(R.id.scanBegleitscheinBtn);
return v;
}
应调用方法
public void setBegleitscheine(ArrayList<String> begleitscheine) {
_begleitscheine = begleitscheine;
}
由于{strong>方向的更改,我的Fragment
为空。
删除方向设置方法可以解决我的问题。但是我需要在景观中保留这个片段。
错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: at.co.era.bilder.erabilderapp, PID: 21835
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=1, data=Intent { launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 }(has extras) }} to activity {at.co.era.bilder.erabilderapp/at.co.era.bilder.erabilderapp.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void at.co.era.bilder.erabilderapp.BiegestatusFragment.setBegleitscheine(java.util.ArrayList)' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:4520)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void at.co.era.bilder.erabilderapp.BiegestatusFragment.setBegleitscheine(java.util.ArrayList)' on a null object reference
at at.co.era.bilder.erabilderapp.HomeActivity.onActivityResult(HomeActivity.java:1130)
at android.app.Activity.dispatchActivityResult(Activity.java:7280)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4516)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4563)
at android.app.ActivityThread.-wrap22(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1698)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
答案 0 :(得分:0)
您可以通过将清单中的以下行添加到活动代码来强制更改方向后不重新加载活动
android:configChanges="orientation"
答案 1 :(得分:0)
//Fragment öffnen
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.fragment_container, fragment);
ft.addToBackStack(null); // or you can give it any name instead of null to get the frament when poping the fragment from backstack
ft.commit();
}
答案 2 :(得分:0)
内部片段中,在onCreateView()中尝试使用:
setRetainInstance(true);
在此处检查文档: https://developer.android.com/reference/android/app/Fragment#setRetainInstance(boolean)