android 实体类里边写一个boolean,尝试在空对象引用上调用虚方法boolean com.google.android.gms.common.ConnectionResult.isSucce...

我正在尝试使用教程在我的应用程序中集成FireBase Cloud Messaging:

当我试图启动应用程序时,我得到一个空指针异常:

这是logcat:

java.lang.NullPointerException: Attempt to invoke virtual method

'boolean com.google.android.gms.common.ConnectionResult.isSuccess()' on a null object reference

at com.google.android.gms.common.internal.zzd$zzi.zzh(Unknown Source)

at com.google.android.gms.common.internal.zzd$zzk.zztp(Unknown Source)

at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source)

at com.google.android.gms.common.internal.zzd$zza.zzw(Unknown Source)

at com.google.android.gms.common.internal.zzd$zze.zztr(Unknown Source)

at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:145)

at android.app.ActivityThread.main(ActivityThread.java:5942)

at java.lang.reflect.Method.invoke(Native Method)

at java.lang.reflect.Method.invoke(Method.java:372)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

这是我的build.gradle:

buildscript {

repositories {

mavenCentral()

}

dependencies {

classpath 'com.android.tools.build:gradle:2.0.0'

//FireBase dependency

classpath 'com.google.gms:google-services:3.0.0'

}

}

apply plugin: 'android'

dependencies {

compile fileTree(include: '*.jar', dir: 'libs')

//FireBase dependency

compile 'com.google.firebase:firebase-messaging:10.0.1'

}

android {

compileSdkVersion 20

buildToolsVersion '23.0.3'

sourceSets {

main {

manifest.srcFile 'AndroidManifest.xml'

java.srcDirs = ['src']

resources.srcDirs = ['src']

aidl.srcDirs = ['src']

renderscript.srcDirs = ['src']

res.srcDirs = ['res']

assets.srcDirs = ['assets']

jniLibs.srcDirs = ['libs']

}

instrumentTest.setRoot('tests')

// Move the build types to build-types/

// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...

// This moves them out of them default location under src//... which would

// conflict with src/ being used by the main source set.

// Adding new build types or product flavors should be accompanied

// by a similar customization.

debug.setRoot('build-types/debug')

release.setRoot('build-types/release')

}

lintOptions {

abortOnError false

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

compileOptions {

sourceCompatibility JavaVersion.VERSION_1_7

targetCompatibility JavaVersion.VERSION_1_7

}

productFlavors {

x86 {

ndk {

abiFilter "x86"

}

}

arm {

ndk {

abiFilters "armeabi-v7a", "armeabi"

}

minSdkVersion 17

}

}

packagingOptions {

exclude 'META-INF/DEPENDENCIES'

exclude 'META-INF/NOTICE'

exclude 'META-INF/LICENSE'

exclude 'META-INF/license.txt'

exclude 'META-INF/notice.txt'

}

defaultConfig {

minSdkVersion 17

applicationId "com.nytshft.evyt"

}

}

//FireBase

apply plugin: 'com.google.gms.google-services'

Note : I don't have an app module in my application. I have a single build.gradle file.

它与build.gradle有关吗?在官方网站上,他们说要在app文件夹中复制json文件 . 不幸的是我的应用程序没有一个这样的文件夹 . 这是ProjectView Screenshot . 他们还说要在app 's build.gradle. Since I dont have any app folder, I did those changes in the project'的build.gradle文件中添加一些依赖项 .

MyFirebaseInstanceIDService.java :

class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "MyFirebaseIIDService";

@Override

public void onTokenRefresh() {

// Get updated InstanceID token.

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Log.d(TAG, "Refreshed token: " + refreshedToken);

sendRegistrationToServer(refreshedToken);

}

private void sendRegistrationToServer(String token) {

// TODO: Implement this method to send token to your app server.

}

MyFireBaseMessagingService.java

class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MyFirebaseMsgService";

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.d(TAG, "From: " + remoteMessage.getFrom());

// Check if message contains a data payload.

if (remoteMessage.getData().size() > 0) {

Log.d(TAG, "Message data payload: " + remoteMessage.getData());

}

// Check if message contains a notification payload.

if (remoteMessage.getNotification() != null) {

Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());

//Calling method to generate notification

//sendNotification(remoteMessage.getNotification().getBody());

}

Toast.makeText(getApplicationContext(), "Push notification: " + remoteMessage.getNotification().getBody(), Toast.LENGTH_LONG).show();

}

/**

* Create and show a simple notification containing the received FCM message.

* This method is only generating push notification.

* @param messageBody FCM message body received.

*/

private void sendNotification(String messageBody) {

Intent intent = new Intent(this, MainActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

.setSmallIcon(R.drawable.androidwidget_logo)

.setContentTitle("FCM Message")

.setContentText(messageBody)

.setAutoCancel(true)

.setSound(defaultSoundUri)

.setContentIntent(pendingIntent);

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值