次のコードがあります(完全なコードではありませんが、残りは関係ありません)。私はブール値 "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());
}
});
}
}
decalare boolean ignoreLength = false;クリックリスナの外とその前。変更する必要があるので、最後にしないでください –
クリックリスナの外でそれを変更すると、同じエラーが発生します。 – Grawl