I've struggled with this for over a week, because I figure it has to be something with my environment, but I can't narrow it down.
I start an empty project, add a single "Blank" activity and accept all defaults. After the project finishes setting up, I add a "Settings" activity.
Inside MainActivity.java, I add two lines of code to connect the two activities, and then build the project.
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
I send the application to debug, and when I pick "Settings" on the action bar, the application crashes with:
android.content.res.Resources$NotFoundException: File res/drawable/ic_sync_black_24dp.xml from drawable resource ID #0x7f020049
I get this same error on the virtual images, and when I send the APK to a real physical device and try to view the settings activity.
I've started new and clean projects several times, and I get the same result with each project. I've tried clean builds, I've tried deleting the R.java stuff. When I look at the res/drawable/ic_sync_black_24dp.xml in my project (which was created by the add new activity wizard) I can see the icon and everything looks normal.
If I remove the one line from the pref_headers.xml file that specifies this icon, the project builds fine, and the settings activity has icons for the other two choices, but not the ic_sync_black_24dp.xml icon.
Here's the line I have to manually delete from pref_headers.xml:
android:icon="@drawable/ic_sync_black_24dp"
So this is the valid pref_headers.xml file for me:
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="com.schramauto.anothertrial.SettingsActivity$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/pref_header_general" />
<header
android:fragment="com.schramauto.anothertrial.SettingsActivity$NotificationPreferenceFragment"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/pref_header_notifications" />
<header
android:fragment="com.schramauto.anothertrial.SettingsActivity$DataSyncPreferenceFragment"
android:title="@string/pref_header_data_sync" />
In res\drawable, there are a 3 xml files added by the wizard that added the settings activity. All 3 display properly for me and show a valid preview of an icon.
Here is the contents of the "good" xml, ic_info_black_24dp.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zm1,15h-2v-6h2v6zm0,-8h-2V7h2v2z" />
</vector>
Here is the questionable XML (ic_sync_black_24dp.xml):
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24.0dp"
android:height="24.0dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01,-.25 1.97,-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0,-4.42,-3.58,-8,-8,-8zm0 14c-3.31 0,-6,-2.69,-6,-6 0,-1.01.25,-1.97.7,-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4,-4,-4,-4v3z" />
</vector>
Again, I've tried this maybe 15 times (creating brand new projects and connecting the two activities), rebooted, searched google, searched here, and I can't figure out what's wrong with ic_sync_black_24dp.xml that the wizard put in my project.
Since I'm making an assumption that this may be environmental in my IDE or my local build configuration, here are some high level details on my setup: Windows 10, Android Studio 1.5, JRE 1.8.0
Seems that the issue lay with API 21, the 24.0dp that was mentioned did not solve the issue for me instead I was receiving:
E/PathParser: error in parsing "c-3.31 0,-6,-2.69,-6,-6 0,-1.01.25,-1.97.7,-2.8"
I found the answer at https://github.com/google/material-design-icons/issues/225
It turns out the pathData is invalid cannot be parsed in API 21 properly (the problem does not exist in API 22 or 23). Simply replace the pathData with:
android:pathData="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01,-.25 1.97,-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0,-4.42,-3.58,-8,-8,-8zm0 14c-3.31 0,-6,-2.69,-6,-6 0,-1.01 .25,-1.97 .7,-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4,-4,-4,-4v3z"
Lewis noticed the clue that help me resolve the issue. I changed the 24.0 to 24
android:width="24dp"
android:height="24dp"
And now the code executes without error! Thanks Lewis!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With