Android Azure 交互发生致命异常

Android Fatal Exception With Azure Interaction

我正在尝试从我的 Android 应用程序向 Azure 发送一些数据。我在设置移动服务时使用 Azure 提供的示例。我的代码没有错误,但是当我尝试在按钮上加载 activity 时,单击应用程序崩溃。我尝试加载的 activity 在 onCreate 方法上有 Azure 交互代码。

更新

这适用于其他 android 设备,但是当我尝试 运行 它在 Samsung Galaxy S3 Mini 运行nning Android 版本 4.1.2 上时它崩溃了。

Register.java

package com.example.martin.ivebeenthere;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
import com.microsoft.windowsazure.mobileservices.MobileServiceClient;
import com.microsoft.windowsazure.mobileservices.http.ServiceFilterResponse;
import com.microsoft.windowsazure.mobileservices.table.TableOperationCallback;

import java.net.MalformedURLException;

public class Register extends AppCompatActivity {

    private MobileServiceClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        try {
            mClient = new MobileServiceClient(
                    "AZURE ACCOUNT",
                    "AZURE KEY",
                    this
            );
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        Item item = new Item();
        item.Text = "Awesome item";
        mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
            public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
                if (exception == null) {
                    // Insert succeeded
                } else {
                    // Insert failed
                }
            }
        });
    }

    public void onClickbtnRegister(View view)
    {
        startActivity(new Intent(Register.this, Login.class));

        Toast.makeText(getApplicationContext(), "Successfully Registered",
                Toast.LENGTH_LONG).show();
    }

}

Item.java

package com.example.martin.ivebeenthere;

/**
 * Created by Martin on 31/03/2016.
 */
public class Item {
    public String Id;
    public String Text;
}

Logcat

04-06 10:28:31.570 15331-15331/com.example.martin.ivebeenthere E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NoClassDefFoundError: com.google.gson.GsonBuilder 
at com.microsoft.windowsazure.mobileservices.MobileServiceClient.createMobileServiceGsonBuilder(MobileServiceClient.java:192)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:179)
at com.microsoft.windowsazure.mobileservices.MobileServiceClient.<init>(MobileServiceClient.java:158)
at com.example.martin.ivebeenthere.Register.onCreate(Register.java:30)
at android.app.Activity.performCreate(Activity.java:5047)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
at android.app.ActivityThread.access0(ActivityThread.java:134)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)

你的依赖中有com.google.code.gson吗?

查看 build.gradle 中的依赖项列表并确保您具有以下内容:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.code.gson:gson:2.3'
    compile 'com.google.guava:guava:18.0'
    compile 'com.squareup.okhttp:okhttp:2.5.0'
    compile 'com.microsoft.azure:azure-mobile-android:3.1.0'
    compile (group: 'com.microsoft.azure', name: 'azure-notifications-handler', version: '1.0.1', ext: 'jar')
}