【问题标题】:Errors in Android Studio myfirstapp : Gradle errorsAndroid Studio myfirstapp 中的错误:Gradle 错误
【发布时间】:2013-09-14 18:14:46
【问题描述】:

我正在尝试按照官方 Android 教程 (http://developer.android.com/training/basics/firstapp/starting-activity.html) 进行操作,但我失败得很惨。一切正常,直到我需要添加第二个活动。首先,我在 Android Studio 中创建的 Activity 没有出现在我的 java 文件列表中。我单击了新建-> 活动,但没有出现任何内容。为了解决这个问题,我打开了 Windows 资源管理器并将 MainActivity.java 复制/重命名为 DisplayMessageActivity.java 并在教程中添加了代码。

跟随后应用程序无法运行,并且出现多个“Gradle”错误,例如:

Gradle: error: cannot find symbol class Activity
Gradle: error: cannot find symbol class Bundle

我需要修复什么才能让它运行?以下是相关代码:

MainActivity.java

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.widget.EditText;


public class MainActivity extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /** Called when the user clicks the Send button */
    public void sendMessage(View view) {
        // Do something in response to button
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        String message = editText.getText().toString();
        intent.putExtra(EXTRA_MESSAGE, message);
    }

}

显示 MessageActivity.java

package com.example.myfirstapp;

public class DisplayMessageActivity extends Activity {

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_message);

        // Get the message from the intent
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);

        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.example.myfirstapp.MainActivity"
            android:label="@string/app_name"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

    标签: java android android-activity android-studio android-gradle-plugin


    【解决方案1】:

    当你创建一个活动时,确保你也将它插入到 mainifest.xml 中,所以你的清单应该是这样的:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myfirstapp"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="7"
            android:targetSdkVersion="16" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
        <activity
            android:name="com.example.myfirstapp.MainActivity"
            android:label="@string/app_name"
            android:parentActivityName="com.example.myfirstapp.MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.myfirstapp.MainActivity" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.myfirstapp.MessageActivity>
        </activity>
    </application>
    
    </manifest>
    

    【讨论】:

    • 嗯...仍然出现错误。我将您的代码编辑为android:name="com.example.myfirstapp.DisplayMessageActivity" /&gt;,但仍然出现相同的错误
    • 确保您的 java 文件和 ClassName 具有相同的名称
    【解决方案2】:

    在遇到每个错误时(如图所示为红色),然后按 Alt+Enter 导入缺失的类或声明变量。

    请注意,所有文件 (.java) 必须没有红色下划线,如果它们没有错误。

    【讨论】:

      【解决方案3】:

      也许某些教程的课程名称已更改。那么你需要把你的活动名称放在变量“extra_message”之前,比如 MyActivity.EXTRA_MESSAGE

      在主 Activity 上放置... public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

      或者简单地使用任何值。像那样 字符串消息 = intent.getStringExtra('xxx');

      【讨论】:

        【解决方案4】:

        继续阅读教程,稍后将教您创建一个。

        【讨论】:

          猜你喜欢
          • 2017-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多