Thanks Kostya, I have grasped that now and have it working in one part of my application.
I have another problem now though. I have this in code: Uri contactUri = ContentUris.withAppendedId (Contacts.People.CONTENT_URI, contactId); Intent intent = new Intent(Intent.ACTION_VIEW, contactUri); startActivity(intent); This launchs the contacts application and displays the contact according to the ID supplied, this works in all Android versions except 2.2 (API level 8) and I cant find the equivalant code for 2.2. I have tried ContactsContract.PhoneLookup, ContactsContract.Contact, ContactsContract.CommonDataKinds but none of them seem to let me launch the contacts application and dispaly a contacts info according to a supplied ID. Can anyone help with this? On Tue, Jul 27, 2010 at 4:27 PM, Kostya Vasilyev <[email protected]> wrote: > Hi, > > The offending line is this: > > ContactsContract.Contacts > > Even though you're not referencing this nested member on the execution > path, the Java VM still tries to resolve all external references made by > your code. This is done when your code is first loaded into memory for > execution, before it is run. At this point, the external reference fails > (since you are on Android 1.5), and your code is not able to load and > execute. > > The solution is to use Java Reflection feature to not make any compile-time > references to missing classes / members, but rather check at runtime whether > they exist. This way, there are no compile-time references to stuff that > only exists in later API levels, and your code can gracefully fall back to > earlier APIs at runtime. > > The key is what references your code makes at compile time, and, as a > consequence, when your class is loaded into memory. > > http://java.sun.com/developer/technicalArticles/ALT/Reflection/ > > That is the general solution. > > However, in this particular case, you could duplicate the URI string from > > ContactsContract.Contacts.CONTENT_URI > > to some string constant in your code. Just like this: > > class YourClass { > > private static final String ANDROID_1_X_CONTACTS_URI = People.CONTENT_URI; > > private static final String ANDROID_2_X_CONTACTS_URI = "content://<put > correct value here>"; > } > > Then use the appropriate constant (one of the two) based on API level just > like you're trying to do now. > > -- Kostya > > 27.07.2010 18:10, [email protected] пишет: > > I have the following piece of code in my Android application that >> looks up a contacts ID, I have just changed the target from 1.5 to 2.2 >> but with a min SDK of 3 (1.5) so that I can use the ContactsContract >> Uri in 2.2 and the People Uri in 1.5. >> >> However the code leads to the following error when run on 1.5 devices: >> >> 07-27 15:02:53.382: WARN/dalvikvm(12656): VFY: unable to resolve >> static field 25 (CONTENT_URI) in Landroid/provider/ContactsContract >> $Contacts; >> >> --- >> >> > From google I have garnered that I need to use reflection in this case >> to allow the application run on both versions of Android? >> >> I have seen example's of how to use reflection to use methods of >> multiple/different versions but how can I use it in mycase where I >> want to use the ContactsContract Uri? >> >> Here is my code: >> >> --- >> >> >> findViewById(R.id.contactimage).setOnClickListener(new >> OnClickListener() { >> public void onClick(View v) { >> >> String sdk = android.os.Build.VERSION.SDK; >> Uri contactUri; >> Log.d("CDA", "Contact ID Button pressed = " >> + >> contactId); >> if(sdk.equals("8")){ >> contactUri = >> ContentUris.withAppendedId >> >> (ContactsContract.Contacts.CONTENT_URI, contactId); >> >> Intent intent = new >> Intent(Intent.ACTION_VIEW, >> contactUri); >> startActivity(intent); >> >> } >> else{ >> contactUri = ContentUris.withAppendedId >> (People.CONTENT_URI, contactId); >> >> Intent intent = new >> Intent(Intent.ACTION_VIEW, >> contactUri); >> startActivity(intent); >> } >> >> dispatchKeyEvent(new >> KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.FLAG_SOFT_KEYBOARD)); >> dispatchKeyEvent(new >> KeyEvent(KeyEvent.ACTION_UP, >> KeyEvent.KEYCODE_BACK)); >> } >> }); >> >> >> > > > -- > Kostya Vasilev -- WiFi Manager + pretty widget -- > http://kmansoft.wordpress.com > > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected]<android-developers%[email protected]> > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en > -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

