0
中執行我需要在註冊期間將註冊憑證寫入文件,並且在登錄期間將檢查該文件的憑證。但在註冊按鈕上單擊什麼都沒有執行。文件寫入沒有在android
清單文件包含此權限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
的registration.java部分:
reg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String user=username.getText().toString();
String pwd=password.getText().toString();
String cpwd=confirm.getText().toString();
if(user.equals("")||pwd.equals("")||cpwd.equals("")){
Toast.makeText(getApplicationContext(), "Fields cannot be Blank", Toast.LENGTH_SHORT).show();
}
else if(pwd.equals(cpwd)){
String data=user+","+pwd+":";
try {
FileOutputStream fout = new FileOutputStream(Environment.getExternalStorageDirectory()+"/myapp/userregister.txt/", true);
fout.write(data.getBytes());
Toast.makeText(getApplicationContext(), "Succesfully Register", Toast.LENGTH_SHORT).show();
Intent a=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(a);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if(!pwd.equals(cpwd)){
Toast.makeText(getApplicationContext(), "Password not match.. Try again!", Toast.LENGTH_SHORT).show();
}
}
});
在LoginActivity.java
if(username.equals("") || password.equals("")){
Toast.makeText(getApplicationContext(), "Fields Cannot be Blank", Toast.LENGTH_SHORT).show();
}else{
String reply=FileManipulator.authenticateUser(username, password);
if(reply.equals("user")){
Intent gotoNextActivity=new Intent(getApplicationContext(),HomeActivity.class);
startActivity(gotoNextActivity);
}else{
Toast.makeText(getApplicationContext(), "Invalid User.... Try again",Toast.LENGTH_SHORT).show();
}
}
FileManipulator類的authenticateUser功能:
public static String authenticateUser(String user, String pass){
String data="";
String data1 = "";
try{
FileInputStream fin=new FileInputStream(Path.userregister);
byte[] b=new byte[fin.available()];
fin.read(b);
data=new String(b);
String st=user+","+pass+":";
if(data.contains(st)){
data1="user";
}
}catch(Exception e){
e.printStackTrace();
}
return data1;
}
日誌貓顯示
09-02 05:48:06.292: D/dalvikvm(1964): GC_CONCURRENT freed 240K, 12% free 2838K/3200K, paused 5ms+32ms, total 177ms
09-02 05:48:06.802: I/Choreographer(1964): Skipped 36 frames! The application may be doing too much work on its main thread.
09-02 05:48:10.362: I/Choreographer(1964): Skipped 32 frames! The application may be doing too much work on its main thread.
09-02 05:48:20.872: W/System.err(1964): java.io.FileNotFoundException: /mnt/sdcard/myapp/userregister.txt: open failed: EACCES (Permission denied)
09-02 05:48:20.872: W/System.err(1964): at libcore.io.IoBridge.open(IoBridge.java:416)
09-02 05:48:20.882: W/System.err(1964): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
09-02 05:48:20.882: W/System.err(1964): at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
09-02 05:48:20.882: W/System.err(1964): at com.example.text.RegActivity$1.onClick(RegActivity.java:49)
09-02 05:48:20.892: W/System.err(1964): at android.view.View.performClick(View.java:4204)
09-02 05:48:20.892: W/System.err(1964): at android.view.View$PerformClick.run(View.java:17355)
09-02 05:48:20.902: W/System.err(1964): at android.os.Handler.handleCallback(Handler.java:725)
09-02 05:48:20.912: W/System.err(1964): at android.os.Handler.dispatchMessage(Handler.java:92)
09-02 05:48:20.912: W/System.err(1964): at android.os.Looper.loop(Looper.java:137)
09-02 05:48:20.912: W/System.err(1964): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-02 05:48:20.922: W/System.err(1964): at java.lang.reflect.Method.invokeNative(Native Method)
09-02 05:48:20.922: W/System.err(1964): at java.lang.reflect.Method.invoke(Method.java:511)
09-02 05:48:20.922: W/System.err(1964): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-02 05:48:20.922: W/System.err(1964): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-02 05:48:20.922: W/System.err(1964): at dalvik.system.NativeStart.main(Native Method)
09-02 05:48:20.932: W/System.err(1964): Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
09-02 05:48:20.942: W/System.err(1964): at libcore.io.Posix.open(Native Method)
09-02 05:48:20.942: W/System.err(1964): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
09-02 05:48:20.952: W/System.err(1964): at libcore.io.IoBridge.open(IoBridge.java:400)
09-02 05:48:20.952: W/System.err(1964): ... 14 more
我是一個新手,Android和java..please幫我找到錯誤。
你有權限在清單中嗎? – 2014-09-02 05:44:06
我懷疑日誌顯示了實際的異常,而不僅僅是它發生了什麼......所以,有什麼例外? – 2014-09-02 05:45:06
檢查你的清單你有沒有使用 給出完整的日誌,以便我們可以檢查錯誤 –
2014-09-02 05:45:22