Sinch呼叫按钮错误?

时间:2017-05-17 08:50:43

标签: java android sinch

我正在尝试访问另一个类中的类,我可以收到此错误。我正在使用sinch在我的应用程序中实现应用程序到应用程序的电话,它仍然无法正常工作。

这是我的错误

 FATAL EXCEPTION: main
                                                                                Process: com.example.thinker.myapplication2, PID: 10039
                                                                                java.lang.NullPointerException: Attempt to invoke virtual method 'com.sinch.android.rtc.calling.Call com.example.thinker.myapplication2.SinchService$SinchServiceInterface.callUser(java.lang.String)' on a null object reference
                                                                                    at com.example.thinker.myapplication2.tabs.Chatting$Bases.callButtonClicked(Chatting.java:128)
                                                                                    at com.example.thinker.myapplication2.tabs.Chatting$1.onClick(Chatting.java:83)
                                                                                    at android.view.View.performClick(View.java:5265)
                                                                                    at android.view.View$PerformClick.run(View.java:21534)
                                                                                    at android.os.Handler.handleCallback(Handler.java:815)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                    at android.os.Looper.loop(Looper.java:207)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5683)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

这是我的java课程。

public class Chatting extends ListActivity {
        Runnable refresh, refres;
        ImageView send,back,call;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.yon);
                call= (ImageView)findViewById(R.id.call);
            call.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    if (!isOnline(Chatting.this)) {
                        Toast.makeText(Chatting.this, "No network connection",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    Bases ba = new Bases();
                    ba.onServiceConnected();
                    ba.callButtonClicked();
                }
            });


        }
        public class Bases extends BaseActivity {
                @Override
                protected void onServiceConnected() {
                    Toast.makeText(this, " call ready", Toast.LENGTH_LONG).show();
                }

                public void callButtonClicked() {
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(this);
                    String emaill = sp.getString("friend_email", "anon");
                    if (emaill.isEmpty()) {
                        Toast.makeText(this, "Please enter a user to call", Toast.LENGTH_LONG).show();
                        return;
                    }

                    try {
                        Call call = getSinchServiceInterface().callUser("emaill");
                        if (call == null) {
                            // Service failed for some reason, show a Toast and abort
                            Toast.makeText(this, "Service is not started. Try stopping the service and starting it again before "
                                    + "placing a call.", Toast.LENGTH_LONG).show();
                            return;
                        }
                        String callId = call.getCallId();
                        Intent callScreen = new Intent(this, CallScreenActivity.class);
                        callScreen.putExtra(SinchService.CALL_ID, callId);
                        startActivity(callScreen);
                    } catch (MissingPermissionException e) {
                        ActivityCompat.requestPermissions(this, new String[]{e.getRequiredPermission()}, 0);
                    }

                }
            }
}
下面的

是具有getSinchServiceInterface()的baseactivity类。返回null

public abstract class BaseActivity extends Activity implements ServiceConnection {

    private SinchService.SinchServiceInterface mSinchServiceInterface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getApplicationContext().bindService(new Intent(this, SinchService.class), this,
                BIND_AUTO_CREATE);
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        if (SinchService.class.getName().equals(componentName.getClassName())) {
            mSinchServiceInterface = (SinchService.SinchServiceInterface) iBinder;
            onServiceConnected();
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        if (SinchService.class.getName().equals(componentName.getClassName())) {
            mSinchServiceInterface = null;
            onServiceDisconnected();
        }
    }

    protected void onServiceConnected() {
        // for subclasses
    }

    protected void onServiceDisconnected() {
        // for subclasses
    }

    protected SinchService.SinchServiceInterface getSinchServiceInterface() {
        return mSinchServiceInterface;
    }

}

1 个答案:

答案 0 :(得分:1)

大多数时候,当有人使用错误的方式传递上下文时尝试将上下文作为YourActivityName.this传递(例如Bases.this)而不仅仅是this或getApplicationContext()

开始
                @Override
                protected void onServiceConnected() {
                    Toast.makeText(Bases.this, " call ready", Toast.LENGTH_LONG).show();
                } 

这不是调用活动的正确方法

  Bases ba = new Bases();
  ba.onServiceConnected();
  ba.callButtonClicked(); 

使用

Intent intent = new Intent(YourCurrentActivityName.this,Bases.class);    
startActivity(intent);  

在oncreate方法中,您可以调用此方法callButtonClicked()