应用程序在使用SherlockFragmentActivity时崩溃

时间:2013-10-10 05:57:35

标签: android actionbarsherlock android-studio

我正在使用ABS。我的启动器活动是RegistrationActivity。当我使用SherlockActivity进行扩展时,我的应用运行正常。但是当我用SherlockFragmentActivity扩展它时,我的应用程序在手机上运行时崩溃了。

 package com.example.app;

 import android.content.Context; 
 import android.content.Intent; 
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.support.v4.app.FragmentManager; 
 import android.view.View;
 import android.support.v4.app.DialogFragment;
 import com.actionbarsherlock.app.SherlockFragmentActivity;

 public class RegistrationActivity extends SherlockFragmentActivity {

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

         // Check if the user is already registered
         Context context = this;
         SharedPreferences settings = context.getSharedPreferences( "settings" , Context.MODE_PRIVATE);
         int isRegistered = settings.getInt( "isRegistered", 0 );

         if( isRegistered == 1 ) {
             Intent intent = new Intent(this, DashboardActivity.class);
             startActivity(intent);
         }
         else {
             setContentView(R.layout.activity_registration);
             getSupportActionBar().setDisplayHomeAsUpEnabled(true);
         }

     }

    public void pickCountry(View view) {

     }

 }

我在MAC上使用AndroidStudio。

10-10 10:53:38.684    7751-7751/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.app/com.example.app.RegistrationActivity}: java.lang.ClassNotFoundException: com.example.app.RegistrationActivity
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1995)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: com.example.app.RegistrationActivity
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1986)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
            at android.app.ActivityThread.access$600(ActivityThread.java:138)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:213)
            at android.app.ActivityThread.main(ActivityThread.java:4787)
            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:789)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
            at dalvik.system.NativeStart.main(Native Method)

我甚至在AndroidManifest.xml

中有活动
    <activity
        android:name="com.example.app.RegistrationActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

2 个答案:

答案 0 :(得分:6)

我在我的日食中运行你的代码,事实上,它崩溃了。但我解决了这个问题:

<activity
    android:name="com.example.app.RegistrationActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

由此:

<activity
    android:name="com.example.app.RegistrationActivity"
    android:theme="@style/Theme.Sherlock"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

告诉我这是否可以解决您的问题。

答案 1 :(得分:0)

如果不在清单中,则在开始活动时设置主题

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_Sherlock);
    super.onCreate(savedInstanceState);
    ...
}

清理/重建项目。

Eclipse:Project -> Clean

Android Studio:Build -> Rebuild Project

Ant:$ ant clean debug