[英]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.