Question

I am creating an android app which uses Azure mobile services. I managed to connect to my azure mobile service and retrieve data.

Then I wanted to include Facebook authentication in to my app.

MobileServiceClient class has a method to use for authentication using Google,
Facebook, Twitter and Microsoft Live login providers.

Right after I inserted the following code snippet to my onCreate() method I started getting errors.

mClient.login(MobileServiceAuthenticationProvider.Facebook,
            new UserAuthenticationCallback() {

                @Override
                public void onCompleted(MobileServiceUser user,
                        Exception exception, ServiceFilterResponse response) {
                    if (exception != null) {

                        Log.d("ShopiN", "login success");
                    }

                }
            });  

Log cat shows following.

04-19 08:42:18.566: D/gralloc_goldfish(3309): Emulator without GPU emulation detected.
04-19 08:44:06.648: D/dalvikvm(3353): GC_FOR_ALLOC freed 137K, 12% free 2747K/3116K, paused 143ms, total 145ms
04-19 08:44:06.748: I/dalvikvm-heap(3353): Grow heap (frag case) to 3.978MB for 1127532-byte allocation
04-19 08:44:06.908: D/dalvikvm(3353): GC_FOR_ALLOC freed 2K, 9% free 3846K/4220K, paused 155ms, total 155ms
04-19 08:44:07.338: D/AndroidRuntime(3353): Shutting down VM
04-19 08:44:07.338: W/dalvikvm(3353): threadid=1: thread exiting with uncaught exception (group=0xb0f03648)
04-19 08:44:07.438: E/AndroidRuntime(3353): FATAL EXCEPTION: main
04-19 08:44:07.438: E/AndroidRuntime(3353): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shopindev2/com.example.shopindev2.MainActivity}: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.os.Looper.loop(Looper.java:137)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at java.lang.reflect.Method.invokeNative(Native Method)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at java.lang.reflect.Method.invoke(Method.java:525)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at dalvik.system.NativeStart.main(Native Method)
04-19 08:44:07.438: E/AndroidRuntime(3353): Caused by: java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.microsoft.windowsazure.mobileservices.LoginManager.showLoginUI(LoginManager.java:282)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.microsoft.windowsazure.mobileservices.LoginManager.authenticate(LoginManager.java:143)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.microsoft.windowsazure.mobileservices.MobileServiceClient.login(MobileServiceClient.java:230)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.microsoft.windowsazure.mobileservices.MobileServiceClient.login(MobileServiceClient.java:214)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at com.example.shopindev2.MainActivity.onCreate(MainActivity.java:31)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.Activity.performCreate(Activity.java:5133)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-19 08:44:07.438: E/AndroidRuntime(3353):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
04-19 08:44:07.438: E/AndroidRuntime(3353):     ... 11 more

My MainActivity class is as follows

package com.example.shopindev2;

import java.net.MalformedURLException;

import com.microsoft.windowsazure.mobileservices.MobileServiceAuthenticationProvider;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.MobileServiceUser;
import com.microsoft.windowsazure.mobileservices.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.UserAuthenticationCallback;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
    MobileServiceClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            mClient = new MobileServiceClient(
                    "myServiceUrl",
                    "myAppKey", getApplicationContext());
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        mClient.login(MobileServiceAuthenticationProvider.Facebook,
                new UserAuthenticationCallback() {

                    @Override
                    public void onCompleted(MobileServiceUser user,
                            Exception exception, ServiceFilterResponse response) {
                        if (exception != null) {

                            Log.d("ShopiN", "login success");
                        }else{

                            Log.d("ShopiN", "login Failed");
                        }

                    }
                });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

My Android Manifest is as follows

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.shopindev2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        >
        <activity
            android:name="com.example.shopindev2.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I am stuck here. Can anyone show me where am I missing it?

Was it helpful?

Solution

First, make sure that the login is working via browser. To do so, go to the following link in your browser:

https://<ServiceName>.azure-mobile.net/login/facebook

Once, you have made sure that it is working, you know that the problem is on the SDK side. To debug that problem, simply try to print the exception in else block or observe it by setting breakpoints.

This should give you an idea. Comment back and tell us how it goes.

Edit: Looks like you are passing an instance of Application to your SDK while creating mClient instance. You're doing the following:

mClient = new MobileServiceClient(
                "myServiceUrl",
                "myAppKey", getApplicationContext());

Instead, change it to this:

mClient = new MobileServiceClient(
                "myServiceUrl",
                "myAppKey", this);

This way, you are actually passing an instance of Activity. Mobile services would otherwise work well with the ApplicationContext. However, when authentication is added, the SDK needs to create a UI overlay for login. Creating UI without host activity isn't possible. That's why, the SDK requires Activity instance to create the login screen when authentication is added.

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