简体   繁体   中英

Android/Flurry: Unable to instantiate activity component?

I am attempting to integrate Mixpanel into a simple application using the instructions here . However I am getting an error that is causing my App to crash.

I have no syntax errors in my code (see below).

Android Activity:

package com.example.testmixpanel;

import org.json.JSONException;
import org.json.JSONObject;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends Activity {

    public static final String MIXPANEL_TOKEN = "xxx"; //intentionally not shown

    // Initialize the library with your
    // Mixpanel project token, MIXPANEL_TOKEN, and a reference
    // to your application context.
    MixpanelAPI mixpanel =
        MixpanelAPI.getInstance(this, MIXPANEL_TOKEN);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        JSONObject props = new JSONObject();

        mixpanel.track("Plan Selected", props);
    }

    @Override
    protected void onDestroy() {
        mixpanel.flush();
        super.onDestroy();
    }



}

However, when I try to run the application I get this error:

0-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException

What could be the cause of this issue?

Full stack trace (for reference):

10-09 16:36:31.575: E/AndroidRuntime(13192): FATAL EXCEPTION: main
10-09 16:36:31.575: E/AndroidRuntime(13192): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testmixpanel/com.example.testmixpanel.MainActivity}: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2219)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.os.Looper.loop(Looper.java:176)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread.main(ActivityThread.java:5419)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at java.lang.reflect.Method.invokeNative(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at java.lang.reflect.Method.invoke(Method.java:525)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at dalvik.system.NativeStart.main(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192): Caused by: java.lang.NullPointerException
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:109)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at com.mixpanel.android.mpmetrics.MixpanelAPI.getInstance(MixpanelAPI.java:175)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at com.example.testmixpanel.MainActivity.<init>(MainActivity.java:22)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at java.lang.Class.newInstanceImpl(Native Method)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at java.lang.Class.newInstance(Class.java:1130)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
10-09 16:36:31.575: E/AndroidRuntime(13192):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
10-09 16:36:31.575: E/AndroidRuntime(13192):    ... 11 more

EDIT:

public class MainActivity extends Activity {

    public static final String MIXPANEL_TOKEN = "xxxxx";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MixpanelAPI mixpanel =
                MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);

        setContentView(R.layout.activity_main);

        JSONObject props = new JSONObject();

        mixpanel.track("Plan Selected", props);
    }

    @Override
    protected void onDestroy() {

        mixpanel.flush(); //how to call this as mixpanel is not a class variable?
        super.onDestroy();
    }



}

If you want to use the mixpanel on onDestroy you should instantiate it like this:

public class MainActivity extends Activity {
    public static final String MIXPANEL_TOKEN = "xxxxx";
    MixpanelAPI mixpanel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mixpanel =
                MixpanelAPI.getInstance(getApplicationContext(), MIXPANEL_TOKEN);

        setContentView(R.layout.activity_main);

        JSONObject props = new JSONObject();

        mixpanel.track("Plan Selected", props);
    }
  (...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM