首先,我必須寫出我剛剛開始使用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>
是否DDMS告訴你什麼?給你關於爲什麼它不開始你的活動的任何信息? – JQCorreia
據我所見,一切看起來都很好。我看不到:start.class是從Activity派生出來的嗎? start.class是否在清單中聲明?同時在Log.d(「mainActivity」,「onClick triggered」)中放入一個日誌在onClick()方法內部,並在每個case語句內部查看它是否被觸發 –
thx rafael T,我會在一秒鐘內檢查它 – iie