Unable to instantiate an Activity
I have a problem that I am implementing application with spring android, I added a class in android:name attribute of Application tag of AndroidManifest file of Android project. But it gives error that unable to instantiate an activity or ClassNotFoundException.
error:
05-13 11:11:52.504: ERROR/AndroidRuntime(579): java.lang.RuntimeException: Unable to instantiate application org.shopzilla.android.MainApplication: java.lang.ClassNotFoundException: org.shopzilla.android.MainApplication in loader dalvik.system.PathClassLoader[/data/app/org.shopzilla.android.common-1.apk]
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.LoadedApk.makeApplication(LoadedApk.java:466)开发者_Python百科
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.ActivityThread.access$2200(ActivityThread.java:117)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.os.Looper.loop(Looper.java:123)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at java.lang.reflect.Method.invoke(Method.java:507)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at dalvik.system.NativeStart.main(Native Method)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): Caused by: java.lang.ClassNotFoundException: org.shopzilla.android.MainApplication in loader dalvik.system.PathClassLoader[/data/app/org.shopzilla.android.common-1.apk]
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
05-13 11:11:52.504: ERROR/AndroidRuntime(579): at android.app.LoadedApk.makeApplication(LoadedApk.java:461)
Code: MyApplication.java
package org.shopzilla.android.common;
import org.springframework.security.crypto.encrypt.AndroidEncryptors;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.sqlite.SQLiteConnectionRepository;
import org.springframework.social.connect.sqlite.support.SQLiteConnectionRepositoryHelper;
import org.springframework.social.connect.support.ConnectionFactoryRegistry;
import org.springframework.social.facebook.api.FacebookApi;
import org.springframework.social.facebook.connect.FacebookConnectionFactory;
import org.springframework.social.twitter.api.TwitterApi;
import org.springframework.social.twitter.connect.TwitterConnectionFactory;
import android.app.Application;
import android.database.sqlite.SQLiteOpenHelper;
public class MainApplication extends Application
{
private ConnectionFactoryRegistry _connectionFactoryRegistry;
private SQLiteOpenHelper _repositoryHelper;
private ConnectionRepository _connectionRepository;
//***************************************
// Application Methods
//***************************************
@Override
public void onCreate()
{
// create a new ConnectionFactoryLocator and populate it with Facebook and Twitter ConnectionFactories
_connectionFactoryRegistry = new ConnectionFactoryRegistry();
_connectionFactoryRegistry.addConnectionFactory(new FacebookConnectionFactory(getFacebookAppId(), getFacebookAppSecret()));
_connectionFactoryRegistry.addConnectionFactory(new TwitterConnectionFactory(getTwitterConsumerToken(), getTwitterConsumerTokenSecret()));
// set up the database and encryption
_repositoryHelper = new SQLiteConnectionRepositoryHelper(this);
_connectionRepository = new SQLiteConnectionRepository(getLocalUserId(), _repositoryHelper, _connectionFactoryRegistry, AndroidEncryptors.text("password", "5c0744940b5c369b"));
}
//***************************************
// Private methods
//***************************************
private String getLocalUserId()
{
return getString(R.string.local_user_id);
}
private String getFacebookAppId()
{
return getString(R.string.facebook_app_id);
}
private String getFacebookAppSecret()
{
return getString(R.string.facebook_app_secret);
}
private String getTwitterConsumerToken()
{
return getString(R.string.twitter_consumer_key);
}
private String getTwitterConsumerTokenSecret()
{
return getString(R.string.twitter_consumer_key_secret);
}
//***************************************
// Public methods
//***************************************
public ConnectionRepository getConnectionRepository()
{
return _connectionRepository;
}
public FacebookConnectionFactory getFacebookConnectionFactory()
{
return (FacebookConnectionFactory) _connectionFactoryRegistry.getConnectionFactory(FacebookApi.class);
}
public TwitterConnectionFactory getTwitterConnectionFactory()
{
return (TwitterConnectionFactory) _connectionFactoryRegistry.getConnectionFactory(TwitterApi.class);
}
}
I also found that this can happen when you have two Android projects in your workspace which have the same package name.
I have two source snapshots of an app. I was comparing their behaviour and was getting the ClassNotFoundException. I managed to get around this by right-clicking on one and selecting 'Close Project' so that only one is actively loaded in Eclipse at once. It all worked after that.
Your application class is org.shopzilla.android.common.MainApplication but from the stack trace it appears that the system is looking for class org.shopzilla.android.MainApplication (missing the sub-package 'common').
You probably specified <application android:name=".MainApplication" ... >
in AndroidManifest.xml. Change it in either android:name=".common.MainApplication"
or specify the full class name with android:name="org.shopzilla.android.common.MainApplication"
.
精彩评论