我正在尝试学习如何使用 TokBox SDK。我已经开始编写一个简单的应用程序来显示视频,以便了解它的工作原理。我的代码基于https://tokbox.com/opentok/tutorials/connect-session/android/#connecting中的示例。但是,每次我初始化 Session 对象时,我的应用程序都会停止工作。我试过用 try/catch 块包围它,但它仍然崩溃。我的代码如下:
package com.example.ben.toktest;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import com.opentok.android.Session;
public class MainActivity extends Activity{
private static final String API_KEY= "12345678"; //replaced api key with 12345678 for privacy purposes
RelativeLayout container;
Session session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
container = (RelativeLayout) findViewById(R.id.main);
session = new Session(MainActivity.this, "12345678", "2_MX40NTEwMjczMn5-MTQxODAyMDUwMjgwMn5PTjdlRDBvSDVYOHBkelZjby80eGZuclN-fg"); //Crashes here
/*session.setSessionListener(new Session.SessionListener() {
@Override
public void onConnected(Session session) {
}
@Override
public void onDisconnected(Session session) {
}
@Override
public void onStreamReceived(Session session, Stream stream) {
mStreams.add(stream);
subscriber = new Subscriber(MainActivity.this, stream);
subscriber.setSubscriberListener(new SubscriberListener() {
@Override
public void onConnected(SubscriberKit subscriberKit) {
LayoutParams params = new LayoutParams(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
container.addView(subscriberKit.getView(), params);
}
@Override
public void onDisconnected(SubscriberKit subscriberKit) {
}
@Override
public void onError(SubscriberKit subscriberKit, OpentokError opentokError) {
}
});
session.subscribe(subscriber);
}
@Override
public void onStreamDropped(Session session, Stream stream) {
}
@Override
public void onError(Session session, OpentokError opentokError) {
}
});*/
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我的 logcat 的输出是:
12-08 17:35:37.240 13677-13677/com.example.ben.toktest W/ActivityThread﹕ Application com.example.ben.toktest is waiting for the debugger on port 8100...
12-08 17:35:37.250 13677-13677/com.example.ben.toktest I/System.out﹕ Sending WAIT chunk
12-08 17:35:37.355 13677-13684/com.example.ben.toktest I/dalvikvm﹕ Debugger is active
12-08 17:35:37.455 13677-13677/com.example.ben.toktest I/System.out﹕ Debugger has connected
12-08 17:35:37.455 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:37.655 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:37.855 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.055 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.255 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.455 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.655 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.855 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.060 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.260 13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.460 13677-13677/com.example.ben.toktest I/System.out﹕ debugger has settled (1374)
12-08 17:35:40.105 13677-13677/com.example.ben.toktest W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/opentok/android/Session;
12-08 17:35:40.105 13677-13677/com.example.ben.toktest D/AndroidRuntime﹕ Shutting down VM
12-08 17:35:40.105 13677-13677/com.example.ben.toktest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40edb2a0)
12-08 17:35:40.205 13677-13677/com.example.ben.toktest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opentok: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.opentok.android.Session.<clinit>(Session.java:46)
at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
12-08 17:36:01.010 14367-14367/com.example.ben.toktest W/ActivityThread﹕ Application com.example.ben.toktest is waiting for the debugger on port 8100...
12-08 17:36:01.020 14367-14367/com.example.ben.toktest I/System.out﹕ Sending WAIT chunk
12-08 17:36:01.070 14367-14373/com.example.ben.toktest I/dalvikvm﹕ Debugger is active
12-08 17:36:01.220 14367-14367/com.example.ben.toktest I/System.out﹕ Debugger has connected
12-08 17:36:01.220 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.420 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.620 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.820 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.020 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.225 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.425 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.630 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.845 14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:03.045 14367-14367/com.example.ben.toktest I/System.out﹕ debugger has settled (1459)
12-08 17:36:03.395 14367-14367/com.example.ben.toktest W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/opentok/android/Session;
12-08 17:36:03.400 14367-14367/com.example.ben.toktest D/AndroidRuntime﹕ Shutting down VM
12-08 17:36:03.400 14367-14367/com.example.ben.toktest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40edb2a0)
12-08 17:36:03.425 14367-14367/com.example.ben.toktest E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ExceptionInInitializerError
at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opentok: findLibrary returned null
at java.lang.Runtime.loadLibrary(Runtime.java:365)
at java.lang.System.loadLibrary(System.java:535)
at com.opentok.android.Session.<clinit>(Session.java:46)
at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
有人可以告诉我为什么它总是崩溃吗?我不知道我做错了什么!