我对此仍然很陌生,因此解决方案可能很明显。无论如何,我试图让这个应用程序中的按钮打开新的活动,但是当我运行它时,强制关闭。有谁知道出了什么问题?我打算使用一种叫做列表视图的东西,但它没有使用我为类制作的 XML。
package com.example.musicbynumbers;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
public class MainMenu extends Activity implements View.OnClickListener {
Button majScales, minHarm, minMel;
ImageButton mainMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
mainMenu = (ImageButton) findViewById(R.id.imagelogo);
majScales = (Button) findViewById(R.id.majorscalesb);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.imagelogo:
Intent i = new Intent(MainMenu.this, MainMenu.class);
startActivity(i);
break;
case R.id.majorscalesb:
Intent j = new Intent(MainMenu.this, majorScales.class);
startActivity(j);
break;}
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="80"
tools:context=".MainMenu"
>
<ImageButton
android:id="@+id/imagelogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_weight="10"
android:gravity="center" />
<Button
android:id="@+id/majorscalesb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Major Scales"
android:gravity="center" />
<Button
android:id="@+id/minormelodicb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Minor Melodic Scales"
android:gravity="center"/>
<Button
android:id="@+id/minorharmonicb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Minor Harmonic Scales"
android:gravity="center"/>
<Button
android:id="@+id/arpeggiosb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Arpeggios"
android:gravity="center" />
<Button
android:id="@+id/chromaticscalesb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Chromatic Scales"
android:gravity="center" />
<Button
android:id="@+id/contraryb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="Contrary Motion"
android:gravity="center" />
<Button
android:id="@+id/aboutb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="10"
android:text="About"
android:gravity="center"
>
</Button>
</LinearLayout>
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.musicbynumbers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.musicbynumbers.MainMenu"
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="com.example.musicbynumbers.majorScales"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.musicbynumbers.majorScales" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>