gpt4 book ai didi

java - Sinch 调用按钮错误?

转载 作者:行者123 更新时间:2023-12-02 13:09:08 25 4
gpt4 key购买 nike

我正在尝试访问另一个类中的一个类,但可能会出现此错误。我正在使用 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() 的基本 Activity 类。返回 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;
}

}

最佳答案

大多数情况下,当有人使用错误的方式传递上下文时,尝试将上下文作为 YourActivityName.this (例如 Bases.this )而不只是 this 或 getApplicationContext() 传递,就会发生这种情况

开始于

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

这不是调用 Activity 的正确方法

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

使用

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

在oncreate方法中你可以调用这个方法callButtonClicked()

关于java - Sinch 调用按钮错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44019939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com