Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为BroadcastReceiver调用onReceive时的NullPointerException_Java_Android_Mobile_Nullpointerexception_Broadcastreceiver - Fatal编程技术网

Java 为BroadcastReceiver调用onReceive时的NullPointerException

Java 为BroadcastReceiver调用onReceive时的NullPointerException,java,android,mobile,nullpointerexception,broadcastreceiver,Java,Android,Mobile,Nullpointerexception,Broadcastreceiver,当我使用我的BroadcastReceiver获取一个布尔值以确定是否使用ImageButton时,我接收到一个NullPointerException。我不知道为什么,我看过的所有教程都没有帮助 这是我调用服务和调用onReceive的代码 //start full day timer service for natural disasters startService(new Intent(runGraphics.this, NaturalDisaster.class));

当我使用我的
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()中注销了receiver。任何帮助都将不胜感激。提前感谢。

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

试试这个
i.putbooleaneextra(“meteostat”,true)@SimplePlan-putBooleanExtra方法不可用。我只是得到了一个错误。@shayanpourvatan-第113行是I.putExtra(“流星统计”,false);在我的代码中发送广播。代码接收中的第133行只是我将imagebutton设置为不可见。