无法实例化活动

时间:2011-05-13 11:19:26

标签: android

我有一个问题,我正在使用spring android实现应用程序,我在Android项目的AndroidManifest文件的Application标签的android:name属性中添加了一个类。 但是它给出了无法实例化活动或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)
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)

代码: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);
    }
}

2 个答案:

答案 0 :(得分:2)

我还发现,当你的工作区中有两个 Android项目具有相同的包名时,就会发生这种情况。

我有一个应用程序的两个源快照。我正在比较他们的行为并获得ClassNotFoundException。我设法通过右键单击一个并选择“关闭项目”来解决这个问题,这样一次只能在Eclipse中主动加载一个。之后一切都奏效了。

答案 1 :(得分:0)

你的应用程序类是org.shopzilla.android.common.MainApplication但是从堆栈跟踪看来系统正在寻找类org.shopzilla.android.MainApplication(缺少子包'common')。

您可能在AndroidManifest.xml中指定了<application android:name=".MainApplication" ... >。在android:name=".common.MainApplication"中更改它,或使用android:name="org.shopzilla.android.common.MainApplication"指定完整的班级名称。