首页 > 解决方案 > 根据条件显示不同的布局

问题描述

在我的情况下,我有两个布局,但我检查了 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();
}

标签: javaandroid

解决方案


onCreate()您的第一个语句中应该是setContentView()然后您需要请求读取外部存储的权限,然后如果您已授予权限,则检查文件是否存在。然后根据条件显示您的正确布局或使用Intent导航到另一个活动


推荐阅读