0

私のアプリには、閲覧ページとホームページがあります。設定ページでパスワードを入力したいのですが、[OK] ボタンを押すとファイルに読み込まれます。ホームページに戻りたいです。

私が試したコードは.....

    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 に答える