Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:14:29+00:00 2026-06-15T06:14:29+00:00

Thanks to the replies on NFC and a few certain things, I’ve understood and

  • 0

Thanks to the replies on NFC and a few certain things, I’ve understood and managed to compile a code in which the users will be able to read a tag, and if the tag contains a string that is similar to my code, a coupon will be added (image changes) and an integer goes up by 1. This integer will be saved by SharedPreferences and it is used to determine how many coupons the users have collected and show it onResume.

However, after compiling, when I try to run it, my application stops immediately. Can someone help me check on what I may have go wrong? I know it’s kinda long but I really have no idea what went wrong.

@TargetApi(10)    
//I have to use this line of code because I'm targetted to code at API 8 but some NFC functionalities that I use requires API 10.

public class CouponManager extends Activity {

        private static final String TAG = "NFCReadTag";
        private NfcAdapter mNfcAdapter;
        private IntentFilter[] mNdefExchangeFilters;
        private PendingIntent mNfcPendingIntent;
        public static final String PREF_FILE_NAME = "PrefFile";

        private int[] images = new int[10];

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.coupon_layout);

            //List of images
            images[0]=R.drawable.cp0;
            images[1]=R.drawable.cp1;
            images[2]=R.drawable.cp2;
            images[3]=R.drawable.cp3;
            images[4]=R.drawable.cp4;
            images[5]=R.drawable.cp5;
            images[6]=R.drawable.cp6;
            images[7]=R.drawable.cp7;
            images[8]=R.drawable.cp8;
            images[9]=R.drawable.cp9;
            images[10]=R.drawable.cp10;

            //Restore preferences 
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            //Image to use depending on coupon collected
            final ImageView img = new ImageView(this);

            if(storedPreference!=10)
            {
                img.setImageResource(images[storedPreference]);
            }
            else
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setCancelable(false);
                builder.setTitle("Redeem Your Coupon?");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.dismiss();
                        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putInt("storedInt", 0); // value to store
                        editor.commit();    
                        img.setImageResource(images[0]);
                    }
                });
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        img.setImageResource(images[10]);
                    }
                });
            }


            //Check and send Intent from NFC tag discovered
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);

            mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);


            IntentFilter coupontag = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
            coupontag.addDataScheme("http");
            coupontag.addDataAuthority("www.ichatime.com", null);
            coupontag.addDataPath(".*", PatternMatcher.PATTERN_SIMPLE_GLOB);

            mNdefExchangeFilters = new IntentFilter[] { coupontag };

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

        @Override
        protected void onResume() {
            super.onResume();
            if(mNfcAdapter != null) {
                mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent,
                    mNdefExchangeFilters, null);
            } else {
                Toast.makeText(getApplicationContext(), "Sorry, No NFC Adapter found.", Toast.LENGTH_SHORT).show();
            }


        }

        @Override
        protected void onPause() {
            super.onPause();
            if(mNfcAdapter != null) mNfcAdapter.disableForegroundDispatch(this);
        }

        @Override
        protected void onStop() {
            super.onStop();

            // We need an Editor object to make preference changes.
              // All objects are from android.context.Context
                SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                int storedPreference = preferences.getInt("storedInt", 0);

                SharedPreferences.Editor editor = preferences.edit();
                editor.putInt("storedInt", storedPreference); // value to store
                editor.commit();    
        }

        @Override
        protected void onNewIntent(Intent intent) {
            super.onNewIntent(intent);      
            SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
            int storedPreference = preferences.getInt("storedInt", 0);

            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
                NdefMessage[] messages = null;
                Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
                if (rawMsgs != null) {
                    messages = new NdefMessage[rawMsgs.length];
                    for (int i = 0; i < rawMsgs.length; i++) {
                        messages[i] = (NdefMessage) rawMsgs[i];
                    }
                }
                if(messages[0] != null) {
                    String result="";
                    byte[] payload = messages[0].getRecords()[0].getPayload();
                    // this assumes that we get back am SOH followed by host/code
                    for (int b = 1; b<payload.length; b++) { // skip SOH
                        result += (char) payload[b];
                    }
                    if (result == "ichatime.com")
                        {
                        final ImageView img = new ImageView(this);
                        Toast.makeText(getApplicationContext(), "Coupon collected!", Toast.LENGTH_SHORT).show();

                        if (storedPreference!=10)
                        {
                            storedPreference++;
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("storedInt", storedPreference);
                            img.setImageResource(images[storedPreference]);
                        }
                            if (storedPreference==10)
                            {
                                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                                builder.setCancelable(false);
                                builder.setTitle("Redeem Your Coupon?");
                                builder.setInverseBackgroundForced(true);
                                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        dialog.dismiss();
                                        SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
                                        SharedPreferences.Editor editor = preferences.edit();
                                        editor.putInt("storedInt", 0); // value to store
                                        editor.commit();    
                                        img.setImageResource(images[0]);
                                    }
                                });
                                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                        img.setImageResource(images[10]);
                                    }
                                });
                            }
                        else
                        {
                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putInt("storedInt", 10);
                            img.setImageResource(images[10]);
                        }}
                    else 
                    {
                        Toast.makeText(getApplicationContext(), "Wrong tag detected!", Toast.LENGTH_SHORT).show();
                    }
                    //Debugging Mode to see what is contained in the tags.
            //          Toast.makeText(getApplicationContext(), "Tag Contains " + result, Toast.LENGTH_SHORT).show();
                }
            }
        }

}

Logcat errors:

>11-26 01:16:11.869: D/AndroidRuntime(550): Shutting down VM
>
11-26 01:16:11.869: W/dalvikvm(550): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
>
11-26 01:16:11.929: I/dalvikvm(550): threadid=3: reacting to signal 3
>
11-26 01:16:11.979: E/AndroidRuntime(550): FATAL EXCEPTION: main
>
**11-26 01:16:11.979: E/AndroidRuntime(550): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ponpon/com.example.ponpon.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ponpon/com.example.ponpon.CouponManager}: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10**
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.os.Handler.dispatchMessage(Handler.java:99)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.os.Looper.loop(Looper.java:137)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.main(ActivityThread.java:4424)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at java.lang.reflect.Method.invokeNative(Native Method)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at java.lang.reflect.Method.invoke(Method.java:511)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at dalvik.system.NativeStart.main(Native Method)
>
11-26 01:16:11.979: E/AndroidRuntime(550): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ponpon/com.example.ponpon.CouponManager}: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.widget.TabHost.setCurrentTab(TabHost.java:346)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.widget.TabHost.addTab(TabHost.java:236)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at com.example.ponpon.MainActivity.onCreate(MainActivity.java:37)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.Activity.performCreate(Activity.java:4465)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  ... 11 more
>
11-26 01:16:11.979: E/AndroidRuntime(550): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=10; index=10
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at com.example.ponpon.CouponManager.onCreate(CouponManager.java:53)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.Activity.performCreate(Activity.java:4465)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
>
11-26 01:16:11.979: E/AndroidRuntime(550):  ... 21 more

What did I do wrong with my arrays? Thanks for the clarification guys!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:14:31+00:00Added an answer on June 15, 2026 at 6:14 am

    Your logcat is printing an ArrayOutOfBounds exception on your onCreate method.

    The problem is that you are declaring a 10 items sized array, and then trying to put 11 items on it.

    You have to declare a new int[11] array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Thanks for previous replies, I tried to check the data connection through code, I
Thanks for your replies. First I am explaining my requirement :- I created my
Thanks for previous replies, is it possible to delete the stored content from sqlite
thanks in advance for the replies.... I have started to create a network topology
Thanks to the epic work of Dennis Williamson, I am now able to calculate
Thanks for previous replies, I am doing application with widget, my idea is to
Edit - Thanks for your replies so far and sorry for not stating exactly
Thanks for previous replies, I am new to Facebook sharing. I am trying to
Thanks for previous replies, How do we set the alias and tag in device
Thanks for previous replies, I am very new to Urban_Airship and integrating this into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.