如何获取当前视图 - Android

时间:2012-07-09 07:39:43

标签: android android-layout android-viewpager android-view android-viewbinder

当我运行此线程时,我正在获取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);

在应用程序的顶部,我已声明查看视图; ,但我知道我没有将其链接到任何视图。

我的问题是:我如何才能将视图显示在屏幕上?

1 个答案:

答案 0 :(得分:4)

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

LinearLayout ln=(LinearLayout)findViewById(R.id.yourid);
ln.addView(adView);
相关问题