gpt4 book ai didi

java - 安卓/乱舞 : Unable to instantiate activity component?

转载 作者:行者123 更新时间:2023-12-02 05:23:34 25 4
gpt4 key购买 nike

我正在尝试使用说明 here 将 Mixpanel 集成到一个简单的应用程序中。但是我收到一个错误,导致我的应用程序崩溃。

我的代码中没有语法错误(见下文)。

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();
}



}

但是,当我尝试运行该应用程序时,我收到此错误:

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

导致此问题的原因是什么?

完整堆栈跟踪(仅供引用):

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

编辑:

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();
}



}

最佳答案

如果你想在 onDestroy 上使用 mixpanel,你应该像这样实例化它:

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);
}
(...)

关于java - 安卓/乱舞 : Unable to instantiate activity component?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26283082/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com