不能引用局部变量

时间:2014-10-20 18:06:32

标签: java android

我是Android编程世界的新手,我想在点击按钮时做一些事情。这是我到目前为止所做的:

public class MainActivity extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        setContentView(R.layout.activity_main);
    Button button = (Button) findViewById(R.id.button1);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {


        Spinner s1,s2,s3,s4,s5,s6,s7;
        TextView txt3,txt4,txt5,txt6,txt7,txt8,txt9;
        EditText ed1;

        s7 = (Spinner) findViewById(R.id.spinner7);
        s1 = (Spinner) findViewById(R.id.spinner1);
        s2 = (Spinner) findViewById(R.id.spinner2);
        s3 = (Spinner) findViewById(R.id.spinner3);
        s4 = (Spinner) findViewById(R.id.spinner4);
        s5 = (Spinner) findViewById(R.id.spinner5);
        s6 = (Spinner) findViewById(R.id.spinner6);
        txt4 = (TextView) findViewById(R.id.textView4);
        txt3 = (TextView) findViewById(R.id.textView3);
        txt5 = (TextView) findViewById(R.id.textView5);
        txt6 = (TextView) findViewById(R.id.textView6);
        txt7 = (TextView) findViewById(R.id.textView7);
        txt8 = (TextView) findViewById(R.id.textView8);
        txt9 = (TextView) findViewById(R.id.textView9);


        ed1 = (EditText) findViewById(R.id.editText1);


        s7.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val = s7.getSelectedItem().toString();
                            txt4.setText(val);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s2.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val2 = s2.getSelectedItem().toString();
                            txt5.setText(val2);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s3.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val3 = s3.getSelectedItem().toString();
                            txt6.setText(val3);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s1.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val1 = s1.getSelectedItem().toString();
                            txt3.setText(val1);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s5.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val5 = s5.getSelectedItem().toString();
                            txt7.setText(val5);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s6.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val6 = s6.getSelectedItem().toString();
                            txt8.setText(val6);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });
        s4.setOnItemSelectedListener(
                new OnItemSelectedListener() {
                    public void onItemSelected(
                            AdapterView<?> parent, View view, int position, long id) {

                            String val7 = s4.getSelectedItem().toString();
                            txt9.setText(val7);

                    }


                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });




    }}    //Syntax error, insert ";" to complete Statement
}}

但是,代码中的所有变量(s1,s2,txt1,txt2,...)都会出现错误&#34;无法引用封闭范围中定义的非最终局部变量[var]&#39 ;&#39;我收到错误&#34;语法错误,插入&#34 ;;&#34;完成声明&#34;在代码上指明的地方。任何人都可以帮我解决这个问题吗?

谢谢你!

5 个答案:

答案 0 :(得分:0)

你不能,因为有局部变量,你从anonymous class调用它们;你只需将它们声明为类的属性。

public class MainActivity extends Activity {
    Spinner s1,s2,s3,s4,s5,s6,s7;
    TextView txt3,txt4,txt5,txt6,txt7,txt8,txt9;
    // The rest of your code

答案 1 :(得分:0)

将局部变量声明为类变量,然后还需要在其末尾正确完成OnClickListener()方法。您需要在语法错误处使用右括号和分号关闭方法。

答案 2 :(得分:0)

三点建议:

  1. 与代码中的格式和缩进保持一致。

  2. 使用数组。

  3. 创建一个OnItemClickListener并将其重复用于所有微调器。

答案 3 :(得分:0)

如果必须访问匿名类之外的变量,则必须为final。 你的代码

public class MainActivity extends Activity {
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        final Spinner s1,s2,s3,s4,s5,s6,s7;
        final TextView txt3,txt4,txt5,txt6,txt7,txt8,txt9;
        final EditText ed1;

        s7 = (Spinner) findViewById(R.id.spinner7);
        s1 = (Spinner) findViewById(R.id.spinner1);
        s2 = (Spinner) findViewById(R.id.spinner2);
        s3 = (Spinner) findViewById(R.id.spinner3);
        s4 = (Spinner) findViewById(R.id.spinner4);
        s5 = (Spinner) findViewById(R.id.spinner5);
        s6 = (Spinner) findViewById(R.id.spinner6);
        txt4 = (TextView) findViewById(R.id.textView4);
        txt3 = (TextView) findViewById(R.id.textView3);
        txt5 = (TextView) findViewById(R.id.textView5);
        txt6 = (TextView) findViewById(R.id.textView6);
        txt7 = (TextView) findViewById(R.id.textView7);
        txt8 = (TextView) findViewById(R.id.textView8);
        txt9 = (TextView) findViewById(R.id.textView9);


        ed1 = (EditText) findViewById(R.id.editText1);

        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {






                s7.setOnItemSelectedListener(
                        new AdapterView.OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val = s7.getSelectedItem().toString();
                                txt4.setText(val);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s2.setOnItemSelectedListener(
                        new AdapterView.OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val2 = s2.getSelectedItem().toString();
                                txt5.setText(val2);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s3.setOnItemSelectedListener(
                        new OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val3 = s3.getSelectedItem().toString();
                                txt6.setText(val3);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s1.setOnItemSelectedListener(
                        new OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val1 = s1.getSelectedItem().toString();
                                txt3.setText(val1);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s5.setOnItemSelectedListener(
                        new OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val5 = s5.getSelectedItem().toString();
                                txt7.setText(val5);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s6.setOnItemSelectedListener(
                        new OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val6 = s6.getSelectedItem().toString();
                                txt8.setText(val6);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                s4.setOnItemSelectedListener(
                        new AdapterView.OnItemSelectedListener() {
                            public void onItemSelected(
                                    AdapterView<?> parent, View view, int position, long id) {

                                String val7 = s4.getSelectedItem().toString();
                                txt9.setText(val7);

                            }


                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });




            }});    //Syntax error, insert ";" to complete Statement
    }
}

答案 4 :(得分:0)

虽然其他答案建议你制作变量类变量,但还有另一种解决第一个错误的方法:在声明中添加final

final Spinner s1,s2,s3,s4,s5,s6,s7;
final TextView txt3,txt4,txt5,txt6,txt7,txt8,txt9;

当你在声明中添加final并且你没有给它们初始值时,Java会让你分配给它们一次,但是这些值是&#34;固定的&#34;你不能再分配给他们了。如果他们从您使用的匿名类型中引用final变量,则必须使用这些变量。 (这是因为Java希望确保变量在创建监听器对象后不会发生变化。如果它们可以改变,那么可能会混淆&#34;它是否应该使用变量具有的值什么时候创建了监听器,或者它应该使用最新值?&#34;)

一般情况下,最好在变量使用的最里面范围内声明变量。这就是为什么我不喜欢简单地让它们成为类变量的解决方案。该解决方案摆脱了错误,但不如清洁代码&#34;透视。而且因为变量final变得容易,所以不必将它们作为类变量,除非你真的打算让它们成为{{1}的一部分国家。

MainActivity错误的原因是,在您的代码段的最顶部,您说

;

由于您的监听器中有如此多的代码,因此很容易忘记语句的生成和结束位置。但是上面的语句必须具有与任何其他语句一样的正确语法,这意味着它必须具有

形式
button.setOnClickListener(new View.OnClickListener() { 
    --followed by lots and lots of code-- 

也就是说,你必须得到一个结束的大括号,然后是button.setOnClickListener(new View.OnClickListener() { [code] }); 之前与)匹配的结束(,后面跟一个分号。

P.S。 new听众中有很多重复的代码;一旦你开始工作,你可能想要简化一些事情。可以编写一个创建并返回监听器的私有方法:

onSelectedItem

private AdapterView.OnItemSelectedListener listenerForSpinner(final Spinner s, final TextView t) { return new AdapterView.OnItemSelectedListener() { public void onItemSelected( AdapterView<?> parent, View view, int position, long id) { String val = s.getSelectedItem().toString(); t.setText(val); } public void onNothingSelected(AdapterView<?> parent) { } }; }

SetOnClickListener

等等,这使您的代码更加简单。 (但正如已经建议的那样,使用数组会更加简化。)