gpt4 book ai didi

java - 显示当前时间

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:05 25 4
gpt4 key购买 nike

我目前正在尝试向自定义 ListView 显示当前时间,但我不断收到 NullPointerException,并且想知道我做错了什么,因为我已经添加到数组并获取其值。

自定义适配器和 setter / getter 位于:1 2

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

listView = (ListView) findViewById(R.id.list_view);
boss_title = getResources().getStringArray(R.array.boss_array);
bossTime = (TextView) findViewById(R.id.boss_time);

adapter = new BossAdapter(getApplicationContext(), R.layout.boss_layout);
listView.setAdapter(adapter);
int i = 0;


Thread t = new Thread() {

@Override
public void run() {
try {
while (!isInterrupted()) {
Thread.sleep(1000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Calendar c = Calendar.getInstance();
SimpleDateFormat df = new SimpleDateFormat("HH-mm-ss");
String formattedTime = df.format(c.getTime());

String [] boss_time_array = new String[6];
for(int i =0; i < 6; i++){
boss_time_array[i] = formattedTime;
}

boss_time = boss_time_array;

}
});
}
} catch (InterruptedException e) {
}
}
};

t.start();

for (String boss : boss_title) {
Boss bossObject = new Boss(boss_icon[i], boss, boss_time[i]);

adapter.add(bossObject);
i++;
}

}

日志猫

 8541-8541/baegmon.com.bosstimer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: baegmon.com.bosstimer, PID: 8541
java.lang.RuntimeException: Unable to start activity ComponentInfo{baegmon.com.bosstimer/baegmon.com.bosstimer.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at baegmon.com.bosstimer.MainActivity.onCreate(MainActivity.java:67)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

如何修改它,使其不返回空指针错误并向我的 ListView 返回正确的时间?

最佳答案

您会收到 NullPointerException,因为创建适配器时线程尚未完成。线程启动后,您将立即读取 boss_title 的结果。这是不可能的,因为您必须等待它完成。

您应该为此使用 AsyncTask 并在 onPostExecute ( http://developer.android.com/reference/android/os/AsyncTask.html ) 内创建适配器。

此外,SimpleDateFormat 类不是线程安全的:

public static Date dateToString(Date date, final String format, final Locale locale)
ThreadLocal<SimpleDateFormat> formater = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat(format, locale);
}
};
return formater.get().format(string);
}

关于java - 显示当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31511256/

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