2013-09-10 272 views
1

我想跟着官方的Android教程(http://developer.android.com/training/basics/firstapp/starting-activity.html),但我很失敗。一切正常,直到我需要添加第二個活動。首先,我在Android Studio中創建的活動不會顯示在我的java文件列表中。我點擊了New - > Activity並沒有出現任何內容。爲了解決這個問題,我打開了Windows資源管理器,並將MainActivity.java複製/重命名爲DisplayMessageActivity.java,並在教程中添加了代碼。Android Studio myfirstapp中的錯誤: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> 

回答

0

當您創建活動,請確保您將它插入到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> 
+0

嗯...仍然收到錯誤。我編輯你的代碼到'android:name =「com.example.myfirstapp.DisplayMessageActivity」/>'但我仍然得到相同的錯誤 – winston

+0

確保你的java文件和ClassName具有相同的名稱 – 2013-09-11 02:28:54

-1

通過教程去吧,你會教後創建一個。

0

在你得到的每個錯誤上(如圖所示),然後按Alt + Enter導入缺失的類或聲明變量。

請注意,如果沒有錯誤,所有文件(.java)必須不帶紅色下劃線。

Error

After Alt+Enter class imported. No error.

0

也許有些tutorial's classes's的名稱改變。那麼你需要把您的活動命名變量「extra_message」之前那樣MyActivity.EXTRA_MESSAGE

上的主要活動放... 公共最後靜態字符串EXTRA_MESSAGE =「com.example.myfirstapp.MESSAGE」;

或簡單使用任何值。像這樣 String message = intent.getStringExtra('xxx');