Android NFC development using Fragment-Collection of common programming errors


  • chuntato

    The online source codes for NFC are done using Activity. I would like to ask if it is possible to develop using Fragment instead?

    This is the error i hit when i run the codes

    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mobile.countmein/com.mobile.countmein.activities.NFCScanner}: java.lang.ClassCastException: com.mobile.countmein.activities.NFCScanner cannot be cast to android.app.Activity

    public class TagViewer extends Fragment {
    
    private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat();
    private LinearLayout mTagContent;
    
    private NfcAdapter mAdapter;
    private PendingIntent mPendingIntent;
    private NdefMessage mNdefPushMessage;
    
    private AlertDialog mDialog;
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        return inflater.inflate(R.layout.tag_viewer, container, false);
    }
    
    
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
          mTagContent = (LinearLayout)view.findViewById(R.id.list);
            resolveIntent(getActivity().getIntent());
    
            mDialog = new AlertDialog.Builder(getActivity()).setNeutralButton("Ok", null).create();
    
            mAdapter = NfcAdapter.getDefaultAdapter(getActivity());
            if (mAdapter == null) {
                showMessage(R.string.error, R.string.no_nfc);
            }
    
            mPendingIntent = PendingIntent.getActivity(getActivity(), 0,
                    new Intent(getActivity(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
            mNdefPushMessage = new NdefMessage(new NdefRecord[] { newTextRecord(
                    "Message from NFC Reader :-)", Locale.ENGLISH, true) });
    
    }
    
    
    private void showMessage(int title, int message) {
        mDialog.setTitle(title);
        mDialog.setMessage(getText(message));
        mDialog.show();
    }
    
    private NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) {
        byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
    
        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
        byte[] textBytes = text.getBytes(utfEncoding);
    
        int utfBit = encodeInUtf8 ? 0 : (1 = 0; --i) {
            int b = bytes[i] & 0xff;
            if (b < 0x10)
                sb.append('0');
            sb.append(Integer.toHexString(b));
            if (i > 0) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
    
    private long getDec(byte[] bytes) {
        long result = 0;
        long factor = 1;
        for (int i = 0; i < bytes.length; ++i) {
            long value = bytes[i] & 0xffl;
            result += value * factor;
            factor *= 256l;
        }
        return result;
    }
    
    void buildTagViews(NdefMessage[] msgs) {
        if (msgs == null || msgs.length == 0) {
            return;
        }
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        LinearLayout content = mTagContent;
    
        // Parse the first message in the list
        // Build views for all of the sub records
        Date now = new Date();
        List records = NdefMessageParser.parse(msgs[0]);
        final int size = records.size();
        for (int i = 0; i < size; i++) {
            TextView timeView = new TextView(getActivity());
            timeView.setText(TIME_FORMAT.format(now));
            content.addView(timeView, 0);
            ParsedNdefRecord record = records.get(i);
            content.addView(record.getView(getActivity(), inflater, content, i), 1 + i);
            content.addView(inflater.inflate(R.layout.tag_divider, content, false), 2 + i);
        }
    }
    

    }

    I removed the following because a fragment does not have the method:

    @Override 
    public void onNewIntent(Intent intent) {
      setIntent(intent);
      resolveIntent(intent);
    }