无法从NavigationDrawerFragmet启动首选项屏幕

时间:2014-09-22 13:51:38

标签: android navigation-drawer preferences

我已经从File> New> Android Application Project创建了NavigationDrawer活动。我选择导航抽屉活动,一切顺利,直到我需要启动首选项屏幕。应用程序崩溃 到目前为止我所拥有的:

主要活动:

...
case R.id.bSettings:
Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
StartActivity(settingsActivity);
break;
...

Preferences.java:

public class Preferences extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    }
}

请注意,我不想要内联首选项,而是喜欢新活动。

09-22 13:57:57.125:W / EGL_genymotion(2120):eglSurfaceAttrib未实现 09-22 13:57:57.125:E / OpenGLRenderer(2120):从GradienCache获取MAX_TEXTURE_SIZE 09-22 13:57:57.137:E / OpenGLRenderer(2120):从Caches :: initConstraints()获取MAX_TEXTURE_SIZE 09-22 13:57:57.137:D / OpenGLRenderer(2120):启用调试模式0 09-22 13:58:00.245:D / AndroidRuntime(2120):关闭VM 09-22 13:58:00.249:W / dalvikvm(2120):threadid = 1:线程退出未捕获的异常(group = 0xa4ba8648) 09-22 13:58:00.273:E / AndroidRuntime(2120):致命异常:主要 09-22 13:58:00.273:E / AndroidRuntime(2120):android.content.ActivityNotFoundException:无法找到显式活动类{ba.arnes.aconverter / ag.android.aconvert.Preferences};你有没有在AndroidManifest.xml中声明这个活动? 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1424) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Activity.startActivityForResult(Activity.java:3390) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Activity.startActivityForResult(Activity.java:3351) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Activity.startActivity(Activity.java:3587) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.Activity.startActivity(Activity.java:3555) 09-22 13:58:00.273:E / AndroidRuntime(2120):at ag.android.aconvert.MainActivity.onClick(MainActivity.java:545) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.view.View.performClick(View.java:4240) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.view.View $ PerformClick.run(View.java:17721) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.os.Handler.handleCallback(Handler.java:730) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.os.Handler.dispatchMessage(Handler.java:92) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.os.Looper.loop(Looper.java:137) 09-22 13:58:00.273:E / AndroidRuntime(2120):在android.app.ActivityThread.main(ActivityThread.java:5103) 09-22 13:58:00.273:E / AndroidRuntime(2120):at java.lang.reflect.Method.invokeNative(Native Method) 09-22 13:58:00.273:E / AndroidRuntime(2120):at java.lang.reflect.Method.invoke(Method.java:525) 09-22 13:58:00.273:E / AndroidRuntime(2120):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737) 09-22 13:58:00.273:E / AndroidRuntime(2120):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 09-22 13:58:00.273:E / AndroidRuntime(2120):at dalvik.system.NativeStart.main(Native Method) 09-22 13:58:01.841:I /处理(2120):发送信号。 PID:2120 SIG:9

2 个答案:

答案 0 :(得分:2)

您缺少AndroidManifest.xml中的活动声明。

正如它所说:

13:58:00.273: E/AndroidRuntime(2120): android.content.ActivityNotFoundException: Unable to find explicit activity class {ba.arnes.aconverter/ag.android.aconvert.Preferences}; have you declared this activity in your AndroidManifest.xml

无法找到偏好设置

编辑:

您似乎正在尝试启动Activity,并且您的偏好设置类为Fragment。所以基本上你需要一个Activity类,在布局中你有片段的占位符(例如 Preferences class )。

public class PrefenecesActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle s) {
        super.onCreate(s);
        setContentView(R.layout.preferences);

        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.holder, new PreferencesFrag());
        transaction.commit();
    }

    public static class PreferencesFrag extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.preferences);
        }
    }
}

布局可能如下所示:

<FrameLayout
    android:id="@+id/holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

不要忘记将活动添加到AndroidManifest.xml:

<activity android:name=".PrefenecesActivity" />

然后你可以使用你的代码:

case R.id.bSettings:
Intent settingsActivity = new Intent(getBaseContext(), PrefenecesActivity.class);
startActivity(settingsActivity);
break;

答案 1 :(得分:0)

vilpe89:谢谢你!你有一点点改变......

AndroidManifest.xml应为:

<activity android:name="add.full.name.PrefenecesActivity" />

而不是:

<activity android:name=".PrefenecesActivity" />