2011-10-19 99 views
0

首先,我必須寫出我剛剛開始使用android進行冒險,我正在構建簡單的菜單,並且據我介紹並退出按鈕工作:),我試着用start按鈕,但它不聽所有,如果您有任何建議,有什麼用它做我會更樂意:)Android上點擊監聽器的困難

主要活動:

public class MainActivity extends Activity implements OnClickListener 
{ 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
View startButton = findViewById(R.id.start_button); 
startButton.setOnClickListener(this); 
View whereButton = findViewById(R.id.where_button); 
whereButton.setOnClickListener(this); 
View aboutButton = findViewById(R.id.about_button); 
aboutButton.setOnClickListener(this); 
View exitButton = findViewById(R.id.exit_button); 
exitButton.setOnClickListener(this); 
} 

public void onClick(View v) { 

switch (v.getId()) { 

case R.id.about_button: 
Intent i = new Intent(this, About.class); 
startActivity(i); 
break; 
case R.id.exit_button: 
finish(); 
break; 
case R.id.start_button: 
i = new Intent(this, Start.class); 
startActivity(i); 
break; 
} 
} 

的strings.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<string name="app_name">City Inspector</string> 
<string name="main_title">Ldz Inspector</string> 
<string name="start_title">Profile</string> 
<string name="start_label">START</string> 
<string name="where_label">WHERE AM I?</string> 
<string name="option_label">OPTIONS</string> 
<string name="about_label">ABOUT</string> 
<string name="create_label">CREATE PROFILE</string> 
<string name="browse_label">BROWSE MAP</string> 
<string name="exit_label">EXIT</string> 
<string name="about_text">\ 
about program things 

</string> 
</resources> 

start.java

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.start); 
} 
} 

start.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/background" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:padding="20dip" 
android:orientation="horizontal" > 
<LinearLayout 
android:orientation="vertical" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_gravity="center" > 
<TextView 
android:text="@string/start_title" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_gravity="center" 
android:layout_marginBottom="25dip" 
android:textSize="24.5sp" /> 
<Button 
android:id="@+id/Create_Profile" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/create_label" /> 
<Button 
android:id="@+id/Browse_Map" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/browse_label" /> 
</LinearLayout> 
</LinearLayout> 

!編輯:

對不起,我知道我忘了張貼的東西:),這裏是AndroidManifest :)

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="menu.dot" 
android:versionCode="1" 
android:versionName="1.0"> 
<application android:label="@string/app_name" android:icon="@drawable/icon"> 
<activity android:name="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=".About"> 
android:label="@string/about_title" 
android:theme="@android:style/Theme.Dialog" > 
</activity> 
<activity android:name=".Exit"> 
andorid:label="@string/exit_title">  
</activity> 
<activity android:name=".Start"> 
andorid:label="@string/start_title"> 

</activity> 
/application> 
</manifest> 

請檢查start.xml版

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:background="@color/background" 
android:layout_height="fill_parent" 
android:layout_width="fill_parent" 
android:padding="20dip" 
android:orientation="horizontal" > 
<LinearLayout 
android:orientation="vertical" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:layout_gravity="center" > 
<TextView 
android:text="@string/main_title" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:layout_gravity="center" 
android:layout_marginBottom="25dip" 
android:textSize="24.5sp" /> 
<Button 
android:id="@+id/start_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/start_label" /> 
<Button 
android:id="@+id/where_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/where_label" /> 
<Button 
android:id="@+id/option_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/option_label" /> 
<Button 
android:id="@+id/about_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/about_label" /> 
<Button 
android:id="@+id/exit_button" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/exit_label" /> 
</LinearLayout> 
</LinearLayout> 
+0

是否DDMS告訴你什麼?給你關於爲什麼它不開始你的活動的任何信息? – JQCorreia

+0

據我所見,一切看起來都很好。我看不到:start.class是從Activity派生出來的嗎? start.class是否在清單中聲明?同時在Log.d(「mainActivity」,「onClick triggered」)中放入一個日誌在onClick()方法內部,並在每個case語句內部查看它是否被觸發 –

+0

thx rafael T,我會在一秒鐘內檢查它 – iie

回答

0

在Android中,你並不需要實現onClickListener和檢查哪個按鈕被點擊。 Android爲您提供了一種簡潔的方式來指定用於處理onClick事件的方法,方法是將onClick-attribute用於XML-Layout文件中的按鈕。

考慮以下佈局:

<Button android:id="@+id/some_button" 
    android:onClick="someButtonsAction" 
    android:layout_width="fill_parent" 
    android:layout_height="match_content" 
/> 

的活動呈現該按鈕現在需要有這樣的方法:

public void someButtonsAction(View v){ 
    // Do your onClick stuff here 
} 

供給的View v -argument是點擊視圖本身。整個事情也說明in the docs如果頁面頂部。

這使您有機會使代碼更具可讀性,並且不需要從佈局中獲取所有按鈕,將它們添加到onClickListener中並確定哪個按鈕被單擊(在偵聽器中)。

+0

謝謝大家的耐心,你今天幫了我很多:) – iie

+0

@ user552629所以,你應該投票選好答案並接受最好的答案。 –

+0

我不能投票。聲望是低 – iie

0

您是否在AndroidManifest.xml中註冊了'開始'活動?
如果您沒有在AndroidManifest.xml中註冊活動,則無法運行活動。

+0

請檢查第一篇文章的最新版本。 – iie

0

你可以發佈你的main.xml嗎?我嘗試了你的代碼,假設你的main.xml看起來像什麼,並開始活動工作得很好(即,開始佈局顯示)。

+0

我已經把它放在第二個以前的主帖 – iie

+0

你的代碼適合我。我不得不首先清理字符串資源xml的底部,因爲你有2個exit_label字符串,而你的「about_text」字符串給了我空指針異常: EXIT \ 關於程序事物 EXIT

+0

嗯,這太奇怪了。我不知道你是否檢查過更新後的版本[做了一個小時或更久以前],但現在問題看起來像這樣:當我按開始時,它會帶我到關於頁面,當我點擊它時帶我到頁。 – iie

0

mainActivity.java

public class MainActivity extends Activity implements OnClickListener 
{ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
View startButton = findViewById(R.id.start_button); 
startButton.setOnClickListener(this); 
View whereButton = findViewById(R.id.where_button); 
whereButton.setOnClickListener(this); 
View aboutButton = findViewById(R.id.about_button); 
aboutButton.setOnClickListener(this); 
View exitButton = findViewById(R.id.exit_button); 
exitButton.setOnClickListener(this); 
} 

public void onClick(View v) { 


switch (v.getId()) { 

case R.id.about_button: 
Intent i = new Intent(this, About.class); 
startActivity(i); 
break; 
case R.id.exit_button: 
finish(); 
break; 

} 
} 
public void StartCl(View v){ 
Intent idd = new Intent(this,Start.class); 
startActivity(idd); 
} 
} 

main.xml中

<Button 
android:id="@+id/start_button" 
android:onClick="StartCl" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/start_label" /> 
+0

您正在佈局和代碼中分配'onClick'-Listener。刪除代碼內分配,它應該工作。此外,您應該爲您的問題添加詳細信息,而不是創建新的答案,除非您想回​​答自己的帖子。 –

+0

嗯你的意思是踢出這部分公共無效onClick(查看v){}?但是當我刪除它時,我用startButton.setOnClickListener(this)得到了一些錯誤; – iie

+0

確定它現在工作不知何故,將看到多久:) – iie