2012-08-23 42 views
0

我已經在A.Class中聲明瞭一個按鈕,並且已將其設置爲禁用。accesing從一個類到另一個按鈕

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) 
     { 

     } 
     } 

`

回答

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

    } 
} 

嘗試了這一點。可能幫助你。

+0

時仍然得到錯誤不能使從類型活動」 – Sindu

+0

你可以把這裏的靜態引用非靜態方法findViewById(INT)什麼樣的代碼你做你的A類和B類裏面? –

+0

我已經添加了代碼Mapper.class,我已經聲明瞭Button s1,並且我已經將它設置爲false。我想在Mapme.class中的函數fu()下啓用它; – Sindu

0

您在錯誤的位置調用findViewById()。你應該從onCreate()這樣的非靜態方法中調用它。這種方法,即findViewById()是非靜態的,所以你不能像你提到的那樣調用它。所以你應該聲明你的變量是靜態的,然後在activity的onCreate()中設置它的值。

爲什麼不在B類中調用findViewById(),然後禁用該視圖。只要視圖在當前加載的佈局中可用,B類中的以下代碼應該可以完成您的工作。

Button s1=(Button)findViewById(R.id.sn1); 
s1.setEnabled(false); 
相關問題