gpt4 book ai didi

java - 从其他类调用公共(public)字符串

转载 作者:搜寻专家 更新时间:2023-11-01 08:55:06 25 4
gpt4 key购买 nike

我尝试获取带时间的字符串 (hh:mm:ss) 但出现此错误:

     10-28 12:35:27.682: E/AndroidRuntime(14002): java.lang.NullPointerException

我通过我的主类调用它:

private TimeUpdater timeupdater;
.
.
.
private Runnable runnable = new Runnable(){

public void run() {

datasource = new DataSource(getBaseContext());
datasource.open();

comment = datasource.getComment("1");
data = comment.toString().split(" ");



/*for(int i=0;i<11;i++)
{

dataUpdate[i]=data[i+1];
System.out.println("U " + dataUpdate[i]);
System.out.println("D " + data[i+1]);
}*/

String a = timeupdater.upTime(dane[5]);

System.out.println(a);

time.setText(timeupdater.upTime(dane[5]));
//for(int i=0;i<12;i++)
//System.out.println(dane[i]);



handler.postDelayed(this, 1000);
}
};

内部数据[5]是00:00:00
这是我的时间更新器类:

public class TimeUpdater {

private int hh, mm, ss;
private String time;
private String shh, smm, sss;
public String upTime(String time) {

String[] czas = time.split(":");
hh = Integer.parseInt(czas[0]);
mm = Integer.parseInt(czas[1]);
ss = Integer.parseInt(czas[2]);
if(ss<60)
ss++;
if(ss==60)
{
ss=0;
mm++;
}
if(mm==60)
hh++;
if(hh<10)
shh = "0"+hh;
if(mm<10)
smm = "0"+mm;
if(ss<10)
sss = "0"+ss;
this.time = shh + ":" + smm + ":" + sss;
System.out.println(time);
return this.time;
}

我需要,因为以后我想做更多的电话。我需要知道如何在其他类中创建公共(public)字符串以每秒更改数据

最佳答案

在调用方法upTime时,需要先实例化它的参数(time),然后再调用该方法。

这是异常发生的地方

String[] czas = time.split(":");

现在因为 time 没有被实例化,你得到了 NullPointerException

关于java - 从其他类调用公共(public)字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19634033/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com