I am populating shared preferences into a PreferenceFragment, available as a tab in the ActionBar. The preferences are populated in the Fragment's onCreate method,
public static class WIKPreferenceFragment extends PreferenceFragment
{
#Override
public void onCreate ( Bundle bundle )
{
super.onCreate( bundle );
this.addPreferencesFromResource( R.layout.settings_fragment );
}
}
When the tab is selected, the PreferenceFragment is added directly,
public void onTabSelected ( Tab tab, FragmentTransaction transaction )
{
transaction.replace( R.id.tab_view_target, new WIKPreferenceFragment() );
( (WIKActivity) this.activity ).setTabMemory( tab.getPosition() );
}
The content of R.layout.settings_fragment is
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<PreferenceCategory android:title="#string/category_proximity_settings" >
<CheckBoxPreference
android:key="#+key/USE_PASSIVE_GPS"
android:summary="#string/summary_use_passive_gps"
android:title="#string/title_use_passive_gps" />
<EditTextPreference
android:inputType="number"
android:key="#+key/GPS_TIMEOUT"
android:summary="#string/summary_gps_timeout"
android:title="#string/title_gps_timeout" />
</PreferenceCategory>
</PreferenceScreen>
With a clean install, the preference fragment appears correctly when the tab is first selected. If another tab is selected and this tab is returned to, the following error occurs:
05-14 19:23:02.366: E/AndroidRuntime(11744): FATAL EXCEPTION: main
05-14 19:23:02.366: E/AndroidRuntime(11744): Process: org.eightbeers.android.wik, PID: 11744
05-14 19:23:02.366: E/AndroidRuntime(11744): java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.Preference.getPersistedString(Preference.java:1429)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.EditTextPreference.onSetInitialValue(EditTextPreference.java:154)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.Preference.dispatchSetInitialValue(Preference.java:1345)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.Preference.onAttachedToHierarchy(Preference.java:1140)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.PreferenceGroup.addPreference(PreferenceGroup.java:163)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:104)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.PreferenceGroup.addItemFromInflater(PreferenceGroup.java:45)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.GenericInflater.rInflate(GenericInflater.java:488)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.GenericInflater.inflate(GenericInflater.java:326)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.GenericInflater.inflate(GenericInflater.java:263)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
05-14 19:23:02.366: E/AndroidRuntime(11744): at org.eightbeers.android.wik.activity.SettingsTab$WIKPreferenceFragment.onCreate(SettingsTab.java:27)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.Fragment.performCreate(Fragment.java:1688)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:860)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1063)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1450)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:444)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.os.Handler.handleCallback(Handler.java:733)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.os.Handler.dispatchMessage(Handler.java:95)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.os.Looper.loop(Looper.java:136)
05-14 19:23:02.366: E/AndroidRuntime(11744): at android.app.ActivityThread.main(ActivityThread.java:5105)
05-14 19:23:02.366: E/AndroidRuntime(11744): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 19:23:02.366: E/AndroidRuntime(11744): at java.lang.reflect.Method.invoke(Method.java:515)
05-14 19:23:02.366: E/AndroidRuntime(11744): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
05-14 19:23:02.366: E/AndroidRuntime(11744): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
05-14 19:23:02.366: E/AndroidRuntime(11744): at dalvik.system.NativeStart.main(Native Method)
Some potentially-helpful data points:
This error does not occur when the CheckBoxPreference is commented-out, or if the EditTextPreference is commented-out (i.e., everything works if there is only one preference; of course, I'd like to have more than one, more than two, even)
This error does not occur if preferences are not reloaded in onCreate if they've already been loaded; however, when this is changed no preferences appear at all when the tab is reselected
This error persists through clearing of user data, restarting Eclipse, uninstalling of the app, project cleans, and phone reboots (okay, just one); with exception to the phone reboot (since I only tried it once), it persists through any combination of the previous
To make uninstalls easier, I renamed the app from WIK to AAWIK; however, in the Settings -> Application Manager window the app still appears alphabetized as WIK. Everywhere else in the UI --- app drawer, uninstall pane, notifications, etc. --- it is AAWIK; this might just be an extreme caching issue? (Edit: this last one was addressed by just letting the phone sit, idle, long enough for me to type this question; no longer a useful data point, all other issues still persist.)
The problem was solved by switching the android:key="#+key/[keyname]" in the XML for android:key="[keyname]".
I do not intend to accept my answer immediately since I have no idea of why this worked, and Google has not been forthcoming.
Key should be stored as string in prefs.xml (or another name if you want)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="pref_key_gps" translatable="false">pref_key_gps</string>
</resources>
you should replace:
android:key="#+key/USE_PASSIVE_GPS"
to:
android:key="#string/pref_key_gps"
Chinese
现在碰到的问题是,addPreferencesFromResource ClassCastException Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
我所使用到的控件 <EditTextPreference .../>
我的业务场景:
通过 EditTextPreference 处理的数据都是数字。
首次将数据写入 xml 文件的工作不是通过 EditTextPreference 完成的;我是手动完成的,使用的方法是 putInt
在加载包含 EditTextPreference 布局的时候报错。
错误原因,在 EditTextPreference 类内部所维护的数据是通过 putString 和 getString 来维护的。
所以我只需要将手动部分设置成 putString 即可解决问题。
English
My problem is : addPreferencesFromResource ClassCastException Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
The widget I am using is: <EditTextPreference .../>
My business scene is:
EditTextPreference only contrue numbers.
I write data to xml file by SharedPerference.Editro.putInt() ,not done by `EditTextPreference' ,in the first time .
App creash when load EditTextPreference layout.
The cause of the problem is :
Inside the EditTextPreference class , data maintained by putString and getString .But I used is putInt
So change putInt to putString can fix the problem
Related
In My app When I go to the settings activity then back, my app force to stop the activity, I don't know which mistake is there.Please help me to solve that. Thanks for your help in advance!
08-13 11:36:36.355 21968-21968/com.example.user.dictationapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.dictationapp, PID: 21968
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.dictationapp/com.example.user.dictationapp.DictationVocab}: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
at android.widget.CompoundButton.onRestoreInstanceState(CompoundButton.java:378)
at android.view.View.dispatchRestoreInstanceState(View.java:12842)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2658)
at android.view.View.restoreHierarchyState(View.java:12820)
at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1874)
at android.app.Activity.onRestoreInstanceState(Activity.java:949)
at android.app.Activity.performRestoreInstanceState(Activity.java:921)
at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1138)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2252)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1200)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
android.view.AbsSavedState$1 cannot be cast to android.widget.CompoundButton$SavedState
Having experienced these ClassCastExceptions before, I'd suggest that you check that you do not have a same id being shared by different widgets. Also, check with layouts in different layout-qualifiers folders for the same condition.
I created simple project with list view. On emulator it works well. The style of Action Bar by default AppTheme. But when I generate new style through http://jgilfelt.github.io/android-actionbarstylegenerator resource and copy it in my simple project it crashes. I don't know what I do wrong. I use AndroidStudio.
The process of pasting new style:
I copy downloaded files in res/ folder, then in my project I change Manifest file from AppTheme to Theme.Inter (like in my example). Next step, I copy data from style_Inter to style. That's it. I run it and it crashes.
Log:
06-16 13:41:27.912 7251-7251/com.examples.nick.listviewexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.examples.nick.listviewexample/com.examples.nick.listviewexample.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.examples.nick.listviewexample.MainActivity.onCreate(MainActivity.java:30)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Thanks, your advice really helped me, Especially maddesa! It was simple, when I generated custom Action Bar I had to chose in Style compatibility AppCompat.
I have to set a String value dynamically to a textview in android, but unfortunately I got null value error but in the log I can print the value of String.So I am sure that the string is not null but pretty large.
here is my logcat results:
2-06 16:05:39.930 11631-11631/sarath.com.reachmeE/from_address﹕ Sub Jail Rd, Talap
Kannur, Kerala 670002
11.877453, 75.372992
12-06 16:05:39.940 11631-11631/sarath.com.reachmeE/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{sarath.com.reachme/sarath.com.reachme.app.Mapview}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4517)
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:993)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at sarath.com.reachme.app.Mapview.onCreate(Mapview.java:95)
at android.app.Activity.performCreate(Activity.java:4470)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
at android.app.ActivityThread.access$600(ActivityThread.java:128)
here is my code snippet:
Log.e("addrs short", String.valueOf(from_address));
address.setText(String.valueOf(from_address));
Is there any limit in size/length for the string which can displayed by the textview ?
NO limits on the size of the string. almost sure that the error is that address TextView is null, this may happen if you do findElementById(R.id.your_address_id); but the View (R.id.your_address_id) is in another Activity.
may be you not give any reference to your address field that's why you getting this error.
address = (TextView) findElementById(R.id.your_address_id);
I am having problem when starting com.facebook.katana.ProxyAuth. The activity that is starting this intent is defined in manifest file with android:launchMode="singleInstance"
I've solved this by removing launch mode as single instance, this intent is started successfully.
I really need this activity to be single instance, so does anyone know how to solve this problem without removing android:launchMode="singleInstance"?
Intent intent = new Intent();
intent.setClassName("com.facebook.katana",
"com.facebook.katana.ProxyAuth");
intent.putExtra("client_id", applicationId);
// Verify that the application whose package name is
// com.facebook.katana.ProxyAuth
// has the expected FB app signature.
if (!validateActivityIntent(activity, intent)) {
return false;
}
// activity is defined in manifest as single instance
activity.startActivityForResult(intent, activityCode);
LogCat:
05-14 16:42:13.470: E/AndroidRuntime(28141): FATAL EXCEPTION: main
05-14 16:42:13.470: E/AndroidRuntime(28141): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.facebook.katana/com.facebook.katana.ProxyAuth}: java.lang.NullPointerException
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2787)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2803)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2136)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.os.Looper.loop(Looper.java:144)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread.main(ActivityThread.java:4937)
05-14 16:42:13.470: E/AndroidRuntime(28141): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 16:42:13.470: E/AndroidRuntime(28141): at java.lang.reflect.Method.invoke(Method.java:521)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
05-14 16:42:13.470: E/AndroidRuntime(28141): at dalvik.system.NativeStart.main(Native Method)
05-14 16:42:13.470: E/AndroidRuntime(28141): Caused by: java.lang.NullPointerException
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.facebook.orca.common.util.Base64.c(Base64.java:497)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.facebook.orca.common.util.Base64.b(Base64.java:459)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.facebook.katana.ProxyAuth.b(ProxyAuth.java:36)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.facebook.katana.activity.PlatformDialogActivity.a(PlatformDialogActivity.java:127)
05-14 16:42:13.470: E/AndroidRuntime(28141): at com.facebook.orca.activity.FbFragmentActivity.onCreate(FbFragmentActivity.java:48)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069)
05-14 16:42:13.470: E/AndroidRuntime(28141): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
05-14 16:42:13.470: E/AndroidRuntime(28141): ... 11 more
05-14 16:42:15.520: E/ActivityManager(114): Error running process
05-14 16:42:15.520: E/ActivityManager(114): java.io.IOException: Error running exec(). Command: [/system/xbin/procrank] Working Directory: null Environment: [ANDROID_SOCKET_zygote=11, ANDROID_BOOTLOGO=1, EXTERNAL_STORAGE=/mnt/sdcard, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, PATH=/sbin:/system/sbin:/system/bin:/system/xbin, ANDROID_DATA=/data, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/com.htc.framework.jar:/system/framework/com.htc.android.pimlib.jar:/system/framework/com.htc.android.easopen.jar:/system/framework/com.scalado.util.ScaladoUtil.jar, ANDROID_PROPERTY_WORKSPACE=10,65536, ANDROID_ROOT=/system, LD_LIBRARY_PATH=/system/lib]
05-14 16:42:15.520: E/ActivityManager(114): at java.lang.ProcessManager.exec(ProcessManager.java:226)
05-14 16:42:15.520: E/ActivityManager(114): at java.lang.ProcessBuilder.start(ProcessBuilder.java:201)
05-14 16:42:15.520: E/ActivityManager(114): at com.android.server.am.ActivityManagerService.logProcessResult(ActivityManagerService.java:9437)
05-14 16:42:15.520: E/ActivityManager(114): at com.android.server.am.ActivityManagerService.access$900(ActivityManagerService.java:155)
05-14 16:42:15.520: E/ActivityManager(114): at com.android.server.am.ActivityManagerService$9.run(ActivityManagerService.java:9614)
05-14 16:42:15.520: E/ActivityManager(114): Caused by: java.io.IOException: No such file or directory
05-14 16:42:15.520: E/ActivityManager(114): at java.lang.ProcessManager.exec(Native Method)
05-14 16:42:15.520: E/ActivityManager(114): at java.lang.ProcessManager.exec(ProcessManager.java:224)
05-14 16:42:15.520: E/ActivityManager(114): ... 4 more
It may not be the answer that you need, but you may try android:launchMode="singleTask", it behaves like singleInstance in many cases and works with the facebook authentication. See Android singleTask or singleInstance launch mode?
HI here is a small code i picked it from the sample code of google analytics.
tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession("UA-YOUR-ACCOUNT-HERE", this);
setContentView(R.layout.main);
Button createEventButton = (Button)findViewById(R.id.NewEventButton);
createEventButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tracker.trackEvent(
"Clicks", // Category
"Button", // Action
"clicked", // Label
77); // Value
}
});
Error:
'05-14 13:52:36.599: E/AndroidRuntime(7367): FATAL EXCEPTION: main
05-14 13:52:36.599: E/AndroidRuntime(7367): java.lang.NoClassDefFoundError: com.google.android.apps.analytics.GoogleAnalyticsTracker
05-14 13:52:36.599: E/AndroidRuntime(7367): at com.google.android.apps.analytics.sample.TestActivity.onCreate(TestActivity.java:19)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.os.Looper.loop(Looper.java:130)
05-14 13:52:36.599: E/AndroidRuntime(7367): at android.app.ActivityThread.main(ActivityThread.java:3687)
05-14 13:52:36.599: E/AndroidRuntime(7367): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 13:52:36.599: E/AndroidRuntime(7367): at java.lang.reflect.Method.invoke(Method.java:507)
05-14 13:52:36.599: E/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-14 13:52:36.599: E/AndroidRuntime(7367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-14 13:52:36.599: E/AndroidRuntime(7367): at dalvik.system.NativeStart.main(Native Method)
To solve the problem with the error NoClassdefFoundError when you are using the Google Analytics v2beta library, you need to mark this library as "exported".
How?
Add your library: Project -> Properties -> Java Build Path -> Libraries -> Add External JARs...
Then go to "Order and Export" in the same window, and mark this library as "exported" with the checkbox.
Your proyect will now find the Analytics class when you run it!
More details and why this happen here
I had this problem after updating ADT.
I was storing all of my JAR files in a folder called "lib" and adding the jars to the build path the normal Eclipse way. This worked fine until my update.
After my update, I was getting the NoClassDefFoundError for a class that I could clearly see was included in the jar (checking out the ReferencedLibraries classes).
The solution was to remove my jars from the build path and rename my "lib" folder to "libs". This is an ant convention, and seems to be the way the new ADT finds and includes libraries in an .apk file. Once I did this, everything worked fine.