2011-11-15 232 views
0

塊引用新意圖按鈕不打開點擊

我嘗試通過簡單地點擊按鈕來改變顯示。我想我應該使用意圖這樣做,但沒有任何效果,請幫我:( 這裏是我的代碼:

FYP.JAVA

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    final Button ProfileTemplate = (Button)findViewById(R.id.ProfileTemplate); 
    ProfileTemplate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Intent mIntent = new Intent(FYP.this, ProfileTemplate.class); 
      startActivity(mIntent); 
      finish(); 
     } 
    }); 



public class ProfileTemplate extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.profiletemplate); 
    } 
} 
+2

你GETT有任何錯誤? –

+0

你的課ProfileTemplate在一個單獨的文件?也請張貼adb的輸出。 – Kerry

+2

有什麼錯誤?你的Manifest文件中有兩項活動嗎? – hovanessyan

回答

0

下面是2個活動的完整的項目有一個按鈕。 ,當你點擊它,它會啓動其他活動。

更多關於Buttons and Clicking

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="Hello, sup son!" /> 

    <Button android:id="@+id/btnButton" android:layout_height="wrap_content" 
     android:layout_width="fill_parent" android:layout_gravity="left" 
     android:onClick="changeActivity" android:text="changeActivity" /> 
</LinearLayout> 

main2.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:text="New Activity Started" /> 

</LinearLayout> 

類Hello.java

public class Hello extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
} 

    //default button handler 
public void changeActivity(View view) { 
    startActivity(new Intent(this, NewActivity.class)); 
} 
} 

類NewActivity.java

public class NewActivity extends Activity { 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 

    } 

} 

的Manifest.xml

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

    <application android:icon="@drawable/icon" android:label="New App Name"> 
     <activity android:name=".Hello" 
        android:label="New App Name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity android:name=".NewActivity" android:label="New App Name"></activity> 

    </application> 
</manifest> 
+0

非常感謝,這真的很有幫助;) – ChristopherF