1

I just simply want to show Toast if device supports SDK <= 13 that's it

I am writing a small program in which allowing user to add an event to user if target sdk greater than or equal to 14 else showing Toast, but getting Unfortunately App has Stopped whenever i am using my code on device which supports API Level 8 or < 14 in place of Toast, see below code:

final Button button = (Button) findViewById(R.id.button1);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Perform action on click
                 if(Build.VERSION.SDK_INT > 8 && Build.VERSION.SDK_INT < 13)
                 {   
                     Toast.makeText(getApplicationContext(), "Some Message", Toast.LENGTH_LONG).show();                             
                }                
                 else 
                 {
                     Intent calIntent = new Intent(Intent.ACTION_INSERT);
                     calIntent.setData(Events.CONTENT_URI);
                     calIntent.putExtra(Events.TITLE, title.toString());
                     calIntent.putExtra(Events.EVENT_LOCATION, KEY_LOCATION);
                     calIntent.putExtra(Events.DESCRIPTION, description.toString());
                     startActivity(calIntent);   
                }
             }
         });
    } 

Logcat:

10-30 12:24:27.142: E/AndroidRuntime(330): FATAL EXCEPTION: main
10-30 12:24:27.142: E/AndroidRuntime(330): java.lang.NoClassDefFoundError: android.provider.CalendarContract$Events
10-30 12:24:27.142: E/AndroidRuntime(330):  at com.example.multileveljson.ProductDetailActivity$1.onClick(ProductDetailActivity.java:51)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.view.View.performClick(View.java:2408)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.view.View$PerformClick.run(View.java:8816)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.os.Handler.handleCallback(Handler.java:587)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
10-30 12:24:27.142: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-30 12:24:27.142: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
10-30 12:24:27.142: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:521)
10-30 12:24:27.142: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-30 12:24:27.142: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-30 12:24:27.142: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)

I am able to add events to devices those are using target sdk >= 14 but not able to show Toast on Devices those are using target SDK <= 13....

Sun
  • 6,768
  • 25
  • 76
  • 131

2 Answers2

3

Try to replace ProductDetailActivity.this with getApplicationContext().

Also, Calendar Contacts introduced in 4.0 which is 14+ sdk.

Build.VERSION.SDK_INT <= 13

Before 4.0 they had android.provider.Calendar

You may want to investigate that.

hasan
  • 23,815
  • 10
  • 63
  • 101
  • I suggest that you ask a new question. this one should be closed. or we answer in comments. – hasan Oct 30 '13 at 07:10
1

The class CalendarContract.Events which is you are using is added in API level 14 so thats why it is giving you exception if you want to use it your ApI version must be 14..so before Api 14 the calss is not found in the Android SDK..

kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • No..once its not because of the toast..Logcat is clearly showing NoClassDefFoundError – kalyan pvs Oct 30 '13 at 07:01
  • I just simply want to show if device supports SDK <= 13 that's it – Sun Oct 30 '13 at 07:01
  • you can not..check this for more info..http://stackoverflow.com/questions/9601456/how-to-use-calendarcontract-class-in-api-level-8 – kalyan pvs Oct 30 '13 at 07:04
  • see this issue: http://stackoverflow.com/questions/19401237/how-to-add-an-event-to-native-calendar – Sun Oct 30 '13 at 07:12