2011-12-26 61 views
1

這似乎是一個簡單的功能,但我已經閱讀和審查了無數的教程,並嘗試多次,以使其發揮作用,我仍然沒有成功。如何在點擊後激活一個按鈕切換到另一個佈局?

我有標題爲「主」開幕XML頁面 我有第二個XML頁面標題爲「菜單」 我有標題爲「ButtontestingActivity.java」 一號java的活動我都第二個java活動名爲「菜單」

在ButtontestingActivity.java我有...

package com.package2.Buttontesting; 

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

public class ButtontestingActivity extends Activity { 


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

    Button Button1 = (Button) findViewById(R.id.sweetness); 
    Button1.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      startActivity(new Intent("com.package2.Buttontesting.MENU")); 
     } 
    }); 

} 
@Override 
protected void onPause() { 
    super.onPause(); 
} 

} 

main.xml中我有...

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

<Button 
    android:id="@+id/sweetness" 
    android:layout_width="40dp" 
    android:layout_height="78dp" 
    android:layout_marginLeft="52dp" 
    android:layout_marginTop="40dp" 
    android:visibility="visible" 
    /> 

</LinearLayout> 

在清單我...

<application 
    android:icon="@drawable/face" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".ButtontestingActivity" > 
     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity 
     android:label="@string/app_name" 
     android:name=".Menu" > 
     <intent-filter > 
      <action android:name="com.package2.Buttontesting.MENU" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

,我有機器人:背景=「@繪製/菜單」以我menu.xml文件

當我安裝的應用程序在我的AVD第一佈局顯示但是當我按下按鈕打開第二個佈局時,AVD發送消息

「應用程序Buttontesting(process com.package2.Buttontesting)已意外停止。請重試」

,然後我返回到應用程序菜單。

+1

請添加您在logcat中越來越錯誤信息,它可以幫助在得到準確的答案。 – kosa 2011-12-26 17:24:44

回答

0

使用

startActivity(new Intent(ButtontestingActivity.this, Menu.class)); 

startActivity(new Intent(ButtontestingActivity.this, com.package2.Buttontesting.Menu.class)); 

而且還從你的菜單中刪除的活動意圖濾波器塊,因爲應用只能有一個發射器活動。

+1

還要從xml文件中刪除第二個意圖過濾器。 – Yury 2011-12-26 17:26:43

+0

謝謝,我錯過了 – 2011-12-26 17:29:42

2

試試這個:

Intent intent = new Intent(ButtontestingActivity.this,Menu.class); 
startActivity(intent); 

也從清單文件中刪除此:

<intent-filter > 
     <action android:name="com.package2.Buttontesting.MENU" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 

<activity 
    android:label="@string/app_name" 
    android:name=".Menu" > 
+0

美麗!它工作感謝 – user1091368 2011-12-26 18:48:48

相關問題