每当我启动我的应用程序时,即使是现在创建了文件的新模拟器,它仍会执行一些代码,即如果文件已经创建了这个意图,我不明白,它不应该,我没有办法测试,因为我有一部iPhone而不是Android手机,理论上代码不应该被调用,只是第二次加载应用程序。
谢谢!
代码:
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 DANNYISREALLLLYYYGAAYYYYYIMNOTBS;
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);
File file = getBaseContext().getFileStreamPath(FILENAME);
if (file.exists());
Intent i = new Intent(LogIn.this, ChatService.class);
startActivity(i);}
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();
{
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);
if (Fos != null) {
Fos.write(a.getBytes());
Fos.write(b.getBytes());
}
Fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
String u = user.getText().toString();
String p = pass.getText().toString();
Bundle send = new Bundle();
send.putString("key", u);
send.putString("key1", p);
Intent c = new Intent(LogIn.this, logincheck.class);
c.putExtra("key", u);
c.putExtra("key1", p);
startActivity(c);
Toast.makeText(this, "Were signing you in!", Toast.LENGTH_LONG)
.show();
if (!staySignedIn.isChecked()) {
finish();
break;
}
}
}
}
}
}
}
答案 0 :(得分:0)
测试后你有一个分号:
if (file.exists());
如果条件为真,则将其视为要运行的块。只需删除分号就可以了。
答案 1 :(得分:0)
正如jbowes所说,删除分号没有任何意义,你的应用程序应该崩溃 如果你有这样的代码
if (file.exists())
{
Intent i = new Intent(LogIn.this, ChatService.class);
startActivity(i);
}
如果它仍然崩溃告诉我抛出什么异常