2012-04-03 8 views
0

次のコードがあります(完全なコードではありませんが、残りは関係ありません)。私はブール値 "ignoreLength"をtrueまたはfalseに設定しようとしていますが、ユーザーがAlertdialogで選択した内容に基づいています。しかし、コードは次のようであるとき、私はこのエラーを取得する:私はそれが最終的に作るときAlertDialog内の最終変数を変更できません

「を異なる方法で定義されたインナークラス内で非final変数ignoreLengthを参照できない」、それが変わります

「それが囲む形で定義されているので、最終的なローカル変数ignoreLengthは、割り当てることができない」

どのように私はそれが私がignoreLengthを変更することができますすることができます。これに?

package com.grawl.passgen; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class PassGenActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    // Interface -- Default 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    // Interface -- Custom 

    final Button button_generate = (Button) findViewById(R.id.button_generate); 
    final EditText text_pass = (EditText) findViewById(R.id.textPassWord); 
    final EditText edit_length = (EditText) findViewById(R.id.editLength); 

    // Set up Arrays 

    final String[] lowerCase; 
    final String[] upperCase; 
    final String[] numbers; 
    final String[] symbols; 

    // Fill Arrays 

    createArray characters = new createArray(); 
    lowerCase = characters.getArrayLower(); 
    upperCase = characters.getArrayUpper(); 
    numbers = characters.getArrayNumbers(); 
    symbols = characters.getArraySymbols(); 


    // Pressing the button WOOOSH! 

    button_generate.setOnClickListener(new View.OnClickListener() { 

     **boolean ignoreLength = false;** 

     public void onClick(View v) { 

      // Set up parameters 

      boolean lowerCaseEnabled = true; // needs interface option 
      boolean upperCaseEnabled = true; // needs interface option 
      boolean numbersEnabled = true; // needs interface option 
      boolean symbolsEnabled = true; // needs interface option 

      // Set up length based on input from EditText 

      int length = 0; 

      try { 
       length = Integer.parseInt(edit_length.getText().toString()); 
      } catch(NumberFormatException nfe) { 
       Toast.makeText(PassGenActivity.this, "Can't parse " + nfe, Toast.LENGTH_LONG).show(); 
      } 

      if (length < 1) { 
       length = 1; 
       edit_length.setText("1"); 
       Toast.makeText(PassGenActivity.this, "Password length can't be less than 1, set it to 1.", Toast.LENGTH_LONG).show(); 
       }; 

      if (length > 100) { 
       AlertDialog.Builder alert = new AlertDialog.Builder(PassGenActivity.this); 

       alert.setTitle("Warning"); 
       alert.setMessage("You are trying to create quite a long password, are you sure?"); 

       alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         ignoreLength = true; 
         } 
        }); 


       alert.setNegativeButton("No", null); 
       alert.show(); 
      } 

      Password password = new Password(); 
      password.fillPassword(lowerCase, upperCase, numbers, symbols); 

      // Generate password 

      password.setPassword(lowerCaseEnabled, upperCaseEnabled, numbersEnabled, symbolsEnabled, length); 
      text_pass.setText(password.getPassword()); 
     } 
    }); 

    } 

} 
+0

decalare boolean ignoreLength = false;クリックリスナの外とその前。変更する必要があるので、最後にしないでください –

+0

クリックリスナの外でそれを変更すると、同じエラーが発生します。 – Grawl

答えて

1

まずはご質問のとおりです。 "AlertDialog内の最終変数を変更できません" は、が間違っています。

の最終という変数は、最終段階では変更できず、作成時に既に宣言されていることを明確にしています。

boolean ignoreLength = false;また、クリックリスナの外側とその前に変数を宣言し、それを最終的にしないでください。将来ignoreLength値を更新する必要があるため。

+0

その変更を加えた完全なコードを追加しました。それは問題を解決しません。エラーは同じです。 – Grawl

+0

エラーをOnClickListener内で定義することで、実際のonClickの前にエラーを取り除きました。偽 button_generate.setOnClickListener(新View.OnClickListener(){ \t ブールignoreLength =; \t ます。public void onClickの(ビューV){ – Grawl

+0

は、プライベートブールignoreLength =偽としてそれを宣言する。PassGenActivityで、それは自身の変数だとして。あなたが私が意味するものを得ることを望む。 –

1

Javaのローカル変数を内部型から再割り当てすることはできません。 More here.

問題を解決するには2通りの方法があります。 1)何かのフィールドを無視する - 実際に匿名のOnClickListenerタイプを使用してigno​​reLengthフィールドを与えることができますが、通常はトップレベルクラスのフィールドにしたいと思っています。

2 )
<superuglyhack>

final boolean[] ignoreLengthHolder= new boolean[]{ false }; 
... 
ignoreLengthHolder[0] = true 

</superuglyhack>

1

あなたはそれ、匿名でのメンバ変数にするのonClickの外boolean ignoreLength;宣言を移動することができますClickListenerクラス。

また、変数を保持するもの(1つの項目を持つ配列のようなもの)に変数を置き、そのように更新することもできます。 ignoreLength[0] = true;

関連する問題