首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >试图调用空对象上的虚方法“void com.pushbots.push.utils.PBPrefs.setCustomHandler(java.lang.String)‘”%r

试图调用空对象上的虚方法“void com.pushbots.push.utils.PBPrefs.setCustomHandler(java.lang.String)‘”%r
EN

Stack Overflow用户
提问于 2016-04-06 04:02:45
回答 1查看 108关注 0票数 0

我正在使用推送机器人API进行推送通知,当我尝试实现通知的自定义处理程序时,我得到了异常空指针。

下面是我学习https://pushbots.com/developer/docs/android-sdk-integration的文档

我的Android清单

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.assorttech.assorttech" >

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- GCM requires a Google account. -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <permission android:name="com.assorttech.assorttech.permission.C2D_MESSAGE" android:protectionLevel="signature" />
    <uses-permission android:name="com.assorttech.assorttech.permission.C2D_MESSAGE" />
    <!-- This app has permission to register and receive dataf message. -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="com.assorttech.assorttech.MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


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

        <activity
            android:name="com.thefinestartist.finestwebview.FinestWebViewActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:screenOrientation="sensor"
            android:theme="@style/FinestWebViewTheme.Light"
            >

        </activity>
        <receiver
            android:name="com.pushbots.google.gcm.GCMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <!-- Receives the actual messages. -->
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- Receives the registration id. -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.assorttech.assorttech" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.pushbots.push.DefaultPushHandler" />
        <!--<receiver android:name="com.assorttech.assorttech.customHandler" />-->
        <service android:name="com.pushbots.push.GCMIntentService" />
    </application>

</manifest>
代码语言:javascript
复制
public class customHandler extends BroadcastReceiver
{
    private static final String TAG = "customHandler";

    public void onReceive(Context context, Intent intent)
    {
        String action = intent.getAction();
        Log.d(TAG, "action=" + action);
        // Handle Push Message when opened
        if (action.equals(PBConstants.EVENT_MSG_OPEN)) {
            //Check for Pushbots Instance
            Pushbots pushInstance = Pushbots.sharedInstance();
            if(!pushInstance.isInitialized()){
               // Log.d("Initializing Pushbots.");
                Pushbots.sharedInstance().init(context.getApplicationContext());
            }

            //Clear Notification array
            if(PBNotificationIntent.notificationsArray != null){
                PBNotificationIntent.notificationsArray = null;
            }

            HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_OPEN);
            Log.w(TAG, "User clicked notification with Message: " + PushdataOpen.get("message"));

            //Report Opened Push Notification to Pushbots
            if(Pushbots.sharedInstance().isAnalyticsEnabled()){
                Pushbots.sharedInstance().reportPushOpened( (String) PushdataOpen.get("PUSHANALYTICS"));
            }

            //Start lanuch Activity
            String packageName = context.getPackageName();
            Intent resultIntent = new Intent(context.getPackageManager().getLaunchIntentForPackage(packageName));
            resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK);

            resultIntent.putExtras(intent.getBundleExtra("pushData"));
            Pushbots.sharedInstance().startActivity(resultIntent);

            // Handle Push Message when received
        }else if(action.equals(PBConstants.EVENT_MSG_RECEIVE)){
            HashMap<?, ?> PushdataOpen = (HashMap<?, ?>) intent.getExtras().get(PBConstants.EVENT_MSG_RECEIVE);
            Log.w(TAG, "User Received notification with Message: " + PushdataOpen.get("message"));
        }
    }
}

主要活动:

代码语言:javascript
复制
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Pushbots.sharedInstance().setCustomHandler(customHandler.class);
}
EN

回答 1

Stack Overflow用户

发布于 2016-04-06 04:15:25

您需要添加

代码语言:javascript
复制
Pushbots.sharedInstance().init(this); 

在您的活动的onCreate()方法中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36435962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档