单击按钮时应用程序力关闭

时间:2011-04-02 21:17:26

标签: android nullpointerexception

我的应用程序在市场上给了我5个不同的错误,我无法弄明白。它强制关闭按钮点击。

这是我遇到的第一个错误。

java.lang.NullPointerException
at com.yes.hotrod.main$1.onClick(main.java:25)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8818)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
at dalvik.system.NativeStart.main(Native Method)

这是我的main.java

package com.yes.hotrod;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class main extends Activity {MediaPlayer FilmSound;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button1 = (Button)findViewById(R.id.button1);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.animalkingdom);
    button1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.animalkingdom);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.animalkingdom); 
    FilmSound.start(); }    
    }});


    Button button2 = (Button)findViewById(R.id.button2);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.bigassstunt);
    button2.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.bigassstunt);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.bigassstunt); 
    FilmSound.start(); }    
    }
    });
    Button button3 = (Button)findViewById(R.id.button3);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.godsofwar);
    button3.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.godsofwar);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.godsofwar); 
    FilmSound.start(); }    
    }
    });
    Button button4 = (Button)findViewById(R.id.button4);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.greentea);
    button4.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.greentea);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.greentea); 
    FilmSound.start(); }    
    }
    });
    Button button11 = (Button)findViewById(R.id.button11);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.havefun);
    button11.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.havefun);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.havefun); 
    FilmSound.start(); }    
    }
    });
    Button button6 = (Button)findViewById(R.id.button6);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.kisswashot);
    button6.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.kisswashot);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.kisswashot); 
    FilmSound.start(); }    
    }
    });
    Button button7 = (Button)findViewById(R.id.button7);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.ohshit);
    button7.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.ohshit);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.ohshit); 
    FilmSound.start(); }    
    }
    });
    Button button8 = (Button)findViewById(R.id.button8);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.throwingstar);
    button8.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.throwingstar);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.throwingstar); 
    FilmSound.start(); }    
    }
    });
    Button button9 = (Button)findViewById(R.id.button9);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.twolegittwoquit);
    button9.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.twolegittwoquit);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.twolegittwoquit); 
    FilmSound.start(); }    
    }
    });
    Button button10 = (Button)findViewById(R.id.button10);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.whiskey);
    button10.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.whiskey);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.whiskey); 
    FilmSound.start(); }    
    }
    });
    Button button5 = (Button)findViewById(R.id.button5);
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.demons);
    button5.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    if (FilmSound.isPlaying()) 
    {
    FilmSound.stop();
    FilmSound.reset();
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.demons);
    FilmSound.start();
    }
    else {
    FilmSound = MediaPlayer.create(getApplicationContext(), R.raw.demons); 
    FilmSound.start(); }    
    }
    });
}
}

1 个答案:

答案 0 :(得分:0)

如果查看堆栈跟踪,您将看到错误发生在main.java的第25行,在onClick方法中为onClickListener for button1。

当你试图实例化FilmSound对象并且在该方法中它是null时,很可能出现了问题。