2014-02-13 139 views
0

我開發了三項活動。我想通過點擊main_activity中的兩個不同的按鈕(註冊按鈕和登錄按鈕)分別進行regfrm活動和登錄活動。我的問題是當我運行我的項目時,它不會導航,而點擊按鈕註冊regfrm活動。如何在單擊同一活動中的兩個不同按鈕時導航到兩個不同的活動?

主activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/pizza1" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="90dp" 
     android:text="REGISTER" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button1" 
     android:layout_alignBottom="@+id/button1" 
     android:layout_marginLeft="54dp" 
     android:layout_toRightOf="@+id/button1" 
     android:text="LOGIN" /> 

</RelativeLayout> 

主要活動的Java代碼

package com.example.pizza; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

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

     public void addButtonClickEventListener() { 
     // TODO Auto-generated method stub 
     Button button =(Button)findViewById(R.id.button1); 
     Button button1 =(Button)findViewById(R.id.button2); 
     final Context context = MainActivity.this; 
     button1.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent (context,RegActivity.class); 
       Intent intent1 = new Intent (context,loginActivity.class); 
       startActivity(intent); 
       startActivity(intent1); 
      } 

     }); 
     } 

    @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; 
    } 

} 

// regfrm acivity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:background="@drawable/view" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".RegActivity" > 
     <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/button4" 
     android:layout_alignLeft="@+id/textView5" 
     android:text="SUBMIT" /> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView3" 
     android:layout_alignBottom="@+id/textView3" 
     android:layout_alignRight="@+id/editText1" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView3" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
     android:inputType="textPersonName" /> 

    <EditText 
     android:id="@+id/editText3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView4" 
     android:layout_alignBottom="@+id/textView4" 
     android:layout_alignParentRight="true" 
     android:ems="10" 
     android:inputType="textEmailAddress" /> 

    <EditText 
     android:id="@+id/editText4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textView5" 
     android:ems="10" 
     android:inputType="textPostalAddress" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/editText4" 
     android:layout_below="@+id/editText4" 
     android:layout_marginTop="80dp" 
     android:text="CANCEL" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="26dp" 
     android:text="PASSWORD" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="22dp" 
     android:text="REGISTRATION FORM" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView2" 
     android:text="USER NAME" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView5" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/textView4" 
     android:layout_marginTop="58dp" 
     android:text="ADDRESS" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/textView5" 
     android:layout_below="@+id/editText2" 
     android:layout_marginTop="48dp" 
     android:text="E-MAIL ID" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

//註冊活動Java代碼

package com.example.pizza; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 


public class RegActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstancestate) 
    { 
     super.onCreate(savedInstancestate); 
     setContentView(R.layout.regfrm); 
     addButtonOnClickEventListener(); 

    } 

    private void addButtonOnClickEventListener() 
    { 
     // TODO Auto-generated method stub 
     Button button =(Button)findViewById(R.id.button3); 
     final Context context = RegActivity.this; 
     button.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent (context,WelActivity.class); 
       startActivity(intent); 
      } 

     }); 
     } 
    @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; 
    } 

} 
// login activity   
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="#ffff0000" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/butlogin" 
     android:text="username" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <EditText 
     android:id="@+id/username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/password" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="16dp" 
     android:ems="10" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/username" 
     android:layout_marginTop="16dp" 
     android:text="password" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <Button 
     android:id="@+id/butlogin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/password" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="61dp" 
     android:text="Login" /> 

    <EditText 
     android:id="@+id/password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@+id/textView3" 
     android:layout_marginRight="22dp" 
     android:layout_marginTop="21dp" 
     android:ems="10" 
     android:inputType="textPassword" > 

     <requestFocus /> 
    </EditText> 

</RelativeLayout> 

//登錄活動的Java代碼

package com.example.pizza; 
import com.example.pizza.R; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 


public class loginActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstancestate) 
    { 
     super.onCreate(savedInstancestate); 
     setContentView(R.layout.login); 
     addButtonOnClickEventListener(); 

    } 

    private void addButtonOnClickEventListener() { 
     // TODO Auto-generated method stub 




     Button button =(Button)findViewById(R.id.butlogin); 
     final Context context = loginActivity.this; 
     button.setOnClickListener(new OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       Intent intent = new Intent (context,WelActivity.class); 
       startActivity(intent); 
      } 

     }); 
     } 
    @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; 
    } 

} 

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

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

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.pizza.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="com.example.pizza.RegActivity"></activity> 
     <activity android:name="com.example.pizza.loginActivity"></activity> 
     <activity android:name="com.example.pizza.WelActivity"></activity> 
     <activity android:name="com.example.pizza.OrderActivity"></activity> 
     <activity android:name="com.example.pizza.MenuActivity"></activity> 
     <activity android:name="com.example.pizza.BillActivity"></activity> 
     <activity android:name="com.example.pizza.ThankuActivity"></activity> 




     </application> 


</manifest> 

logcat的錯誤:

02-13 15:24:05.520: D/dalvikvm(885): GC_FOR_ALLOC freed 44K, 4% free 3164K/3276K, paused 200ms, total 252ms 
02-13 15:24:06.370: D/gralloc_goldfish(885): Emulator without GPU emulation detected. 
02-13 15:24:22.080: I/Choreographer(885): Skipped 80 frames! The application may be doing too much work on its main thread. 
02-13 15:24:37.430: I/Choreographer(885): Skipped 30 frames! The application may be doing too much work on its main thread. 
02-13 15:24:43.170: I/Choreographer(885): Skipped 33 frames! The application may be doing too much work on its main thread. 
02-13 15:24:43.950: D/dalvikvm(885): GC_FOR_ALLOC freed 40K, 4% free 3369K/3476K, paused 80ms, total 84ms 
02-13 15:24:43.960: I/dalvikvm-heap(885): Grow heap (frag case) to 3.879MB for 552016-byte allocation 
02-13 15:24:44.070: D/dalvikvm(885): GC_FOR_ALLOC freed 1K, 3% free 3906K/4016K, paused 102ms, total 102ms 
02-13 15:24:44.450: I/Choreographer(885): Skipped 32 frames! The application may be doing too much work on its main thread. 
02-13 15:24:45.120: I/Choreographer(885): Skipped 92 frames! The application may be doing too much work on its main thread. 
02-12 23:22:47.564: D/dalvikvm(885): GC_FOR_ALLOC freed 28K, 3% free 3966K/4064K, paused 100ms, total 107ms 
02-12 23:22:47.594: I/dalvikvm-heap(885): Grow heap (frag case) to 4.675MB for 775012-byte allocation 
02-12 23:22:47.734: D/dalvikvm(885): GC_FOR_ALLOC freed 1K, 3% free 4721K/4824K, paused 138ms, total 139ms 
02-12 23:22:47.984: I/Choreographer(885): Skipped 693 frames! The application may be doing too much work on its main thread. 
02-12 23:22:49.394: W/ViewRootImpl(885): Dropping event due to no window focus: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x8, repeatCount=0, eventTime=217187, downTime=217187, deviceId=0, source=0x101 } 
02-12 23:22:49.554: I/Choreographer(885): Skipped 38 frames! The application may be doing too much work on its main thread. 
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885): KeyEvent: ACTION_UP but key was not down. 
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885): in com.android.internal.policy.impl.PhoneWindow$DecorView{b4e1e358 V.E..... R.....I. 0,0-480,800} 
02-12 23:22:51.104: D/InputEventConsistencyVerifier(885): 0: sent at 217477000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=158, metaState=0, flags=0x8, repeatCount=0, eventTime=217477, downTime=217187, deviceId=0, source=0x101 } 
02-12 23:22:55.714: I/Choreographer(885): Skipped 176 frames! The application may be doing too much work on its main thread. 
02-12 23:23:22.464: I/Choreographer(885): Skipped 187 frames! The application may be doing too much work on its main thread. 
+1

使用'startActivities',而不是'startActivitiy點擊監聽器'通過傳遞Intents數組來啓動任務 –

回答

1

您需要爲這兩個按鈕

Button button =(Button)findViewById(R.id.button1); 
    Button button1 =(Button)findViewById(R.id.button2); 
    final Context context = MainActivity.this; 
    button.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent (context,RegActivity.class); 
      startActivity(intent); 

     } 

    }); 
    button1.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      Intent intent1 = new Intent (context,loginActivity.class); 
      startActivity(intent1); 
     } 

    }); 
相關問題