Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialWei-Ting Chen
7,632 Pointscan not resolve constructor 'Date()'
Hi everyone, I followed the instruction of this course but there is an error shows "can not resolve constructor 'Date()'". Where did I do wrongly?
``Android
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
Intent takePhotoIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mMediaUri=getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
if(mMediaUri==null){
Toast.makeText(MainActivity.this,R.string.error_external_storage,Toast.LENGTH_LONG).show();
}
else {
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
}
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
private Uri getOutputMediaFileUri(int mediaTypeImage) {
if(isExternalStorageAvailable()){
String appName=MainActivity.this.getString(R.string.app_name);
File mediaStorageDir =new File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),appName);
if(! mediaStorageDir.exists()){
if(! mediaStorageDir.mkdirs()){
Log.e(TAG,"Failed to create directory");
return null;
}
}
File mediaFile;
Date now=new Date();
String timestamp=new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.TAIWAN).format(now);
``
Wei-Ting Chen
7,632 PointsThanks!!!! I found out that I imported the "import java.sql.Date;" 謝謝你:)
1 Answer
Vrezh Gulyan
11,161 PointsYou need to import java.util.date . In general if you are using Android Studio hit alt + enter to get a quick fix and it should automatically import the necessary classes. It also help to use auto complete rather then type out a method when you are following along even though something like date is easy to type. When you use autocomplete it automatically imports the applicable classes along with finishing what you were writing.
Whitney Foster
2,744 PointsWhitney Foster
2,744 PointsCheck to see that you imported Date "import java.util.Date;"