Android在' onNavigationItemSelected'上显示布局来自导航抽屉

时间:2015-10-30 10:24:19

标签: java android android-layout android-activity

我从Android Studio

创建了一个Empty项目

从左侧的导航抽屉中,我编辑了这样的控件

<group android:checkableBehavior="single">
    <item android:id="@+id/nav_blog"
        android:title="Blog" />
    <item android:id="@+id/nav_task"
        android:title="Task" />
</group>

我点击nav_blog

我将此作为活动

if (id == R.id.nav_blog) {
        setContentView(R.layout.layout_blog);
    } else if (id == R.id.nav_task) {
setContentView(R.layout.layout_task);
    }

res / layout

我有nav_task.xml和layout_task.xml

但是当我点击导航抽屉上的侧栏时,我的应用程序总是在模拟器中崩溃并显示此日志

我在做什么错误..

当我点击导航抽屉上的项目时,如何显示布局。

10-30 15:28:29.010 945-945/? E/libEGL: called unimplemented OpenGL ES API
10-30 15:28:29.010 945-945/? E/SurfaceFlinger: glCheckFramebufferStatusOES error 1907239303
10-30 15:28:29.010 945-945/? E/SurfaceFlinger: got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot
10-30 15:28:30.610 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:32.170 1307-1561/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.sulthan.sa/.SysMain (has extras)} from uid 10007 on display 0
10-30 15:28:34.120 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:35.210 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:37.290 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:38.460 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:40.320 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:28:41.050 1307-1465/? W/AudioTrack: AUDIO_OUTPUT_FLAG_FAST denied by client
10-30 15:34:00.810 1307-1334/? W/BatteryStatsImpl: Couldn't get kernel wake lock stats
10-30 15:35:11.810 1307-1307/? I/EntropyMixer: Writing entropy...
10-30 15:37:00.020 1307-1328/? I/ProcessStatsService: Prepared write state in 0ms
10-30 15:38:16.770 3761-3761/? D/AndroidRuntime: CheckJNI is ON
10-30 15:38:16.790 3761-3761/? E/memtrack: Couldn't load memtrack module (No such file or directory)
10-30 15:38:16.790 3761-3761/? E/android.os.Debug: failed to load memtrack module: -2
10-30 15:38:16.810 2278-2293/? D/DefContainer: Copying /data/local/tmp/com.example.sulthan.sa to base.apk
10-30 15:38:16.880 1307-1338/? D/PackageManager: Renaming /data/app/vmdl1512606718.tmp to /data/app/com.example.sulthan.sa-2
10-30 15:38:16.890 1307-1328/? W/ActivityManager: Force removing ActivityRecord{1d72e8d0 u0 com.example.sulthan.sa/.SysMain t37}: app died, no saved state
10-30 15:38:16.960 3772-3772/? E/cutils-trace: Error opening trace file: Permission denied (13)
10-30 15:38:16.960 3772-3772/? I/dex2oat: /system/bin/dex2oat --zip-fd=5 --zip-location=/data/app/com.example.sulthan.sa-2/base.apk --oat-fd=6 --oat-location=/data/dalvik-cache/x86_64/data@app@com.example.sulthan.sa-2@base.apk@classes.dex --instruction-set=x86_64 --instruction-set-features=default --runtime-arg -Xms64m --runtime-arg -Xmx512m
10-30 15:38:17.390 3772-3772/? W/dex2oat: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-30 15:38:18.920 3772-3772/? I/dex2oat: dex2oat took 1.960s (threads: 1)

在主要活动中

更新:

这是AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sulthan.sa" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".SysMain"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

我想我应该添加一些内容。但我不清楚。

我正在做的错误是什么?如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你做错了。首先创建片段,然后onNavigationItemSelected调用这些片段。 您可以按照本文进行参考。 Using Fragments with the Navigation Drawer Activity