Question

I'm trying to design a countdown timer for my activity. Whenever I try to run the below code, there is an error coming out. I will show the logcat for more error details.

02-16 18:12:07.535: E/AndroidRuntime(550): FATAL EXCEPTION: main
02-16 18:12:07.535: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to     instantiate activity ComponentInfo{com.example.onlineauction/com.example.onlineauction.Placed_Product_Details}: java.lang.NullPointerException
02-16 18:12:07.535: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)

There are several more errors in the logcat, but due to space limitation, I'm skipping it. I don't know what is the possible error for this countdown timer. I need to start the countdown timer as soon as this activity is launched. Can some one please help me out.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("SimpleDateFormat")
public class Placed_Product_Details extends Activity {

TextView timerv=(TextView)findViewById(R.id.timer);
public timer tim;
Intent i=getIntent();
final String productbiddate=i.getStringExtra("productbiddate");
final String productplaceddate=i.getStringExtra("productplaceddate");
TextView pbid=(TextView)findViewById(R.id.pbid);
TextView pplaced=(TextView)findViewById(R.id.pplaced);
SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yy hh:mm:ss aa");


@SuppressLint("SimpleDateFormat")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_placed__product__details);

    try
    {

     // Converting the two string dates into date time format and comparing the dates
        Date dt1=sdf.parse(productbiddate);
        Date dt2=sdf.parse(productplaceddate);
        int val=dt1.compareTo(dt2);
        if(val>0)
        {
        long diff=Math.abs(dt1.getTime()-dt2.getTime());
        long seconds=(TimeUnit.MILLISECONDS.toSeconds(diff))%60;
        long minutes=(TimeUnit.MILLISECONDS.toMinutes(diff))%60;
        long hours=(TimeUnit.MILLISECONDS.toHours(diff))%24;
            long days = TimeUnit.MILLISECONDS.toDays(diff);
        //here I'm trying to start my timer in my activity.
            tim=new timer(diff, 1000);
            tim.start();
            }
    pbid.setText("Bid Ends On: "+productbiddate);
        pplaced.setText("Product Was Placed For Bidding On: "+productplaceddate);

    }
    catch(Exception e)
    {
        Log.d("Exception:",e.toString());
    }  

}
public class timer extends CountDownTimer
{

    public timer(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onFinish() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTick(long millisUntilFinished) {
        // TODO Auto-generated method stub
    long seconds=(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))%60;
    long minutes=(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))%60;
    long hours=(TimeUnit.MILLISECONDS.toHours(millisUntilFinished))%24;
    long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
timerv.setText("Time Remaining: "+days+" days "+hours+" hours "+hours+" minutes "+minutes+" seconds "+seconds);

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.placed__product__details, menu);
    return true;
}

}
Was it helpful?

Solution

Move the below inside onCreate after setContentView

 TextView pbid=(TextView)findViewById(R.id.pbid);
 TextView pplaced=(TextView)findViewById(R.id.pplaced);
 TextView timerv=(TextView)findViewById(R.id.timer);
 Intent i=getIntent();
 final String productbiddate=i.getStringExtra("productbiddate");
 final String productplaceddate=i.getStringExtra("productplaceddate");

findViewById looks for a view with the id mentioned in the current inflated layout. So you need to set the content of the activity first to the layout and then initialize views.

You need to wait till activity is created to use getIntent().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top