在運行下面的代碼時,我從Eclipse獲取NullPointerException
,無法準確地確定它。這是通過Eclipse btw在Android之前的代碼。我曾嘗試將私人課程移至UpdateTimeTask
,因爲我認爲這是問題所在。epoch中的nullpointerexception(android)
public class ClockWidgetXActivity extends Activity {
private UpdateTimeTask m_updateTime;
private Handler m_handler;
private TextView time;
private final int DELAY = 100;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
TextView time = (TextView) findViewById(R.id.time1);
Typeface font = Typeface.createFromAsset(getAssets(), "Carre.ttf");
time.setTypeface(font);
time.setTextSize(65.f);
time.setText(currentDateTimeString);
m_updateTime = new UpdateTimeTask();
m_handler = new Handler();
m_handler.post(m_updateTime);
}
private class UpdateTimeTask implements Runnable {
public void run() {
String currentDateTimeString = DateFormat.getTimeInstance().format(new Date());
time.setText(currentDateTimeString);
m_handler.postDelayed(m_updateTime, DELAY);
}
}
}
的logcat:
12-01 03:26:54.642: E/AndroidRuntime(673): FATAL EXCEPTION: main
12-01 03:26:54.642: E/AndroidRuntime(673): java.lang.NullPointerException
12-01 03:26:54.642: E/AndroidRuntime(673): at com.sokydevelopers.clockWidgetX.ClockWidgetXActivity$UpdateTimeTask.run(ClockWidgetXActivity.java:23)
12-01 03:26:54.642: E/AndroidRuntime(673): at android.os.Handler.handleCallback(Handler.java:605)
12-01 03:26:54.642: E/AndroidRuntime(673): at android.os.Handler.dispatchMessage(Handler.java:92)
12-01 03:26:54.642: E/AndroidRuntime(673): at android.os.Looper.loop(Looper.java:137)
12-01 03:26:54.642: E/AndroidRuntime(673): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-01 03:26:54.642: E/AndroidRuntime(673): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 03:26:54.642: E/AndroidRuntime(673): at java.lang.reflect.Method.invoke(Method.java:511)
12-01 03:26:54.642: E/AndroidRuntime(673): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-01 03:26:54.642: E/AndroidRuntime(673): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-01 03:26:54.642: E/AndroidRuntime(673): at dalvik.system.NativeStart.main(Native Method)
謝謝,這工作就像一個魅力! – adamwhiles