我粘貼一些部分我的代碼在這裏:關於Android的背景下
public void onCreate(Bundle savedInstanceState) {
Log.e("Main Activity", "OnCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.main_frame);
//GlowEffect is the custom class's constructor:GlowEffect(context);
new GlowEffect2(getApplictionContext());
AlertDialog.Builder builder = new Builder(this);
...
...
我發現,當我改變**Builder(this)**
參數去Builder(getApplicationContext());
該應用程序會崩潰!而GlowEffect2
的構造函數運行良好,無論參數是「this
」還是"getApplicationContext()".
那麼之間的差異兩個參數之間呢?
謝謝你的回答。是的,我也認爲也許「getApplicationContext()」爲空,但爲什麼「GlowEffect2(getApplicationContext())」是OK?!(PS:在GlowEffect2中我在Context中使用「TextView tv =新的TextView(上下文)「)。所以我仍然不明白爲什麼? –
您需要從崩潰中提供堆棧跟蹤,並告訴我們它指向哪個源代碼行。您還應該添加日誌以在不同階段輸出getApplicationContext()以確定它何時爲空。也許有一個計時問題,意味着在GlowEffect2代碼運行時存在getApplicationContext()? –