如何连接活动(android studio 1.2)

时间:2015-05-13 22:42:22

标签: android android-studio

最大的问题是大多数教程都适用于早期版本的Android - 而且这些解决方案根本不适用于AS1.2。并且#34;弃用" Android新手非常困惑。

我尝试做一个非常简单的应用 - 只有一个菜单选项的MainActivity - 设置。

所以我创建了新项目(模板Blank Activity),然后是New - >活动 - >设置活动。当我运行应用程序时,主要活动出现在屏幕上,菜单有一个选项设置 - 但它不起作用 - 期望什么,因为我没有向MainActivity添加任何代码。但经过5天的挖掘和复制粘贴"解决方案"我仍然在同一点上。没有反应 - 我还有两个没有连接的活动。

我确定我在这里添加了一些代码:

 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) {

   // TB: HERE Should Setting Activity be shown
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

我发现的最佳代码(最清晰):

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

但是 - 没有反应......

*已更新*

清单:

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

    <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="pl.b6a.test4.MainActivity" />
        </activity>
    </application>

</manifest>

*已更新2 *

我发现按“设置”菜单项应用程序不会做任何事情,但会崩溃......也许这是一个提示?

*日志* Gradle控制台:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature.
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72211Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42211Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest
:app:processDebugResources
:app:generateDebugSources
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:preDexDebug UP-TO-DATE
:app:dexDebug
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug

BUILD SUCCESSFUL

Total time: 24.012 secs

ADB LOGS:

Waiting for device.
Target device: samsung-gt_i9070-FExxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Uploading file
    local path: C:\Users\tombrz\AndroidStudioProjects\Test4\app\build\outputs\apk\app-debug.apk
    remote path: /data/local/tmp/pl.b6a.test4
No apk changes detected. Skipping file upload, force stopping package instead.
DEVICE SHELL COMMAND: am force-stop pl.b6a.test4
Launching application: pl.b6a.test4/pl.b6a.test4.MainActivity.
DEVICE SHELL COMMAND: am start -n "pl.b6a.test4/pl.b6a.test4.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=pl.b6a.test4/.MainActivity }

1 个答案:

答案 0 :(得分:0)

删除

返回true

来自if子句的

{ } 把它带到外面 或者只是把它弄错