java - 根据条件显示不同的布局
问题描述
在我的情况下,我有两个布局,但我检查了 mainactivity 的 oncreate,如果创建了文件,则打开 indexpage,activity_indexpage2.xml,但我不能不打印此结果来检查错误。
protected void onCreate(Bundle savedInstanceState) {
try {
contentfile = read();
} catch (IOException e) {
e.printStackTrace();
}
if(contentfile!=null
){
Intent intent = new Intent(MainActivity.this, indexpage.class);
setContentView(R.layout.activity_indexpage2);
startActivity(intent);
}
`super.onCreate(savedInstanceState);`
public String read() throws IOException {
FileInputStream input = this.openFileInput(File_NAME);
byte[] temp = new byte[1024];
StringBuffer stringBuffer = new StringBuffer("");
int len = 0;
while ((len = input.read(temp)) > 0) {
stringBuffer.append(new String(temp, 0, len));
}
input.close();
return stringBuffer.toString();
}
解决方案
在onCreate()
您的第一个语句中应该是setContentView()然后您需要请求读取外部存储的权限,然后如果您已授予权限,则检查文件是否存在。然后根据条件显示您的正确布局或使用Intent导航到另一个活动
推荐阅读
- mongodb - 如何获取所有字段的最近 5 天数据
- php - 如何在 cakephp 3 中为插件添加视图类?
- c# - 添加新子对象时 C# 更新父对象
- mysql - 如何解决以下死锁问题?
- azure-devops - 从 VSTS 将包推送到 Octopus Deploy 失败
- javascript - 如何制作单个 React 组件,然后根据 URL 填充信息?
- javascript - ReactJs 从一个类访问“状态”?
- copy - 将文件夹中的所有文件复制到另一个文件夹
- java - 在 IntelliJ getResources() 中编译 JavaFx 应用程序返回 null
- php - PHP 将 SVG 转换为 Jpg 缺少元素