ADT 没有显示任何警告,但尝试在模拟器上运行它失败并出现异常。
(我对这段代码做了一些修改)
public class MainActivity extends Activity {
final Calendar myCalendar = Calendar.getInstance();
final EditText textEdit = (EditText) findViewById(R.id.edit_message);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
textEdit.setOnClickListener(new OnClickListener() {
public void onClick(View v ) {
// TODO Auto-generated method stub
new DatePickerDialog(MainActivity.this, date, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
textEdit.setText(sdf.format(myCalendar.getTime()));
}
错误日志:
: W/dalvikvm(2025): threadid=1: thread exiting with uncaught exception (group=0xa86ce648)
: E/AndroidRuntime(2025): FATAL EXCEPTION: main
: E/AndroidRuntime(2025): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hh_app/com.example.hh_app.MainActivity}: java.lang.NullPointerException
: E/AndroidRuntime(2025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2137)
: E/AndroidRuntime(2025): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
: E/AndroidRuntime(2025): at android.app.ActivityThread.access$600(ActivityThread.java:141)
: E/AndroidRuntime(2025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
: E/AndroidRuntime(2025): at android.os.Handler.dispatchMessage(Handler.java:99)
: E/AndroidRuntime(2025): at android.os.Looper.loop(Looper.java:137)
: E/AndroidRuntime(2025): at android.app.ActivityThread.main(ActivityThread.java:5103)
: E/AndroidRuntime(2025): at java.lang.reflect.Method.invokeNative(Native Method)
: E/AndroidRuntime(2025): at java.lang.reflect.Method.invoke(Method.java:525)
: E/AndroidRuntime(2025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
: E/AndroidRuntime(2025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
: E/AndroidRuntime(2025): at dalvik.system.NativeStart.main(Native Method)
: E/AndroidRuntime(2025): Caused by: java.lang.NullPointerException
: E/AndroidRuntime(2025): at android.app.Activity.findViewById(Activity.java:1853)
: E/AndroidRuntime(2025): at com.example.hh_app.MainActivity.<init>(MainActivity.java:19)
: E/AndroidRuntime(2025): at java.lang.Class.newInstanceImpl(Native Method)
: E/AndroidRuntime(2025): at java.lang.Class.newInstance(Class.java:1130)
: E/AndroidRuntime(2025): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
: E/AndroidRuntime(2025): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
: E/AndroidRuntime(2025): ... 11 more
谢谢你。