Announcement Announcement Module
Collapse
No announcement yet.
NullPointerException in WebView with spring-android. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • NullPointerException in WebView with spring-android.

    Hello everyone.
    I have an Android application an I want to implement a Facebook login with the spring-android module. For that, I used the spring-android-facebook-client from the samples, which is running perfect. But when I run my application, I get the following error:

    Code:
    FATAL EXCEPTION: WebViewCoreThread
    java.lang.NullPointerException
    at android.webkit.WebViewDatabase.getInstance(WebViewDatabase.java:184)at android.webkit.CacheManager.init(CacheManager.java:178)
    at android.webkit.BrowserFrame.<init>(BrowserFrame.java:202)
    at android.webkit.WebViewCore.initialize(WebViewCore.java:186)
    at android.webkit.WebViewCore.access$500(WebViewCore.java:53)
    at android.webkit.WebViewCore$WebCoreThread$1.handleMessage(WebViewCore.java:
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:629)
    at java.lang.Thread.run(Thread.java:1019)
    Here's my code(shortened), which should be the same as in the samples:

    Code:
    public abstract class AbstractWebViewActivity extends Activity implements BaseActivity
    {
        private Activity container;
        private WebView webView;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
            webView = new WebView(this);
            setContentView(webView);
            container = this;
    
            webView.setWebChromeClient(new WebChromeClient()
            {
                @Override
                public void onProgressChanged(WebView view, int progress) {
                    container.setTitle("Loading...");
                    container.setProgress(progress * 100);
                    if (progress == 100) {
                        container.setTitle(R.string.app_name);
                    }
                }
            });
        }
    
        protected WebView getWebView() {
            return this.webView;
        }
    }
    Code:
    public class FacebookOAuthSignInActivity extends AbstractWebViewActivity
    {
    
        private static final String TAG = FacebookOAuthSignInActivity.class.getSimpleName();
        private ConnectionRepository connectionRepository;
        private FacebookConnectionFactory connectionFactory;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getWebView().getSettings().setJavaScriptEnabled(true);
            getWebView().setWebViewClient(new FacebookOAuthWebViewClient());
            this.connectionRepository = getAppContext().getConnectionRepository();
            this.connectionFactory = getAppContext().getFacebookConnectionFactory();
        }
    
        @Override
        public void onStart() {
            super.onStart();
            // display the Facebook authorization page
            getWebView().loadUrl(getAuthorizeUrl());
        }
    
        private String getAuthorizeUrl() {
            String redirectUri = getString(R.string.facebook_oauth_callback_url);
            String scope = getString(R.string.facebook_scope);
            OAuth2Parameters params = new OAuth2Parameters();
            params.setRedirectUri(redirectUri);
            params.setScope(scope);
            params.add("display", "touch");
            return this.connectionFactory.getOAuthOperations().buildAuthorizeUrl(GrantType.IMPLICIT_GRANT, params);
        }
    
        private void displayFacebookOptions() {
            Intent intent = new Intent();
            intent.setClass(this, GMapActivity.class);
            startActivity(intent);
            finish();
        }
    
        private class FacebookOAuthWebViewClient extends WebViewClient
        {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // parse the captured url
                Uri uri = Uri.parse(url);
                String uriFragment = uri.getFragment();
                if (uriFragment != null && uriFragment.startsWith("access_token=")) {
                    try {
                        // split to get the two different parameters
                        String[] params = uriFragment.split("&");
    
                        // split to get the access token parameter and value
                        String[] accessTokenParam = params[0].split("=");
                        String accessToken = accessTokenParam[1];
                        AccessGrant accessGrant = new AccessGrant(accessToken);
                        Connection<Facebook> connection = connectionFactory.createConnection(accessGrant);
    
                        try {
                            connectionRepository.addConnection(connection);
                        }
                        catch (DuplicateConnectionException e) {
                            // connection already exists in repository!
                        }
                    }
                    catch (Exception e) {
                    }
                    displayFacebookOptions();
                }
                if (uri.getQueryParameter("error") != null) {
                    CharSequence errorReason = uri.getQueryParameter("error_description").replace("+", " ");
                    Toast.makeText(getApplicationContext(), errorReason, Toast.LENGTH_LONG).show();
                    displayFacebookOptions();
                }
            }
        }
    }
    In debug mode I can see that
    Code:
    WebViewDataBase.getInstance(context)
    is invoced with context=null. But The context is given because of
    Code:
    webView = new WebView(this)
    ;

    Unfortunately I couldn't find any ideas or solutions via internet research.

    Any ideas? Do I miss something???
    Thanks in advance!
    Markus

  • #2
    Wow, tricky ... I don't see anything wrong here.

    Just a few things you might try / check out:
    - What exactly is the interface BaseActivity meant for?
    - What's the value of 'this' at the moment of setting new WebView(this), and what does getApplicationContext return? (simple Log-debug outputs)
    - Increase logging-verbosity, in order to see androids internal outputs
    - re-start form the showcase-sample, and introduce your changes one after each other
    - all activities correctly registered in manifest file?

    Cheers, S.

    Comment


    • #3
      Hey!
      - BaseActivity is just an interface, that all my (Base)-Activities have to implement. Just adopted from the sample:

      Code:
      public interface BaseActivity
      {
          MainApplication getAppContext();
      
          void displayNetworkError(String message);
          void displayAuthorizationError(String message);
          void showProgressDialog(String message);
          void dismissProgressDialog();
      }
      - in
      Code:
      webView = new WebView(this);
      , this refers to to my FacebookOAuthSignInActivity, as it should..
      - android doesn't give me more output...is there a place I miss to look for?
      -all activities registered...

      I'll try it step by step, but thank you a lot!!!

      Comment


      • #4
        Ok I was just a little bit confused, because I didn't see anything that might be related to BaseActivity implementation.

        Good luck!

        Comment


        • #5
          Started all from scratch and it worked...Confusing...

          Comment


          • #6
            @markuse1501 glad to hear you got it working. We're evaluating how to improve this functionality in a future release, and encapsulate the login process so you don't have to implement the complicated webview logic.

            Comment


            • #7
              I'm not really sure if it's too complicated, maybe I just have to do some homework (yes, I have to!)
              Thanks for the great work you're doing at SpringSource!!
              It's a pleasure, with every line of code.

              Comment

              Working...
              X