为BroadcastReceiver调用onReceive时出现NullPointerException

时间:2014-06-07 04:42:38

标签: java android mobile nullpointerexception broadcastreceiver

当我使用NullPointerException获取布尔值以确定是否将使用BroadcastReceiver时,我收到ImageButton。我不知道为什么以及我所看过的所有教程都没有帮助。

这是我的代码调用服务并调用onReceive

    //start full day timer service for natural disasters
    startService(new Intent(runGraphics.this, NaturalDisaster.class));

    //stop full day timer service for natural disasters
    stopService(new Intent(runGraphics.this, NaturalDisaster.class));

    meteorAndStormStatus = new BroadcastReceiver()
    {
        @Override
        public void onReceive(Context context, Intent intent) 
        {
            meteorExists = intent.getBooleanExtra("meteorStat", false);
            Toast.makeText(getApplicationContext(), "Here we are: " + meteorExists, Toast.LENGTH_SHORT).show();
            if (meteorExists == true)
            {
                //on click listener for meteor
                meteor = (ImageButton) findViewById(R.id.meteor);
                meteor.setVisibility(View.VISIBLE);
                meteor.setOnClickListener(new OnClickListener()
                {

                    @Override
                    public void onClick(View v) 
                    {
                        if (wasMined == false)
                        {
                            meteorRockAmount += 50;
                            wasMined = true;
                        }//end if
                    }//end onClick function

                });//end setOnClickListener
            }//end if
        }//end onReceive function
    };//end meteorAndStormStatus BroadcastReceiver
    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(meteorAndStormStatus, new IntentFilter("meteorStat"));

这是发送广播的代码

        //send broadcast back that a meteor exists
        Intent i = new Intent();
        i.putExtra("meteorStat", true);
        LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);

这是我的logcat:

06-06 23:30:22.318: E/AndroidRuntime(21714): FATAL EXCEPTION: main
06-06 23:30:22.318: E/AndroidRuntime(21714): java.lang.NullPointerException
06-06 23:30:22.318: E/AndroidRuntime(21714):    at com.project.llb.runGraphics$1.onReceive(runGraphics.java:113)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.os.Looper.loop(Looper.java:130)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at android.app.ActivityThread.main(ActivityThread.java:3806)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at java.lang.reflect.Method.invoke(Method.java:507)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-06 23:30:22.318: E/AndroidRuntime(21714):    at dalvik.system.NativeStart.main(Native Method)

另外,我使用onReceive在类的onPause()中取消注册接收器。任何帮助,将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

问题是我有一个if语句if(meteorExists == false)并且在这个语句中我有meteor.setVisibility(View.INVISIBLE)。问题是,如果不首先访问原始if,则会产生NullPointerException。所以为了解决这个问题,我不得不包括一个meteor =(ImageButton)findViewById(R.id.meteor);在我试图将流星设置为隐形之前。谢谢你的帮助。你指出了我正确的方向。