2016-07-15 132 views
1

我有类此代码A尝试调用接口方法的Android

private RtcListener mListener; 

@Override 
public void Update(String string) { 
    //here its work now i want to pass data to Interface 

     mListener.onUpdate(sometext); 


} 


public interface RtcListener{ 

    void onUpdate(String string); 

} 
现在

在另一个类及其B

我实现接口

public class B extends Service implements A.RtcListener{ 

// here i Override interface method 
    @Override 
public void onUpdate(String string) { 

Log.d("dwdwadwadwd"," the data is"+string); 

} 



} 

其给我错误

Attempt to invoke interface method 'void ..... on a null object reference 
+0

@quidproquo多数民众赞成在回答工作,活动和服务不带班没有活动 – medo

+0

这不是你如何通常会与服务通信,您应该使用LocalBroadcastManager或EventBus。 – Egor

+1

Your mListener为空 –

回答

0

我通过解决它做在类B

构建类

甲B类

A myclass = new A(this,another prameter if u have,another prameter if u have); 

并在构造函数A类别i为此

public A(RtcListener l , other prameter , other prameter){ //construct method //RtcListener its your interface name 
mListener = l; 
} 

只是确保你的代码的界面就像我的问题上面的代码也 农具

这是所有:)

相关问题