繁体   English   中英

当为BroadcastReceiver调用onReceive时发生NullPointerException

[英]NullPointerException when onReceive is callled for BroadcastReceiver

使用我的BroadcastReceiver取回布尔值以确定是否将使用ImageButton时,我收到NullPointerException 我不知道为什么,我看过的所有教程都无济于事。

这是我的代码调用服务并调用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);

这是我的日志:

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()中注销了接收者。 任何帮助,将不胜感激。 提前致谢。

问题是我有一个if语句if(meteorExists == false),在此语句中我有meteor.setVisibility(View.INVISIBLE)。 问题是,如果不首先访问原始的if,则会给出NullPointerException。 因此,要解决此问题,我必须添加一个流星=(ImageButton)findViewById(R.id.meteor); 在我尝试将流星设置为不可见之前。 感谢您的帮助。 你为我指明了正确的方向。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM