Java threadid=1:线程以未捕获异常退出(组=0x4001d5a0)
在将倒计时脚本编程到状态栏时,但在调用 函数OnClickListener destroy以终止应用程序)。 我认为“我的通知经理。取消(1)”有问题但我不知道是什么错 这是我的密码Java threadid=1:线程以未捕获异常退出(组=0x4001d5a0),java,android,exception,Java,Android,Exception,在将倒计时脚本编程到状态栏时,但在调用 函数OnClickListener destroy以终止应用程序)。 我认为“我的通知经理。取消(1)”有问题但我不知道是什么错 这是我的密码 public class ZaagmanActivity extends Activity { public Handler handler = new Handler(); public NotificationManager myNotificationManager; private static fina
public class ZaagmanActivity extends Activity {
public Handler handler = new Handler();
public NotificationManager myNotificationManager;
private static final int NOTIFICATION_ID = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.refresh);
button.setOnClickListener(refresh);
Button button1 = (Button)findViewById(R.id.destroy);
button1.setOnClickListener(destroy);
Button button2 = (Button)findViewById(R.id.counter);
button2.setOnClickListener(counter);
//test
}
private void GeneratNotification(){
myNotificationManager =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "Counter gestart";
CharSequence NotificationTitle = "Zaagmans Counter";
CharSequence NotificationContent = Nextwedsmall();
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.poppetje,NotificationTicket, when);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this,ZaagmanActivity.class);
PendingIntent contentIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, NotificationTitle,
NotificationContent, contentIntent);
myNotificationManager.notify(NOTIFICATION_ID, notification);
}
private OnClickListener destroy = new OnClickListener() {
public void onClick(View v) {
System.out.println("Closing");
myNotificationManager.cancel(1);
handler.removeCallbacks(notification);
finish();
}
};
private OnClickListener counter = new OnClickListener() {
public void onClick(View v) {
notification.run();
}
};
private OnClickListener refresh = new OnClickListener() {
public void onClick(View v) {
refresh();
Button button1 = (Button)findViewById(R.id.destroy);
button1.setOnClickListener(destroy);
;
}
};
private final Runnable showtext = new Runnable() {
public void run() {
TextView t=new TextView(getApplicationContext());
t=(TextView)findViewById(R.id.t);
t.setText(Nextwed());
handler.postDelayed(this, 500);
}
};
private final Runnable notification = new Runnable() {
public void run() {
GeneratNotification();
handler.postDelayed(this, 500);
}
};
public Calendar Nextwednesday(int dow){
Calendar date = Calendar.getInstance();
int diff = dow - date.get(Calendar.DAY_OF_WEEK);
if (!(diff > 0)) {
diff += 7;
}
date.add(Calendar.DAY_OF_MONTH, diff);
date.set(Calendar.HOUR_OF_DAY, 12);
System.out.println(date);
return date;
} public String Nextwedsmall() {
//
long wednes = Nextwednesday(Calendar.WEDNESDAY).getTimeInMillis();
System.out.println(wednes);
Date date = new Date(); // given date
Calendar date2 = GregorianCalendar.getInstance(); // creates a new calendar instance
date2.setTime(date);
long now = date2.getTimeInMillis();
long zaagmans = wednes - now;
System.out.println("time "+date2.getTime());
long Hour = date2.get(Calendar.HOUR_OF_DAY);
Hour = Hour -2;
long Minute = date2.get(Calendar.MINUTE);
long Seconds = date2.get(Calendar.SECOND);
long mSeconds = date2.get(Calendar.MILLISECOND);
float total = (mSeconds + (1000*Seconds) + (60*1000 * Minute) + (60*60*1000 * Hour));
zaagmans = (long) (zaagmans - total + (11*60*60*1000));
long days = zaagmans / (1000*60*60*24);
long hours = zaagmans / (1000*60*60) ;
long mins = zaagmans / (1000*60) ;
long secs = zaagmans / 1000 ;
long dd = days;
long hh = hours - days * 24;
long mm = mins - hours * 60;
long ss = secs - mins * 60;
System.out.println(date2);
date2.clear();
return (dd+" dag(en), "+hh+" uur(en), "+mm+" minuten en "+ss+" seconden");
}
public String Nextwed() {
//
long wednes = Nextwednesday(Calendar.WEDNESDAY).getTimeInMillis();
System.out.println(wednes);
Date date = new Date(); // given date
Calendar date2 = GregorianCalendar.getInstance(); // creates a new calendar instance
date2.setTime(date);
long now = date2.getTimeInMillis();
long zaagmans = wednes - now;
System.out.println("time "+date2.getTime());
long Hour = date2.get(Calendar.HOUR_OF_DAY);
Hour = Hour - 13;
long Minute = date2.get(Calendar.MINUTE);
long Seconds = date2.get(Calendar.SECOND);
long mSeconds = date2.get(Calendar.MILLISECOND);
float total = (mSeconds + (1000*Seconds) + (60*1000 * Minute) + (60*60*1000 * Hour));
zaagmans = (long) (zaagmans - total);
long days = zaagmans / (1000*60*60*24);
long hours = zaagmans / (1000*60*60) ;
long mins = zaagmans / (1000*60) ;
long secs = zaagmans / 1000 ;
long dd = days;
long hh = hours - days * 24;
long mm = mins - hours * 60;
long ss = secs - mins * 60;
System.out.println(date2);
date2.clear();
return ("Dagen: "+dd+", Uren: "+hh+", Minuten: "+mm+", Seconden: "+ss+" ");
}
public void refresh(){
Calendar calendar = Calendar.getInstance();
int day = calendar.get(Calendar.DAY_OF_WEEK);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
hour = hour + 2;
if(day==4 && hour >= 12){
System.out.println("woensdag");
setContentView(R.layout.zaagmans);
}
else if(day==5){
System.out.println("donderdag");
setContentView(R.layout.zaagmans);
}
else if(day==6 && hour <= 17){
System.out.println("vrijdag");
setContentView(R.layout.zaagmans);
}
else if(day==6 && hour > 17){
System.out.println("vrijdag");
setContentView(R.layout.weekend);
}
else if(day==7 || day==1){
System.out.println("Weekend");
setContentView(R.layout.weekend);
}
else{
setContentView(R.layout.jammer);
showtext.run();
}
}
}
线路
t=(TextView)findViewById(R.id.t);
返回null,因为run方法不在任何布局文件的作用域中。顺便说一句,代码没有任何意义,因为您正在创建一个对象,并在执行此分配之前将其分配给t
请不要发布完整的源代码,只发布相关部分。哪一行是第128行?此外,请尽量将代码限制在返回日期内;第128行我将缩短代码这对错误没有帮助。当我编辑或删除此项时,计数器将不再显示。无论如何,当我删除'myNotificationManager.cancel(1);'错误消失了,但通知不会消失。顺便说一句,谢谢你的评论。我提到的那行导致了异常,因为“t”为null;)如果你删除了cancel()-调用,错误代码就不再被调用了。啊,我明白了。我会试试的
t=(TextView)findViewById(R.id.t);