-1

I am Getting an nullPointerException at "button.setOnClickListener(this);"....while changing layout in CountDownTimer 's finish() method. here is code...Please help me to solve this....... thanks in advance

package com.example.vikrant.countdowntimer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button button;
    ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Context c = getApplicationContext();
         new CountDownTimer(10000,5000){

             @Override
             public void onTick(long millisUntilFinished) {
                 setContentView(R.layout.timer);
             }

             @Override
             public void onFinish() {
                 setContentView(R.layout.activity_main);
             }
         }.start();

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        String url="https://firebasestorage.googleapis.com/v0/b/fir-assignment-34e5b.appspot.com/o/rangoliA%2Frangolinew.jpg?alt=media&token=4a376937-7fec-420a-a18c-8c9ec7da95af";
        image = (ImageView) findViewById(R.id.imageView);
        DownloadImage downloadImage = new DownloadImage();
        Bitmap bitmap;
        try {
            bitmap = downloadImage.execute(url).get();
            image.setImageBitmap(bitmap);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    public class DownloadImage extends AsyncTask<String,Void,Bitmap>{

        @Override
        protected Bitmap doInBackground(String... params) {
            URL url;
            HttpURLConnection httpURLConnection;
            try {
                url = new URL(params[0]);
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.connect();
                InputStream inputStream = httpURLConnection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;

            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

        }
    }
}

1 Answers1

0

Your button is probably defined in your "R.layout.activity_main". When you set another content, the "findViewById(R.id.button)" will return null (because its not defined in the other layout).

Why do you change the layouts after all?

Prexx
  • 2,959
  • 5
  • 31
  • 48
  • Yes the button is defined in activity_main.xml....I want to change the layout automatically when my app start...is there any way by which i will not get this exception and do what i want?? – Vikrant Tyagi Jul 21 '17 at 08:12
  • are you talking about splsh screen in android ? https://stackoverflow.com/questions/5486789/how-do-i-make-a-splash-screen – King of Masses Jul 21 '17 at 08:18