Vk sdk for android

时间:2014-03-27 14:37:58

标签: android vk

我想与vk.com进行自动化。我下载了this库,但没有正常的例子如何使用它?我尝试下一个代码,但它对我不起作用。

 package ua.khuta.testVkSdk;

   import android.app.Activity;
    import android.app.AlertDialog;
   import android.content.Intent;
   import android.os.Bundle;
   import com.vk.sdk.*;
   import com.vk.sdk.api.VKError;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */


        private static String sTokenKey = "VK_ACCESS_TOKEN";
        private static String[] sMyScope = new String[]{VKScope.FRIENDS, VKScope.WALL, VKScope.PHOTOS, VKScope.NOHTTPS};
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            VKSdk.initialize(sdkListener, "id_приложения", VKAccessToken.tokenFromSharedPreferences(this, sTokenKey));
            setContentView(R.layout.main);
            VKSdk.authorize(sMyScope, true, false);
        }

        @Override
        protected void onResume() {
            super.onResume();
            VKUIHelper.onResume(this);
        }

        @Override
        protected void onDestroy() {
            super.onDestroy();
            VKUIHelper.onDestroy(this);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            VKUIHelper.onActivityResult(requestCode, resultCode, data);
        }

        private VKSdkListener sdkListener = new VKSdkListener() {
            @Override
            public void onCaptchaError(VKError captchaError) {
                new VKCaptchaDialog(captchaError).show();
            }

            @Override
            public void onTokenExpired(VKAccessToken expiredToken) {
                VKSdk.authorize(sMyScope);
            }

            @Override
            public void onAccessDenied(VKError authorizationError) {
                new AlertDialog.Builder(MyActivity.this)
                        .setMessage(authorizationError.errorMessage)
                        .show();
            }

            @Override
            public void onReceiveNewToken(VKAccessToken newToken) {
                newToken.saveTokenToSharedPreferences(MyActivity.this, sTokenKey);
               // Intent i = new Intent(LoginActivity.this, MainActivity.class);
               // startActivity(i);
            }

            @Override
            public void onAcceptUserToken(VKAccessToken token) {
                //Intent i = new Intent(LoginActivity.this, MainActivity.class);
               // startActivity(i);
            }
        };

     }

错误文本

 03-27 16:42:16.189: WARN/System.err(19974): java.net.BindException: Context must not be null
03-27 16:42:16.189: WARN/System.err(19974): at com.vk.sdk.VKSdk.checkConditions(VKSdk.java:90)
03-27 16:42:16.189: WARN/System.err(19974): at com.vk.sdk.VKSdk.authorize(VKSdk.java:170)
03-27 16:42:16.189: WARN/System.err(19974): at ua.khuta.testVkSdk.MyActivity.onCreate(MyActivity.java:23)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.Activity.performCreate(Activity.java:5231)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-27 16:42:16.189: WARN/System.err(19974): at android.os.Handler.dispatchMessage(Handler.java:102)
03-27 16:42:16.189: WARN/System.err(19974): at android.os.Looper.loop(Looper.java:136)
03-27 16:42:16.189: WARN/System.err(19974): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-27 16:42:16.189: WARN/System.err(19974): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 16:42:16.189: WARN/System.err(19974): at java.lang.reflect.Method.invoke(Method.java:515)
03-27 16:42:16.189: WARN/System.err(19974): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-27 16:42:16.189: WARN/System.err(19974): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-27 16:42:16.189: WARN/System.err(19974): at dalvik.system.NativeStart.main(Native Method)
03-27 16:42:16.189: INFO/ActivityManager(593): Resuming delayed broadcast

1 个答案:

答案 0 :(得分:2)

问题出现是因为您没有关注UIHelper Applying部分。您发布的错误指向以下代码:

if (sInstance.getContext() == null) {
    throw new BindException("Context must not be null");
}

所以,让我们来看看getContext方法,它非常简单:

Context getContext() {
    return VKUIHelper.getTopActivity();
}

为什么会返回null?好吧,VKUIHelper很简单,请查看it,然后您就会明白原因(因为mTopActivity null没有调用VKUIHelper.onCreate()