【问题标题】:App Bypassing the Login-screen, even though it shouldn't应用程序绕过登录屏幕,即使它不应该
【发布时间】:2024-04-27 13:20:01
【问题描述】:

每当我启动我的应用程序时,即使是从创建了现在文件的新模拟器,它仍然会执行一些代码,这表明如果文件已创建执行此意图,我不明白,它不应该,我没办法测试,因为我是iPhone而不是安卓手机,理论上不应该调用代码,只能在第二次加载应用时调用。

谢谢!

代码:

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;
                        }
                }
                 }
             }





            }
        }
    }

【问题讨论】:

    标签: java android file if-statement try-catch


    【解决方案1】:

    测试后有一个分号: if (file.exists()); 如果条件为真,这将算作要运行的块。去掉分号就可以了。

    【讨论】:

    • 什么时候,我这样做应用程序崩溃了。
    • 显然,这段代码有很多问题。在这种情况下,它不应该崩溃,但它根本不应该工作。恐怕原来的问题是个玩笑……
    【解决方案2】:

    正如 jbowes 所说,删除分号没有必要让您的应用程序崩溃 如果你有这种方式的代码

    if (file.exists())
    {
            Intent i = new Intent(LogIn.this, ChatService.class);
            startActivity(i);
    }
    

    如果它仍然崩溃,请告诉我抛出了什么异常

    【讨论】: