2012-04-03 20 views
0

我有下面的代碼(它不是完整的代碼,但其餘無關緊要)。我試圖根據用戶在Alertdialog中選擇的內容將boolean「ignoreLength」設置爲true或false。然而,當代碼是這樣的,我得到這個錯誤:無法更改AlertDialog中的最終變量

「不能指非最終變量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;外部和之前的點擊監聽器。並且因爲需要更改而不能完成它 – 2012-04-03 19:32:59

+0

當我在點擊偵聽器之外改變它時,它會給出相同的錯誤。 – Grawl 2012-04-03 19:34:14

回答

1

首先根據你的問題。 「無法更改AlertDialog中的最終變量」是錯誤的

正如你明確指出的那樣,最後的變量永遠不會被更改,因爲它是最終的,並且在創建時已經聲明。

同樣在你的情況下boolean ignoreLength = false;在click listener之前和之前聲明變量,並且不會使它成爲final。因爲您將來需要更新ignoreLength值。

+0

我已經添加了完整的代碼與該更改。它不能解決問題;錯誤是一樣的。 – Grawl 2012-04-03 19:47:40

+0

通過在OnClickListener中定義它,但在實際的onClick之前,我擺脫了錯誤。 button_generate.setOnClickListener(新View.OnClickListener(){ \t 布爾ignoreLength = FALSE; \t 公共無效的onClick(視圖v){ – Grawl 2012-04-03 19:52:26

+0

聲明爲私有布爾ignoreLength = FALSE;在PassGenActivity,因爲它是自己的變量。希望你明白我的意思是 – 2012-04-03 19:53:49

1

您不能在內部類型中重新分配Java中的局部變量。 More here.

有兩種方法可以解決您的問題。 1)忽略大小寫字段 - 你實際上可以使用匿名的OnClickListener類型,並給它一個ignoreLength字段,但我想一般你會希望它是最高級別的字段

2 )
<superuglyhack>

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

</superuglyhack>

1

你可以移動的onClick之外boolean ignoreLength;聲明,使其匿名在的成員變量ClickListener類。另外,你可以把變量放在某個持有它的東西里(像一個包含一個項目的數組),然後用這種方式更新它。 ignoreLength[0] = true;