在我的情况下,我有两个布局,但是我检查了 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();
}
答案 0 :(得分:0)
在 onCreate()
中,您的第一条语句应该是 setContentView(),然后您需要 request permission 读取外部存储,然后如果您已获得权限,则检查该文件是否存在。然后根据条件显示您的正确布局或使用 Intent