-1

当我运行这个线程时,我目前收到 NULL 指针异常

public void run() {
    long idle = 0;
    this.touch();
    do {
        idle = System.currentTimeMillis() - lastUsed;
        Log.d(TAG, "Application is idle for " + idle + " ms");
        if ((idle > 5000) && (check == false)) { // Checking For 2 Minutes.
            Log.e("AthanAd", "2 minutes over - Show Ad.");
            adView = (AdView) view.findViewById(R.id.adView);
            adView.loadAd(new AdRequest());
            check = true;
        }
        try {
            Thread.sleep(8000); // check every 5 seconds
        } catch (InterruptedException e) {
            Log.d(TAG, "Waiter interrupted!");
        }
        if (idle > period) {
            idle = 0;
            // do something here - e.g. call popup or so
        }
    } while (!stop);
    Log.d(TAG, "Finishing Waiter thread");
}

我在这条线上得到了例外。

adView = (AdView) view.findViewById(R.id.adView);

在应用程序的顶部,我声明了View 视图;但我知道我没有将它与任何观点联系起来。

我的问题是:我怎样才能让视图在屏幕上可见?

4

1 回答 1

4

不要使用view.findViewById(R.id.adView); 使用findViewById(R.id.adView);

LinearLayout ln=(LinearLayout)findViewById(R.id.yourid);
ln.addView(adView);
于 2012-07-09T07:50:20.507 回答