我想做一个简单的钢琴应用程序。我找到了这个教程;http://www.martinhoeller.net/2012/01/13/developing-a-musical-instrument-app-for-android/.
我在这个链接上下载了文件并导入了它。我运行这个应用程序,它上面有4个按钮,播放4个不同的音符,这些音符加载在资产文件中。
然后我尝试修改这个项目来制作一个钢琴应用程序。
起初我从xml中删除了这些红色按钮。
我添加了一个钢琴图像作为背景。
然后我在每个键上创建按钮。
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:layout_gravity="fill_vertical"
android:background="@drawable/images"
android:orientation="vertical" >
<Button
android:id="@+id/buttonC4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonD4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonE4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonF4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonG4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonA4"
android:layout_width="294dp"
android:layout_height="60dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonB4"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonC5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonD5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
<Button
android:id="@+id/buttonE5"
android:layout_width="294dp"
android:layout_height="58dp"
android:background="@android:color/transparent"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
然后我在网上找到笔记并下载它们。
我把这些笔记文件上传到assets文件夹。
每一个都是wav格式。
然后我将这些代码添加到PianoTest Activity;
buttons = new ArrayList<View>();
buttons.add(findViewById(R.id.buttonC4));
buttons.add(findViewById(R.id.buttonD4));
buttons.add(findViewById(R.id.buttonF4));
buttons.add(findViewById(R.id.buttonG4));
buttons.add(findViewById(R.id.buttonA4));
buttons.add(findViewById(R.id.buttonB4));
buttons.add(findViewById(R.id.buttonC5));
buttons.add(findViewById(R.id.buttonD5));
buttons.add(findViewById(R.id.buttonE5));
然后我修改这个函数;
private void toggleButtonSound(View button, boolean down){String note = null;
switch (button.getId())
{
case R.id.buttonC4:
note = "C4";
break;
case R.id.buttonD4:
note = "D4";
break;
case R.id.buttonE4:
note = "E4";
break;
case R.id.buttonF4:
note = "F4";
break;
case R.id.buttonG4:
note = "G4";
break;
case R.id.buttonA4:
note = "A4";
break;
case R.id.buttonB4:
note = "B4";
break;
case R.id.buttonC5:
note = "C5";
break;
case R.id.buttonD5:
note = "D5";
break;
case R.id.buttonE5:
note = "E5";
break;
}
然后我改变AudioTrack类。我将文件格式改为wav文件;
public void run()
{
try
{
String path = note + ".wav";
其他内容与原项目相同。
但是应用程序不能正常工作。
当我运行应用程序,点击一个键上的透明按钮,它播放的声音,但它是不一样的声音,因为它是。
听起来有些不同。我可以说这是一个电子版本的笔记。
当我点击其他键时,应用程序崩溃了。提示应用程序意外停止
怎么了?我该怎么做才能成功呢?原因可能是文件被下载了。我需要找什么样的笔记?在这个项目中,他们的位或时间持续时间有什么特殊的格式吗?
这对我来说非常重要,如果你可以从链接下载文件并运行,你可以看到它是如何工作的。你可以建议我修改的方法。我希望你能帮助我。这是logcat:
05-13 19:49:55.580: D/AndroidRuntime(485): Shutting down VM
05-13 19:49:55.580: W/dalvikvm(485): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 19:49:55.600: E/AndroidRuntime(485): FATAL EXCEPTION: main
05-13 19:49:55.600: E/AndroidRuntime(485): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.martinhoeller/net.martinhoeller.PianoTestActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class ButtonView
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.os.Looper.loop(Looper.java:123)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 19:49:55.600: E/AndroidRuntime(485): at dalvik.system.NativeStart.main(Native Method)
05-13 19:49:55.600: E/AndroidRuntime(485): Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class ButtonView
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.Activity.setContentView(Activity.java:1647)
05-13 19:49:55.600: E/AndroidRuntime(485): at net.martinhoeller.PianoTestActivity.onCreate(PianoTestActivity.java:23)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-13 19:49:55.600: E/AndroidRuntime(485): ... 11 more
05-13 19:49:55.600: E/AndroidRuntime(485): Caused by: java.lang.ClassNotFoundException: android.view.ButtonView in loader dalvik.system.PathClassLoader[/data/app/net.martinhoeller-1.apk]
05-13 19:49:55.600: E/AndroidRuntime(485): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
05-13 19:49:55.600: E/AndroidRuntime(485): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createView(LayoutInflater.java:466)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:544)
05-13 19:49:55.600: E/AndroidRuntime(485): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
05-13 19:49:55.600: E/AndroidRuntime(485): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
05-13 19:49:55.600: E/AndroidRuntime(485): ... 20 more
05-13 19:52:22.293: D/AndroidRuntime(513): Shutting down VM
05-13 19:52:22.293: W/dalvikvm(513): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 19:52:22.309: E/AndroidRuntime(513): FATAL EXCEPTION: main
05-13 19:52:22.309: E/AndroidRuntime(513): java.lang.ArrayIndexOutOfBoundsException
05-13 19:52:22.309: E/AndroidRuntime(513): at net.martinhoeller.PianoTestActivity.onTouchEvent(PianoTestActivity.java:66)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.app.Activity.dispatchTouchEvent(Activity.java:2089)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.os.Looper.loop(Looper.java:123)
05-13 19:52:22.309: E/AndroidRuntime(513): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 19:52:22.309: E/AndroidRuntime(513): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 19:52:22.309: E/AndroidRuntime(513): at java.lang.reflect.Method.invoke(Method.java:521)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 19:52:22.309: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 19:52:22.309: E/AndroidRuntime(513): at dalvik.system.NativeStart.main(Native Method)
你似乎有很多问题。让我们先试着修复布局膨胀崩溃。
List<Button> buttons;
...
buttons = new ArrayList<Button>();
buttons.add((Button)findViewById(R.id.buttonC4));
buttons.add((Button)findViewById(R.id.buttonD4));
...
for (Button button : buttons) {
button.setOnClickListener(this);
}
这将正确地转换布局的按钮视图,并为每个设置一个OnClickListener。
您的活动需要implement View.OnClickListener
才能使用setOnClickListener(this)
。替换为适当的类,如果你有你的点击处理分离,该类将implement OnClickListener
而不是你的活动在这种情况下。