Android 将文件保存到内部存储

Android save file to internal storage

您好,我有一个应用程序在开始时会显示登录按钮,当我单击它时,它会在输入用户名和密码时显示一个对话框,然后我单击“确定”,它应该会将用户名保存到我内部存储中的一个文件中。但是当我按确定时,我的应用程序崩溃并在 logcat.

中出现错误

这里是主类:

    public class MainActivity extends ActionBarActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }

        public void loginMethod(View v){
            MyDialog dialog = new MyDialog();
            dialog.show(getSupportFragmentManager(),"my_dialog");
        }

        public void writeToFile(String data) {
            try {
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));
                outputStreamWriter.write(data);
                outputStreamWriter.close();
            }
            catch (IOException e) {
                Log.e("Exception", "File write failed: " + e.toString());
            }
        }

    }


Here DialogClass:

    public class MyDialog extends DialogFragment{

    LayoutInflater inflater;
    View v;
    EditText user, password;

    MainActivity main;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        inflater = getActivity().getLayoutInflater();
        v = inflater.inflate(R.layout.my_dialog_layout,null);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                user = (EditText) v.findViewById(R.id.username);
                password = (EditText) v.findViewById(R.id.password);
                if (user.getText().toString().equals("hello") && password.getText().toString().equals("123")) {   //here it checks if username and password are "hello" and "123"; if true it should save into file

                    main.writeToFile(user.getText().toString());
                    Intent i = new Intent("user_activity");
                    startActivity(i);
                    Toast.makeText(getActivity(), "Welcome: " + user.getText().toString(), Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
                }
            }
        }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });

        return builder.create();
    }
}

这是logcat:

    01-07 10:53:32.000: E/AndroidRuntime(13341): FATAL EXCEPTION: main
01-07 10:53:32.000: E/AndroidRuntime(13341): java.lang.NullPointerException
01-07 10:53:32.000: E/AndroidRuntime(13341):    at com.example.digitpassword2.MyDialog.onClick(MyDialog.java:44)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at android.os.Looper.loop(Looper.java:137)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at android.app.ActivityThread.main(ActivityThread.java:5103)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at java.lang.reflect.Method.invokeNative(Native Method)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at java.lang.reflect.Method.invoke(Method.java:525)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-07 10:53:32.000: E/AndroidRuntime(13341):    at dalvik.system.NativeStart.main(Native Method)

谢谢

这里:

main.writeToFile(user.getText().toString());

NullPointerException 因为 MainActivitymain 实例是 Null

如果你想调用从 ActivityFragment 的方法,那么这样做:

((MainActivity)getActivity()).writeToFile(user.getText().toString());