2013-03-11 67 views
1

我是新來的Android編程,並試圖爲我的應用程序(取自Android教程網站)創建一個新的主要活動。我原來的主要活動被稱爲「MainActivity」。我想成爲我的主要活動的新活動稱爲「主頁」,並且它應該包含一個在點擊時會產生「MainActivity」的按鈕。我不確定我應該在清單中包含關於新頁面「主頁」,homepage.xml和按鈕的信息。具體代碼將不勝感激。創建新的Android MainActivity的問題

主頁:

package com.myphoneapp; 

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

public class HomePage extends Activity { 

    private Button ScheduleBtn; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.homepage); 

     ScheduleBtn = (Button) findViewById(R.id.home_btn); 

     ScheduleBtn.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       Intent myIntent = new Intent(HomePage.this, MainActivity.class); 
       HomePage.this.startActivity(myIntent); 


      } 
     }); 
    } 


} 

homepage.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <Button 

    android:layout_width="wrap_content" 

    android:layout_height="wrap_content" 

    android:text="Welcome to ClearLight" 

    android:id="@+id/home_btn" 

    /> 

</LinearLayout> 

MainActivity:

package com.myphoneapp; 

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


    public class MainActivity extends Activity { 

    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // 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); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_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); 
     startActivity(intent); 
    } 

    } 

清單:

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

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.myphoneapp.MainActivity" 
      android:label="@string/title_activity_main" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.myphoneapp.DisplayMessageActivity" 
      android:label="@string/title_activity_display_message" 
      android:parentActivityName="com.example.myphoneapp.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.myphoneapp.MainActivity" /> 
     </activity> 
     <activity 
      android:name="com.myphoneapp.HomePage" 
      android:label="@string/homepage" android name="MainActivity" 
      android:parentActivityName="com.example.myphoneapp.MainActivity" > 

      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.myphoneapp.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 

回答

1

要使HomePage成爲您的第一個活動,請編輯您的Manifest文件,使其具有action.MAIN的意圖過濾器。你不必在Manifest文件中定義任何關於佈局的東西。只有活動的聲明(你已經有了)

所以,你的新的清單文件看起來像

<application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.myphoneapp.MainActivity" 
      android:label="@string/title_activity_main" > 

     </activity> 
     <activity 
      android:name="com.myphoneapp.DisplayMessageActivity" 
      android:label="@string/title_activity_display_message" 
      android:parentActivityName="com.example.myphoneapp.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.myphoneapp.MainActivity" /> 
     </activity> 
     <activity 
      android:name="com.myphoneapp.HomePage" 
      android:label="@string/homepage" 
      android:parentActivityName="com.example.myphoneapp.MainActivity" > 

      <!-- Move the intent filter to HomePage --> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 

      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.myphoneapp.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 

而且按鈕開始mainActivity,你已經做了,在HomePage.java

ScheduleBtn = (Button) findViewById(R.id.home_btn); 
ScheduleBtn.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent myIntent = new Intent(HomePage.this, MainActivity.class); 
     HomePage.this.startActivity(myIntent); 
    } 
}); 

此代碼(取自您的HomePage.java從意圖打開MainActivity

+0

嗨感謝您的幫助,我用你的代碼已經提供給我但是我仍然收到這行的錯誤:'android:label =「@ string/homepage」android:name =「MainActivity」' – Nick 2013-03-11 14:59:30

+0

如果你可以在你的問題中發佈確切的錯誤,那會很好 – 2013-03-11 15:01:00

+0

不應該在那裏。我已經修復了我的代碼。對不起 – 2013-03-11 15:01:50

0

更改您的清單文件,像這樣

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.myphoneapp.MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <activity 
     android:name=".HomePage" 

    </activity> 
</application> 

0
清單文件的首頁的活動必須是

您的主要活動,以便您的清單應該是這樣的:

<activity 
     android:name="com.myphoneapp.MainActivity" 
     android:label="@string/homepage" android name="MainActivity" 
     android:parentActivityName="com.example.myphoneapp.MainActivity" > 

     <meta-data 
      android:name="android.support.PARENT_ACTIVITY" 
      android:value="com.example.myphoneapp.MainActivity" /> 
    </activity> 
<activity 
     android:name="com.myphoneapp.HomePage" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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