This question already has answers here:
NullPointerException accessing views in onCreate()

(13个回答)



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

(12个答案)


6年前关闭。




所以基本上,我想要一个按钮,当单击该按钮时会播放声音。
这是代码。
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/

10-12 06:12