所以基本上,我想要一个按钮,当点击时播放声音。 这是代码。
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)this.findViewById(id.button1);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.lifesucks);
//mp.start();
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
如果onclicklistener部分被注释掉而mp.start()不是,它确实可以正常播放。当onclicklistener处于活动状态时,应用程序会在加载时崩溃。当我将mp.start()置于onClick(它应该在逻辑上)时,也会发生同样的事情
编辑:对不起,我是新手,这是LogKat:
08-25 16:58:56.520: D/AndroidRuntime(5303): Shutting down VM
08-25 16:58:56.520: W/dalvikvm(5303): threadid=1: thread exiting with uncaught exception (group=0x41a86ba8)
08-25 16:58:56.530: E/AndroidRuntime(5303): FATAL EXCEPTION: main
08-25 16:58:56.530: E/AndroidRuntime(5303): Process: com.example.nohomo, PID: 5303
08-25 16:58:56.530: E/AndroidRuntime(5303): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nohomo/com.example.nohomo.MainActivity}: java.lang.NullPointerException
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.os.Handler.dispatchMessage(Handler.java:102)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.os.Looper.loop(Looper.java:136)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-25 16:58:56.530: E/AndroidRuntime(5303): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 16:58:56.530: E/AndroidRuntime(5303): at java.lang.reflect.Method.invoke(Method.java:515)
08-25 16:58:56.530: E/AndroidRuntime(5303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-25 16:58:56.530: E/AndroidRuntime(5303): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-25 16:58:56.530: E/AndroidRuntime(5303): at dalvik.system.NativeStart.main(Native Method)
08-25 16:58:56.530: E/AndroidRuntime(5303): Caused by: java.lang.NullPointerException
08-25 16:58:56.530: E/AndroidRuntime(5303): at com.example.nohomo.MainActivity.onCreate(MainActivity.java:30)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.Activity.performCreate(Activity.java:5231)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-25 16:58:56.530: E/AndroidRuntime(5303): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-25 16:58:56.530: E/AndroidRuntime(5303): ... 11 more
布局:
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.nohomo.MainActivity$PlaceholderFragment" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:text="Az élet szar!" />
</RelativeLayout>
答案 0 :(得分:1)
不确定这是如何编译的。这条线是错字吗?
Button button=(Button)this.findViewById(id.button1);
应该是
Button button=(Button)this.findViewById(R.id.button1);
如果这不是问题,请发布您的布局文件。