如何使活动设置正常工作

时间:2015-05-25 21:12:32

标签: android android-studio

我正在尝试使用Android Studio 1.2.1.1进行非常简单的应用我的应用程序应该有两个活动 - main_activity和设置活动,当用户按下左键时显示(我不知道它是如何调用的 - 菜单?)和设置选项出现。

我不能使用模拟器(我不知道为什么 - HAXModule存在一些问题 - 我无法在BIOS(HP BIOS sux)中打开它 - 所以我使用了我的Galaxy S Advance设备。

我正在使用安装向导 - 启动新的Android项目: enter image description here

接下来我选择Jelly Bean 4.1(因为我的手机上有4.1.2) enter image description here

然后空白活动(也许我应该使用片段空白活动?) enter image description here

主要活动无变化: enter image description here

由于oF Gradle Issue,我将charset更改为UTF-8 enter image description here

现在,当我运行这个应用程序时 - 它有效 - 它显示了Hello World,当我按下菜单按钮时,我可以看到设置,但按下什么都没做....

让我们添加SettingsActivity文件/新建/活动/设置活动。在这里,我将Hierarchical Parent更改为MainActivity

enter image description here

我改变方法onOptionsItemSelected:

public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

因为无法解析Intent符号,我按ALt + Enter添加Intent符号。

但现在当我在手机上运行时,按设置会导致错误:应用已停止(或者像这样)

的AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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=".SettingsActivity"
            android:label="@string/title_activity_settings"
            android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="local.test.testso.MainActivity" />
        </activity>
    </application>

</manifest> 

Logcat没有显示任何内容

我做错了什么?

0 个答案:

没有答案
相关问题