2013-04-12 47 views
3

當我點擊按鈕時,我的意圖去到另一個Android頁面沒有激活。Android意圖不激活

我的Java代碼

public class CharSheets extends Activity implements OnClickListener{ 

    Button Descrip, Atributes, Weapons, Skills, ACItems, Gear, 
     Feats, SpecialAbilities, Spells; 

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

    private void setids() { 
     // TODO Auto-generated method stub 
     Descrip = (Button)findViewById(R.id.bCharDescrip); 
    } 

    @Override 
    public void onClick(View arg0) { 
     // TODO Auto-generated method stub 
     switch(arg0.getId()) { 
     case R.id.bCharDescrip: 
      Intent i = new Intent(CharSheets.this, CharDescrip.class); 
      startActivity(i); 
      break; 
     case R.id.bCharAtributes: 
      Intent i1 = new Intent(CharSheets.this, CharAtributes.class); 
      startActivity(i1); 
      break; 
     } 
    } 
} 

的XML mainfest

<activity 
    android:name=".CharAtributes" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.CHARSATRIBUTES" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
</activity> 
<activity 
    android:name=".CharDescrip" 
    android:label="@string/app_name" > 
    <intent-filter> 
     <action android:name="android.intent.action.CHARDESCRIP" /> 

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

我不知道是什麼原因造成的問題,但我已經嘗試了其他的解決辦法他們都沒有工作。我現在做的方式我以前用過,它工作得很好。

回答

3

你還沒有爲你Button

private void setids() { 
    // TODO Auto-generated method stub 
    Descrip = (Button)findViewById(R.id.bCharDescrip); 
    Descrip.setOnClickListener(this); // add this line 
} 

現在添加其他Buttons這裏使用setOnClickListener()每個以同樣的方式設置onClickListener()

3

您尚未將OnClickListener應用於您的按鈕。

private void setids() { 
    // TODO Auto-generated method stub 
    Descrip = (Button)findViewById(R.id.bCharDescrip); 
    Descrip.setOnClickListener(this); // <--- This line is required. 
} 

作爲一個方面說明,通常的做法是命名以小寫字母開頭的變量,例如,記述

2

您應該在Button上設置OnClickListener。 。 。

private void setids() { 
     // TODO Auto-generated method stub 
     Descrip = (Button)findViewById(R.id.bCharDescrip); 
     Descrip.setOnClickListener(this); 
    }