无法解析为变量

时间:2012-06-15 11:12:21

标签: java android variables

我有这段代码

filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onNothingSelected(AdapterView<?> parent) {
    }
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        int throwid = (int)id;  
        System.out.println( "from onItemselected" +throwid);
    }
});

我尝试获取throwid变量的值,因此我创建了一个全局变量themerowid,我这样做:

themerowid = throwid;

但我有这个错误:

  

throwid无法解析为变量

如何获得throwid价值?

问题是throwid没有改变值,它总是返回0,我想那个孔代码:

filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    public void onNothingSelected(AdapterView<?> parent) {
    }
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
           throwid = (int)id;  
          System.out.println( "from onItemselected" +throwid);
    }
});

没有被执行,这就是我一直得到0的原因,这段代码中有一个错误。

6 个答案:

答案 0 :(得分:0)

看起来thowid不在可见范围内。试试这个:

    filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onNothingSelected(AdapterView<?> parent) {
        }
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
              themerowid = (int)id;  
              System.out.println( "from onItemselected" + id);
        }
    });

答案 1 :(得分:0)

    public YOURActivty extend Activty{
    int themerowid;  //<--------------------



      public void onCreate(Bundle savedInstanceState) {
      ...

    }


 public void xYZ() {

    filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                            int throwid = (int)id;  
                          System.out.println( "from onItemselected" +throwid);
                    }
                });

    }
}

答案 2 :(得分:0)

全局宣告throwid .....然后使用它......

   int throwid
  filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onNothingSelected(AdapterView<?> parent) {
        }
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                throwid = (int)id;  
              System.out.println( "from onItemselected" +throwid);
        }
    });

答案 3 :(得分:0)

filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onNothingSelected(AdapterView<?> parent) {
            }
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    throwid = (int)id;  
                  System.out.println( "from onItemselected" +throwid);
            }
        });

答案 4 :(得分:0)

尝试这种方式:

public class testingActivity extends Activity 
{

public int throwid;
 /** Called when the activity is first created. */

@Override      
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
  ////YOUR CODE...
          filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onNothingSelected(AdapterView<?> parent) {
            }
            public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                     throwid = (int)id;  
                  System.out.println( "from onItemselected" +throwid);
            }
        });
}

}

答案 5 :(得分:0)

在匿名类(侦听器)之外定义throwId,如下所示:

public class MyClass extends ... implements ... {
    ...
    ...
    int throwId;

    filethemeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onNothingSelected(AdapterView<?> parent) {}

        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            throwId = (int)id;  
            System.out.println( "from onItemselected" +throwid);
        }
    });
}