gpt4 book ai didi

android - OnClickListener 导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-03 17:48:52 31 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





NullPointerException accessing views in onCreate()

(13 个回答)



What is a NullPointerException, and how do I fix it?

(12 个回答)


7年前关闭。




所以基本上,我想要一个按钮,当点击时播放声音。
这是代码。

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 处于 Activity 状态时,应用程序在加载时崩溃。当我将 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>

最佳答案

不知道这是如何编译的。这条线是错字吗?

Button button=(Button)this.findViewById(id.button1);

应该
Button button=(Button)this.findViewById(R.id.button1);

如果这不是问题,请发布您的布局文件。

关于android - OnClickListener 导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25488554/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com