2010-10-24 116 views
0

問候語 我正在創建一個服務客戶端,並且以下行給我 錯誤。我想知道如何解決這些...Android服務客戶端代碼

// I have put in the service client stuff below... 

private CheckZone mBoundService; 
private ServiceConnection mConnection = new ServiceConnection() { 
public void onServiceConnected(ComponentName CheckZone, IBinder service) { 
    // This is called when the connection with the service has been 
    // established, giving us the service object we can use to 
    // interact with the service. Because we have bound to a explicit 
    // service that we know is running in our own process, we can 
    // cast its IBinder to a concrete class and directly access it. 
    mBoundService = ((CheckZone.LocalBinder)service).getService(); 

    //This line complians about binding, and states that it can not be resolved. How do I fix this please? 
    Toast.makeText(binding.this, "Connected to CheckZone", Toast.LENGTH_SHORT).show(); 
} 
+0

你一定要重新格式化你的問題。單行復制和粘貼並不是非常有用。 – 2010-10-25 06:52:54

回答

0

我認爲你指的是編譯錯誤?綁定沒有定義?只是擺脫binding.this,只是使用,「這」

0

你的CheckZone服務中有一個LocalBinder嗎?

1

我剛剛遇到了這個問題,使用http://developer.android.com/reference/android/app/Service.html的同樣的本地服務示例代碼,Toast.makeText對this不滿意。您需要使用classname.this

例如,

public class MyClass extends Activity { 
    private ServiceConnection mConnection = new ServiceConnection() { 

    public void myMethod() { 
       Toast.makeText(MyClass.this, 
       R.string.local_service_connected, 
       Toast.LENGTH_SHORT).show(); 
    } 
    } 
} 

沒有MyClass.你的錯誤:

The method makeText(Context, int, int) in the type Toast is not applicable for the arguments (new ServiceConnection(){}, int, int)

的原因是,你在一個內部類實際上是。

參見:Using "this" with class name

,當然,類名不放過的示例代碼是Binder所以如果你的類被稱爲是,它只是工作。