findViewById在上下文之外设置时给我一个问题

时间:2014-03-12 13:11:05

标签: java android eclipse

我在Main.java AndroidManifest.xml文件的onCreate方法中从Main.java拨打MainView.java我创建Context的新实例并发送{{1} }}。在MainView.java我有一个名为buildView的方法,它返回LinearLayout。在LinearLayout我正在创建SpinnerButton。我给了Spinner一个.setId(1)。在Button我添加了setOnClickListener。在onClick方法中,我使用Spinner引用了findViewById(1)。这是我收到错误的地方。 Eclipse要求我创建一个名为findViewById的方法。

我复制了我在我的主onCreate方法中的代码并将其移至MainView.java,因此我可以决定在开始时要加载哪个View。我的想法是FooView.javaBlahView.java所以当应用程序启动时,它会决定什么?View.java加载。

以下是MainView.java

中的代码
    public LinearLayout buildView(){
    this.datasource = new LocationDataSource(context);
    this.datasource.open();

    LinearLayout llmain = new LinearLayout(context);
    llmain.setOrientation(LinearLayout.VERTICAL);
    llmain.setGravity(Gravity.CENTER_HORIZONTAL);

    LinearLayout llcore = new LinearLayout(context);
    llcore.setOrientation(LinearLayout.VERTICAL);
    llcore.setLayoutParams(new LayoutParams(600,-2));

    TextView tv = new TextView(context);
    String tx = "Select Location";
    tv.setText(tx);
    tv.setPadding(0, 25, 0, 0);
    llcore.addView(tv);

    LinearLayout llTemp = new LinearLayout(context);
    llTemp.setOrientation(LinearLayout.HORIZONTAL);
    llTemp.setLayoutParams(new LayoutParams(600,-2));
    llTemp.setPadding(0, 15, 0, 0);
    Spinner s = new Spinner(context);
    s.setId(1);
    s.setLayoutParams(new LayoutParams(450,80));
    final List<SpinnerObject> list = this.datasource.getLocation();
    final ArrayAdapter<SpinnerObject> adapter = new ArrayAdapter<SpinnerObject>(context, android.R.layout.simple_spinner_dropdown_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);
    llTemp.addView(s);

    Button b = new Button(context);
    b.setText("Select");
    b.setLayoutParams(new LayoutParams(150,80));
    b.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
            //need help here
            //need help here
            //need help here
            //need help here
            //need help here


            Spinner spin = (Spinner)findViewById(1);
            //String str = Integer.toString(( (SpinnerObject) spin.getSelectedItem () ).getId () );

        /*  if(spin.getSelectedItemPosition() == 0){
                Messages message = new Messages(context);
                message.alert("ERROR", "Selecting a location is required!");
            }else{
                //loadVehicle();
            }*/
        }

    });
    llTemp.addView(b);      
    llcore.addView(llTemp);

    llmain.addView(llcore);
    return llmain;
}

1 个答案:

答案 0 :(得分:1)

尝试

 Spinner spin = (Spinner)context.findViewById(1);

或者在点击之前获得Spiner的最终引用并按原样重复使用

final tempSpinner = s;
Button b = new Button(context);
b.setText("Select");
b.setLayoutParams(new LayoutParams(150,80));
b.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View arg0) {

    //reuse tempSpinner

   }