0

在我的应用程序中有一个查看页面和一个主页,在设置页面中我想输入密码并在按下确定按钮后将其读取到文件中我想返回主页

我试过的代码是......

    public class SetteingsPage extends Activity 
      {
Button okBtn,resetBtn,cancelBtn;
TextView passwordTxt,passwordTxt2;
String passwordTxtValue,passwordTxtValue2;
String value;
String localDataPassword;
int id=1;
OpenHelper db_obj;
ArrayList<HashMap<String, String>> PASSWORD_DATA = new ArrayList<HashMap<String, String>>();
String FILENAME1 = "password.txt";

 @Override 
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.settingspage);
      db_obj = new OpenHelper(getApplicationContext());

      okBtn = (Button)findViewById(R.id.button1s);

      passwordTxt = (TextView)findViewById(R.id.editText1s);
      passwordTxt2 = (TextView)findViewById(R.id.editText2s);

      resetBtn = (Button)findViewById(R.id.btnreset);
      cancelBtn = (Button)findViewById(R.id.btnCancels);

        passwordTxt.setHint("Enter Password");
        passwordTxt2.setHint("Confirm Password");



      try {
          System.out.println("Enter try block!!!!!!!!!!!!!!!");
            FileInputStream fis = openFileInput(FILENAME1);
            InputStreamReader in = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(in);
            localDataPassword = br.readLine();
            System.out.println("localDataPassword"+localDataPassword);
            System.out.println("dasretetile"+localDataPassword);

        } catch (Exception e) { 

        }


       if(localDataPassword == null || localDataPassword.equals("") || localDataPassword.equals("null"))
       {
           resetBtn.setVisibility(View.INVISIBLE);
          okBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String id1 = Integer.toString(id);
                      passwordTxtValue = passwordTxt.getText().toString();
                      passwordTxtValue2 = passwordTxt2.getText().toString();

                      if(passwordTxt.getText().length()>=5)
                      {
                      if(passwordTxtValue.equals(passwordTxtValue2))
                      {

                            try 
                            {
                                db_obj.password_varification(id1, passwordTxtValue);
                                Log.e("Insert Values"," " );

                            } 
                            catch (Exception e) 
                            {
                            }



                             FileOutputStream fos;
                                try {
                                    fos = openFileOutput(FILENAME1, Context.MODE_PRIVATE);
                                    fos.write(passwordTxtValue.getBytes());
                                    System.out.println("---------Data written to files is:"
                                            + passwordTxtValue);
                                    fos.close();

                                   SetteingsPage.this.finish();

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }




                      } 

                      else
                      {
                          Toast.makeText(getApplicationContext(), "Enter values correctly!!!", Toast.LENGTH_SHORT).show();
                            passwordTxt.setText("");
                            passwordTxt2.setText("");
                      }
                      }
                      else
                      {
                          Toast.makeText(getApplicationContext(), "Should hhave min 5 characters!!!", Toast.LENGTH_SHORT).show();
                            passwordTxt.setText("");
                            passwordTxt2.setText("");
                      }
                }
            });
       }
       else
       {
           okBtn.setVisibility(View.INVISIBLE);
           cancelBtn.setVisibility(View.INVISIBLE);
            passwordTxt.setHint("Old Password");
            passwordTxt2.setHint("New Password");
          resetBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    String oldPassword  = passwordTxt.getText().toString();
                    String newPassword  = passwordTxt2.getText().toString();
                    System.out.println("Old Password Value"+oldPassword);
                    System.out.println("From Reset Button"+localDataPassword);
                    System.out.println("New password"+newPassword);

                     if(passwordTxt.getText().length()>=5)
                     {
                    if(oldPassword.equals(localDataPassword))
                    {
                        FileOutputStream fos;
                            try {
                                fos = openFileOutput(FILENAME1, Context.MODE_PRIVATE);
                                fos.write(newPassword.getBytes());
                                System.out.println("-----stert----Data written to files is:"
                                        + newPassword);
                                fos.close();

                                Intent intent=new Intent(SetteingsPage.this,Home.class);
                                startActivity(intent);
                                finish();

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                    }

                    else
                    {
                        Toast.makeText(getApplicationContext(), "Enter the old password correctly!!!", Toast.LENGTH_LONG).show();
                    }



                    String dummy = Integer.toString(id);
                      db_obj.Deletrowpassword(dummy);


                }
                     else
                     {
                         Toast.makeText(getApplicationContext(), "Should Have Minimum 5 Characters!!!", Toast.LENGTH_SHORT).show();
                            passwordTxt.setText("");
                            passwordTxt2.setText("");
                     }
                }
            });
       }



   }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {

        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
             Intent intent = new Intent(getApplicationContext(),Home.class);
             intent.putExtra("key", "common");
             startActivityForResult(intent, 0);
             finish();
        }
        return super.onKeyDown(keyCode, event);

    }

}

两个代码

       SetteingsPage.this.finish();

    Intent home  = new Intent();
     home.setClass(getApplicationContext(), Home.class);
     startActivity(home);

试过了,但这些都不起作用......

定位输出是..

    01-11 12:39:44.218: E/AndroidRuntime(13506): FATAL EXCEPTION: main
   01-11 12:39:44.218: E/AndroidRuntime(13506): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.example.security/com.example.security.Home}:  java.lang.NullPointerException
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.os.Handler.dispatchMessage(Handler.java:99)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.os.Looper.loop(Looper.java:130)
  01-11 12:39:44.218: E/AndroidRuntime(13506):  at android.app.ActivityThread.main(ActivityThread.java:3687)
4

2 回答 2

0

试试这样。。

直接..之后

TextView txt=(TextView)findViewById(R.id.//);

...添加: if (txt == null) { Log.w("", "TextView is null"); } //

于 2013-01-11T06:44:53.040 回答
0

如果您已经从其中调用过,则不应finish为当前活动调用方法startActivityForResult

也许这会有所帮助:

startActivity(intent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();

如果有帮助,谢谢:)

于 2013-01-11T09:07:03.137 回答