尝试在android中创建倒数计时器时出现运行时异常和空指针异常

时间:2014-02-16 13:08:59

标签: android nullpointerexception countdowntimer runtimeexception

我正在尝试为我的活动设计一个倒数计时器。每当我尝试运行以下代码时,都会出现错误。我将向logcat显示更多错误详细信息。

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)

logcat中还有几个错误,但是由于空间限制,我正在跳过它。我不知道倒数计时器的可能错误是什么。我需要在启动此活动后立即启动倒数计时器。有人可以帮助我。

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

}

1 个答案:

答案 0 :(得分:2)

onCreate

之后移动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

getIntent()查找当前膨胀布局中提到的id的视图。因此,您需要首先将活动的内容设置为布局,然后初始化视图。

您需要等到创建活动才能使用{{1}}。