从一个类访问Button到另一个类

时间:2012-08-23 11:35:50

标签: android

我在A.Class中声明了一个按钮并设置了禁用。

public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);

现在我想在另一个类中启用此按钮(B.class) 我尝试通过给予启用此按钮  按钮s2 = A.s1; 但它抛出一个错误,说“不能从类型Activity中对静态方法findViewById(int)进行静态引用”

请帮助我纠正错误

    `First Class:

    public class Mapper extends Activity implements OnClickListener 
    {
public static int sng=0,c=0;
public static double lat;
public static double lon;
public String placenametemp;
public Bundle savedInstanceState;
public static int chk= MapMe.check;
 LocationManager locman1= MapMe.locman;
 MyOwnLocationOverlay mylocover=MapMe.myLocationOverlay;
public MediaPlayer msong=MapMe.mp;
***public Button s1=(Button)findViewById(R.id.sn1);***



@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

 // Add Click listeners for all buttons
    View firstButton = findViewById(R.id.geocode_button);
    firstButton.setOnClickListener(this);
    View secondButton = findViewById(R.id.latlong_button);
    secondButton.setOnClickListener(this);
    View thirdButton = findViewById(R.id.presentLocation_button);
    thirdButton.setOnClickListener(this);
    View selectsong=findViewById(R.id.song_select);
    selectsong.setOnClickListener(this);
    s1.setOnClickListener(this);
    s1.setEnabled(false);


    }
 }


Second Class :
    public class MapMe extends MapActivity implements LocationListener 
  {

Button s1=(Button)findViewById(R.id.sn1);
public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);  // Suppress title bar to give more space
    setContentView(R.layout.mapme); 

    updateGPSprefs();

    // Set up location manager for determining present location of phone
    locman = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    // Listener for GPS Status...   
    final GpsStatus.Listener onGpsStatusChange = new GpsStatus.Listener(){
        public void onGpsStatusChanged(int event){
            switch(event){
                case GpsStatus.GPS_EVENT_STARTED:
                    // Started...
                break;
                case GpsStatus.GPS_EVENT_FIRST_FIX:
                    // First Fix...
                    Toast.makeText(MapMe.this, "GPS has First fix",       
                     Toast.LENGTH_LONG).show();
                break;
                case GpsStatus.GPS_EVENT_STOPPED:
                    // Stopped...
                break;
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                    // Satellite update
                break;
            }
            GpsStatus status = locman.getGpsStatus(null);


            Iterable<GpsSatellite> satlist = status.getSatellites();
        }
    };
   public  void fu(double a,double b,double c,double d)
{

    if((val==0)&&(val1==0))
            {
        mp.reset();
        try {

            check=1;
            System.out.println("matched");
            mp.setDataSource(link1);
            mp.prepare();
            mp.start();
            Toast.makeText(MapMe.this, "playing", Toast.LENGTH_LONG).show();
            System.out.println("Song button1");
            ***s1.setEnabled(true);***
           System.out.println("Song button");
        }

        catch(Exception e)
        {

        }
        }

`

2 个答案:

答案 0 :(得分:0)

在A组内:

public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);

B类内部:

你应该写:A.s1.setEnabled(true);

<强> EDITED

在Mapper.java中:

Class Mapper extends Activity{

public static Button s1;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

                s1=(Button)findViewById(R.id.sn1);
                s1.setEnabled(false);

    }
}
Mapme.java里面的

Class Mapme extends Activity{


@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_new);


                Mapper.s1.setEnabled(true);

    }
}

试一试。可能会帮助你。

答案 1 :(得分:0)

您在错误的位置调用findViewById()。你应该在onCreate()之类的非静态方法中调用它。这个方法,即findViewById()是非静态的,所以你不能按照你提到的方式调用它。因此,您应该将变量声明为static,然后在活动的onCreate()中设置其值。

为什么不在B类中调用findViewById()然后禁用视图。只要视图在当前加载的布局中可用,B类中的以下代码就可以完成您的工作。

Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);