我在父活动上加载了一个片段。该活动具有“底部导航”视图。我想在特定阶段隐藏“底部导航”。
这是我尝试执行的操作,但是不起作用,并出现以下错误。
java.lang.NullPointerException:
如果我在Activity本身onCreate(
上调用这些方法,则可以正常工作。隐藏的导航视图。
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="411dp"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/frame_layout"
app:menu="@menu/navigation" />
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView navigation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(this);
loadFragment(new LiveVideoOneFragment());
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
Fragment fragment = null;
switch (menuItem.getItemId()) {
case R.id.navigation_live_one:
fragment = new LiveVideoOneFragment();
break;
case R.id.navigation_live_two:
fragment = new LiveVideoTwoFragment();
break;
case R.id.navigation_contact:
fragment = new ContactFragment();
break;
}
return loadFragment(fragment);
}
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.frame_layout, fragment)
.commit();
return true;
}
return false;
}
public void hideBottomNavigation() {
if (navigation.getVisibility() == View.VISIBLE) {
navigation.setVisibility(View.GONE);
}
}
public void showBottomNavigation() {
if (navigation.getVisibility() == View.GONE) {
navigation.setVisibility(View.VISIBLE);
}
}
}
...
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(getActivity(), "Landscape", Toast.LENGTH_SHORT).show();
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
parentActivity.hideBottomNavigation();
} else {
Toast.makeText(getActivity(), "Portrait", Toast.LENGTH_SHORT).show();
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
parentActivity.showBottomNavigation();
}
}
答案 0 :(得分:0)
显然[' START 100', " SUB =X'1D'", ' LTORG', "=X'1D' BYTE X'1D'", ' RESW 20', " SUB =X'0259'", " ADD =C'12345'", " MUL =X'4356'", " SUB =X'69'", ' LTORG', "=X'0259' BYTE X'0259'", "=C'12345' BYTE C'12345'", "=X'4356' BYTE X'4356'", "=X'69' BYTE X'69'", " ADD =C'05'", ' END EXA']
是parentActivity
,那么您将其分配到哪里?理想情况下,您的片段/活动交互应该通过Android Documentation中概述的技术通过null
进行。
答案 1 :(得分:0)
我看不到您在哪里声明或分配parentActivity
变量。有一种简单的方法可以从片段中调用MainActivity
中的方法。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(getActivity(), "onConfigurationChanged", Toast.LENGTH_SHORT).show();
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(getActivity(), "Landscape", Toast.LENGTH_SHORT).show();
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);
// Comment-out this line
// parentActivity.hideBottomNavigation();
// Add this line
((MainActivity)requireActivity()).hideBottomNavigation();
} else {
Toast.makeText(getActivity(), "Portrait", Toast.LENGTH_SHORT).show();
View decorView = getActivity().getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE;
decorView.setSystemUiVisibility(uiOptions);
// Comment-out this line
// parentActivity.showBottomNavigation();
// Add this line
((MainActivity)requireActivity()).showBottomNavigation();
}
}