Reputation: 287
I am receiving a NullPointerException
when I use my BroadcastReceiver
to get a Boolean value back to determine whether or not an ImageButton
will be used. I have no idea why and all of the tutorials I have looked at are not helping.
here is my code calling the service and calling 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"));
here is the code sending the broadcast
//send broadcast back that a meteor exists
Intent i = new Intent();
i.putExtra("meteorStat", true);
LocalBroadcastManager.getInstance(getApplicationContext()).sendBroadcast(i);
Here is my 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)
Also, I do unregister the receiver in my onPause() of the class with the onReceive. Any help would be appreciated. Thanks in advance.
Upvotes: 1
Views: 1282
Reputation: 287
The problem was that I had an if statement if(meteorExists == false) and inside this statement I had meteor.setVisibility(View.INVISIBLE). The problem is that this will give a NullPointerException if the original if is not visited first. So to fix the issue, I had to include a meteor = (ImageButton) findViewById(R.id.meteor); before I tried to set the meteor to invisible. Thanks for the help guys. You pointed me in the right direction.
Upvotes: 1