How To Get The Current View - Android

muneikh picture muneikh · Jul 9, 2012 · Viewed 10.4k times · Source

I am getting NULL pointer exception currently, when I run this thread

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");
}

I am getting the exception this line.

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

At the top of the application I have declared View view; but I know I have not linked it to any view.

My question is: How I can get the view visible to screen?

Answer

Nitin picture Nitin · Jul 9, 2012

Do not use view.findViewById(R.id.adView); use findViewById(R.id.adView);

LinearLayout ln=(LinearLayout)findViewById(R.id.yourid);
ln.addView(adView);