0

我已经有这个简单的程序大约两个月了,前几天晚上,一个错误出现在用户面前,我不明白为什么。

public class mainmenu extends Activity implements OnPreparedListener {
    private PopupWindow pw;
    public static int RandomNum;
    public static String quickStart;
    private dbadapter mydbhelper;
    MediaPlayer mpButtonClick;
    Intent intent;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);




    Button bsound = (Button) findViewById(R.id.bCategories);
    bsound.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            quickStart = "Cate";
            onPrepared(mpButtonClick);
            intent = new Intent ("wanted.pro.madlibslite.CATEGORIES");
            intent.putExtra("quickStart", quickStart);
            startActivity(intent);
        }
    });



    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    //Menu Items
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.mainmenu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
             case R.id.help:
               showHelp();
                return true;
           }
        return false;
    }

        private void showHelp() {
            LayoutInflater inflater = (LayoutInflater)
                       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     pw = new PopupWindow(
                       inflater.inflate(R.layout.help, null, false), 
                       LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 
                       true);
                     pw.showAtLocation(this.findViewById(R.id.bCategories), Gravity.CENTER, 0, 0); 
        }
        public void onClickHelp(View helper){pw.dismiss();

        }   
        //QuickStart
        public void onClickQuick(View main) {
            Toast.makeText(this, "Quick Start only available in full version.", 1000).show();
            //RandomNum = (int) (Math.random()*101);
            //quickStart = "Quick";
            //mpButtonClick.start();
            //startActivity(new Intent("wanted.pro.madlibslite.INPUTPAGE"));


        }

        public void onPrepared(MediaPlayer player) {
            mpButtonClick.setOnPreparedListener(this);
            try {
                mpButtonClick.prepare();

            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mpButtonClick.start();
        }

        @Override
        protected void onResume() {
            mydbhelper = dbadapter.getInstance(mainmenu.this);
            mydbhelper.open();
            mpButtonClick = MediaPlayer.create(this, R.raw.button50);
            super.onResume();
        }

        @Override
        protected void onPause() {
            mpButtonClick.release();
            super.onPause();
        }

} 

有问题的错误...

java.lang.NullPointerException
at wanted.pro.madlibslite.mainmenu.onPrepared(mainmenu.java:107)
at wanted.pro.madlibslite.mainmenu$1.onClick(mainmenu.java:45)
at android.view.View.performClick(View.java:2485)
at android.view.View$PerformClick.run(View.java:9089)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3806)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)

可悲的是,我没有他们使用的设备。但是希望这里的某人可能会看到我做错的事情,这会导致 onprepared 出错。

4

1 回答 1

0

在 onPrepared 方法中唯一可能导致 NullPointerException 的变量是:mpButtonClick。所以让我们试着弄清楚怎么mpButtonClick可能是空的......

在 onResume 方法中,您将创建一个新的 MediaPlayer,并将其分配给变量 mpButtonClick。但是,您可能错过的是有时可能无法创建 MediaPlayers。正如Android文档所说的静态方法MediaPlayer.create(Context context, int resid)

退货

一个 MediaPlayer 对象,如果创建失败,则返回 null

没错——该方法不一定总是返回有效的 MediaPlayer。每当无法创建 MediaPlayer 时,它将返回 null。然后导致您的 onPrepared 方法访问一个空变量 - 因此是 NullPointerException。

于 2012-04-20T01:15:38.417 回答