Soundboard 应用程序强制关闭,我不明白为什么
所以我一直在为我的孩子们开发一款音板应用程序。这是我的第一个应用程序,所以你可以想象我几乎不知道我在做什么(菜鸟),所以我提前道歉:-)。我不确定我的问题出在哪里,但我的启动屏幕运行没有问题,但当它尝试加载下一个活动时,它会强制关闭。我将在清单中包含应该播放音频的 java 文件和可单击图像的按钮的布局。提前致谢!另外,我想将其设置为按钮可以播放与使用 soundpool 的图像相关的随机声音,但又是新手。我对这些错误并不熟悉,但我看到 java.land.classcastexception: android.widget.imageview 作为 mymenu 活动未启动的原因。希望有帮助。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pnl.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/sssicon"
android:label="@string/app_name" >
<activity android:label="@string/app_name" android:name=".myMain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name=".myMenu">
<intent-filter>
<action android:name="com.pnl.thebasics.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
package com.pnl.thebasics;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class myMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Hide the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Go full screen
final Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
//these are the buttons that play sounds
//button 1 (sponge bob)
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.sb1);
Button bSpongebob = (Button) findViewById(R.id.sbbutton);
bSpongebob.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
//button 2 (patrick)
final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.pat1);
Button bPatrick = (Button) findViewById(R.id.patbutton);
bPatrick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick2.start();
}
});
}
}
<?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:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/sbbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/sbbuttonimage" />
<ImageView
android:id="@+id/patbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/patbuttonimage" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/mrcrabsbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/mrcrabsbuttonimage" />
<ImageView
android:id="@+id/squidwardbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/squidwardbuttonimage" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/planktonbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/planktonbuttonimage" />
<ImageView
android:id="@+id/garybutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/garybuttonimage" />
</LinearLayout>
</LinearLayout>
So I've been working on a soundboard app for my kids. This is my first app so as you can imagine I very nearly have no idea what I'm doing (noob) so I'm apologizing in advance :-). I'm not sure where my problem is but my splash screen runs no problem but when it tries to load the next activity it force closes. i'm going to include my manifest the java file that is supposed to play the audio and the layout for the buttons which are clickable images. Thanks in advance! Also I would like to set it up where the buttons could play a random sound that relates to the image using soundpool but again with the noobness. I'm not really familiar at all with the errors but I'm seeing java.land.classcastexception: android.widget.imageview as the reason the mymenu activity isn't starting. Hope that helps.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pnl.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/sssicon"
android:label="@string/app_name" >
<activity android:label="@string/app_name" android:name=".myMain">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="@string/app_name" android:name=".myMenu">
<intent-filter>
<action android:name="com.pnl.thebasics.CLEARSCREEN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
package com.pnl.thebasics;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
public class myMenu extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Hide the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// Go full screen
final Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
//these are the buttons that play sounds
//button 1 (sponge bob)
final MediaPlayer mpButtonClick1 = MediaPlayer.create(this, R.raw.sb1);
Button bSpongebob = (Button) findViewById(R.id.sbbutton);
bSpongebob.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick1.start();
}
});
//button 2 (patrick)
final MediaPlayer mpButtonClick2 = MediaPlayer.create(this, R.raw.pat1);
Button bPatrick = (Button) findViewById(R.id.patbutton);
bPatrick.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
mpButtonClick2.start();
}
});
}
}
<?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:orientation="vertical" >
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/sbbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/sbbuttonimage" />
<ImageView
android:id="@+id/patbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/patbuttonimage" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout02"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/mrcrabsbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/mrcrabsbuttonimage" />
<ImageView
android:id="@+id/squidwardbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/squidwardbuttonimage" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout03"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/planktonbutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/planktonbuttonimage" />
<ImageView
android:id="@+id/garybutton"
android:layout_width="wrap_content"
android:layout_height="130dp"
android:layout_weight="50"
android:clickable="true"
android:src="@drawable/garybuttonimage" />
</LinearLayout>
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 Java 代码期望在布局中找到
Button
对象:但是您的布局声明该小部件是
ImageView
:ImageView
不是' t 一个Button
,当您的 Java 代码尝试强制它成为一个Button
时,您会得到java.lang.ClassCastException
。需要修复的两个选择:
1) 更改 Java 代码以使用
ImageView
。2) 更改布局以声明
Button
。两者都会接受您尝试设置的点击侦听器。不要忘记您需要对应用程序中的两个小部件进行此修复。
Your Java code is expecting to find a
Button
object in your layout:But your layout declares that widget to be an
ImageView
:An
ImageView
isn't aButton
, and when your Java code tries to force it to be aButton
you get thejava.lang.ClassCastException
.Two choices to fix:
1) Change your Java code to use
ImageView
.2) Change your layout to declare a
Button
.Either will accept the click listener you're trying to set. Don't forget you need to make this fix for both widgets in your app.