Java 应用程序启动时崩溃,试图查看文件是否存在';s用于登录屏幕

Java 应用程序启动时崩溃,试图查看文件是否存在';s用于登录屏幕,java,android,file,crash,Java,Android,File,Crash,我正在创建一个类来检查文件是否已创建(包含用户名和密码),如果创建了,则会创建一个意图,即转到另一个类来读取数据,并通过FTP再次检查服务器。由于某些原因,我无法让它工作,我尝试了所有的方法,阅读了每一个我能阅读的网页,但没有运气 我的代码: public class LogIn extends Activity implements OnClickListener { Button send; EditText user; EditText pass; CheckBox staySignedI

我正在创建一个类来检查文件是否已创建(包含用户名和密码),如果创建了,则会创建一个意图,即转到另一个类来读取数据,并通过FTP再次检查服务器。由于某些原因,我无法让它工作,我尝试了所有的方法,阅读了每一个我能阅读的网页,但没有运气

我的代码:

public class LogIn extends Activity implements OnClickListener {
Button send;
EditText user;
EditText pass;
CheckBox staySignedIn;
FileOutputStream Fos;
String a;
String b;
String string = a;
String string2 = b;

String FILENAME = "userandpass";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);
    send = (Button) findViewById(R.id.bLogIn);
    user = (EditText) findViewById(R.id.eTuser);
    pass = (EditText) findViewById(R.id.eTpassword);
    staySignedIn = (CheckBox) findViewById(R.id.Cbstay);
    send.setOnClickListener(this);

    if (staySignedIn.isChecked()) {

        String a = user.getText().toString();
        String b = pass.getText().toString();
        File f = new File(FILENAME);
        try {
            Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            Fos.write(string.getBytes());
            Fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        File file = getBaseContext().getFileStreamPath(FILENAME);
        if(file.exists());
        Intent i = new Intent(LogIn.this, ChatService.class);
    }

}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bLogIn:
        if (pass.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else if (user.length() == 0)
            Toast.makeText(this,
                    "Try to type in your username and password again!",
                    Toast.LENGTH_LONG).show();
        else {

            String u = user.getText().toString();
            String p = pass.getText().toString();
            Bundle send = new Bundle();
            send.putString("key", u);
            send.putString("key1", p);
            Intent a = new Intent(LogIn.this, logincheck.class);
            a.putExtra("key", u);
            a.putExtra("key1", p);
            startActivity(a);
            Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
                    .show();
            break;
        }
    }

}
}
日志:

 01-19 11:37:17.601: W/dalvikvm(4411): threadid=1: thread exiting with uncaught  exception (group=0x4001d800)
    01-19 11:37:17.621: E/AndroidRuntime(4411): FATAL EXCEPTION: main
    01-19 11:37:17.621: E/AndroidRuntime(4411): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.gta5news.bananaphone/com.gta5news.bananaphone.LogIn}:  java.lang.NullPointerException
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.os.Handler.dispatchMessage(Handler.java:99)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.os.Looper.loop(Looper.java:123)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at java.lang.reflect.Method.invoke(Method.java:521)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
   01-19 11:37:17.621: E/AndroidRuntime(4411):    
    at dalvik.system.NativeStart.main(Native  Method)
    01-19 11:37:17.621: E/AndroidRuntime(4411): Caused by: java.lang.NullPointerException
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at  com.gta5news.bananaphone.LogIn.onCreate(LogIn.java:55)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    01-19 11:37:17.621: E/AndroidRuntime(4411):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
如果第55行是

Fos.write(string.getBytes());
然后,
Fos
string
未初始化。假设
string
被初始化为
a
,而a本身未初始化,这就解释了这一点。您需要为
字符串
指定一个适当的值

Fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
似乎出于某种原因,openFileOutput返回null,这使得Fos为null,这会引发NullPointerException


或者catch
NullpointerException

根据eclipse Fos.write(string.getBytes())这是第55行;我该怎么做呢?我已经在代码中说过string&string1等于两个编辑文本字段。不,你没有。在进行赋值时,
string=a
a
null
。哦,我明白了,那么我该怎么给string赋值呢?是的!这起作用了,但我的字符串没有保存到文件中,或者文件没有创建。似乎文件没有创建,可能是您没有所需的权限?请参阅此链接,它可能会帮助您获得什么权限?我只有“写入外部驱动器”权限,我想这不是正确的权限,我需要什么权限?
if(Fos != null) { 
 Fos.write(string.getBytes());
 Fos.close();
} check.