Question

I want to make autorizathion with vk.com. I download this library, but there is no normal exemple how to use it? I try next code, but it is not work for me.

 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);
            }
        };

     }

Text of error

 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
Was it helpful?

Solution

The problem happens because you didn't follow UIHelper Applying section. The error you posted is pointing to the following piece of code:

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

So, let's take a look at the getContext method, it's very simple:

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

Why does it return null? Well, VKUIHelper is simple, take a look on it and you'll realize why (because mTopActivity is null without calling VKUIHelper.onCreate())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top