繁体   English   中英

Android Studio-首次启动应用程序时如何将数据加载到应用程序中?

[英]Android Studio - How to load data into the app when the app first launch?

我有一个包含某些片段的应用程序,用于刷卡。 并且在其中一个片段上,用户可以输入自己的名称,然后输入名称,应用程序将使用FileOutputStream保存其数据。

然后,每次用户启动应用程序时,我都想使用FileInputStream来获取数据并显示数据。

问题是,我认为将FileInputStream实现到public View onCreateView()中是错误的,因为在将代码实现到public View onCreateView()之后,每次尝试运行它时,我的应用都会崩溃,没有错误,没有任何显示在事件日志上,它只是崩溃。 下面是我的片段,java

package layout;


import ...;

public class page1 extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final EditText text = (EditText) getView().findViewById(R.id.p1Name);
    try {
        FileInputStream fileinputStream = getActivity().openFileInput("page1name.txt");
        InputStreamReader inputstreamReader = new InputStreamReader(fileinputStream);
        BufferedReader bufferedReader = new BufferedReader(inputstreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        String lines;
        while ((lines = bufferedReader.readLine())!= null){
            stringBuffer.append(lines+"\n");
        }
        text.setText(stringBuffer.toString());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return inflater.inflate(R.layout.page1, container,
            false);


}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    final EditText text = (EditText) getView().findViewById(R.id.p1Name);


    text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String p1sName = s.toString();
            try {
                FileOutputStream fileoutputStream = getActivity().openFileOutput( "page1name.txt", Context.MODE_PRIVATE);
                fileoutputStream.write(p1sName.getBytes());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });
}

}

因此,我想知道,应该在哪里实现FileInputStream才能使其正常工作? 谢谢!

您可以保存首选项,而不是使用FileInputStream

喜欢

public static void saveName(Context context, String name) {
    SecureSharedPreferences sharedPreferences = new SecureSharedPreferences(context);
    SecureSharedPreferences.Editor ed = sharedPreferences.edit();
    ed.putString("name", name);
    ed.commit();
}

 public static String getName(Context context) {
    SecureSharedPreferences sharedPreferences = new SecureSharedPreferences(context);
    String name = sharedPreferences.getString("name", null);
    return name;
}

如果要使用FileInputStream保存在文件中,则需要使用AsyncTask,在代码中,您需要在受限的主线程中执行后台操作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM