Android 从一个类到另一个类的访问按钮

Android 从一个类到另一个类的访问按钮,android,Android,我在.Class中声明了一个按钮,并将其设置为禁用 public static Button s1=(Button)findViewById(R.id.sn1); s1.setEnabled(false); 现在我想在另一个类(B.class)中启用此按钮 我尝试通过给出 按钮s2=A.s1; 但是它抛出了一个错误,即“无法从类型活动对非静态方法findViewById(int)进行静态引用” 请帮我改正这个错误 `First Class: public class Mapp

我在.Class中声明了一个按钮,并将其设置为禁用

public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
现在我想在另一个类(B.class)中启用此按钮 我尝试通过给出 按钮s2=A.s1; 但是它抛出了一个错误,即“无法从类型活动对非静态方法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)
        {

        }
        }
`头等舱:
公共类映射器扩展活动实现OnClickListener
{
公共静态int sng=0,c=0;
公共静态双lat;
公共静态双lon;
公共字符串placenametemp;
公共包savedInstanceState;
公共静态int chk=MapMe.check;
LocationManager locman1=MapMe.locman;
MyOwnLocationOverlay mylocover=MapMe.myLocationOverlay;
公共媒体播放器msong=MapMe.mp;
***公共按钮s1=(按钮)findViewById(R.id.sn1)***
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//为所有按钮添加单击侦听器
查看第一个按钮=findViewById(R.id.geocode_按钮);
firstButton.setOnClickListener(这个);
查看第二个按钮=findViewById(R.id.latlong\u按钮);
secondButton.setOnClickListener(此);
查看第三个按钮=findViewById(R.id.presentLocation_按钮);
setOnClickListener(this);
View selectsong=findviewbyd(R.id.song\u select);
选择Song.setOnClickListener(此选项);
s1.setOnClickListener(此);
s1.设置启用(假);
}
}
二等舱:
公共类MapMe扩展MapActivity实现LocationListener
{
按钮s1=(按钮)findViewById(R.id.sn1);
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\u NO\u TITLE);//抑制标题栏以提供更多空间
setContentView(R.layout.mapme);
updategpsrefs();
//设置位置管理器以确定手机的当前位置
locman=(LocationManager)getSystemService(Context.LOCATION\u服务);
//GPS状态的侦听器。。。
final GpsStatus.Listener onGpsStatusChange=new gpssstatus.Listener(){
公共无效onGpsStatusChanged(内部事件){
开关(事件){
案例GpsStatus.GPS\u事件\u开始:
//开始。。。
打破
案例GpsStatus.GPS\u事件\u第一个\u修复:
//第一次修复。。。
Toast.makeText(MapMe.this,“GPS有第一个修正”,
Toast.LENGTH_LONG).show();
打破
案例GpsStatus.GPS\u事件\u停止:
//停止。。。
打破
案例GpsStatus.GPS\事件\卫星\状态:
//卫星更新
打破
}
GpsStatus status=locman.getGpsStatus(null);
Iterable satlist=status.getSatellites();
}
};
公共空间fu(双a、双b、双c、双d)
{
如果((val==0)和(&(val1==0))
{
mp.reset();
试一试{
检查=1;
系统输出打印项次(“匹配”);
mp.setDataSource(link1);
mp.prepare();
mp.start();
Toast.makeText(MapMe.this,“playing”,Toast.LENGTH_LONG.show();
System.out.println(“歌曲按钮1”);
***s1.设置启用(真)***
System.out.println(“歌曲按钮”);
}
捕获(例外e)
{
}
}
`A类内部:

public static Button s1=(Button)findViewById(R.id.sn1);
s1.setEnabled(false);
B类内部:

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

已编辑

在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);

    }
}

尝试一下。可能会对您有所帮助。

您在错误的位置调用了findViewById()。您应该从一些非静态方法(如onCreate())中调用它。此方法即findViewById()是非静态的,因此您不能以您提到的方式调用它。因此,您应该将变量声明为静态,然后在onCreate()中设置其值这项活动的目的

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

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

我仍然收到错误,无法从类型活动中静态引用非静态方法findViewById(int)“你能把你在类a和类B中所做的代码放在这里吗?我添加了代码映射器。类我声明了按钮s1,并将其设置为false。我想在函数fu()下的Mapme.class中启用它;