{"id":6667,"date":"2014-04-20T21:49:37","date_gmt":"2014-04-20T21:49:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/android-nfc-development-using-fragment-collection-of-common-programming-errors\/"},"modified":"2014-04-20T21:49:37","modified_gmt":"2014-04-20T21:49:37","slug":"android-nfc-development-using-fragment-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/20\/android-nfc-development-using-fragment-collection-of-common-programming-errors\/","title":{"rendered":"Android NFC development using Fragment-Collection of common programming errors"},"content":{"rendered":"<li><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/776c7ee3de342cca0c6a5c7b57b58827?s=32&amp;d=identicon&amp;r=PG\" \/><br \/>\nchuntato<\/p>\n<p>The online source codes for NFC are done using Activity. I would like to ask if it is possible to develop using Fragment instead?<\/p>\n<p>This is the error i hit when i run the codes<\/p>\n<p><strong>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<\/strong><\/p>\n<pre><code>public class TagViewer extends Fragment {\n\nprivate static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat();\nprivate LinearLayout mTagContent;\n\nprivate NfcAdapter mAdapter;\nprivate PendingIntent mPendingIntent;\nprivate NdefMessage mNdefPushMessage;\n\nprivate AlertDialog mDialog;\n\n@Override\npublic View onCreateView(LayoutInflater inflater, ViewGroup container,\n        Bundle savedInstanceState) {\n\n    return inflater.inflate(R.layout.tag_viewer, container, false);\n}\n\n\npublic void onViewCreated(View view, Bundle savedInstanceState) {\n    super.onViewCreated(view, savedInstanceState);\n\n      mTagContent = (LinearLayout)view.findViewById(R.id.list);\n        resolveIntent(getActivity().getIntent());\n\n        mDialog = new AlertDialog.Builder(getActivity()).setNeutralButton(\"Ok\", null).create();\n\n        mAdapter = NfcAdapter.getDefaultAdapter(getActivity());\n        if (mAdapter == null) {\n            showMessage(R.string.error, R.string.no_nfc);\n        }\n\n        mPendingIntent = PendingIntent.getActivity(getActivity(), 0,\n                new Intent(getActivity(), getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);\n        mNdefPushMessage = new NdefMessage(new NdefRecord[] { newTextRecord(\n                \"Message from NFC Reader :-)\", Locale.ENGLISH, true) });\n\n}\n\n\nprivate void showMessage(int title, int message) {\n    mDialog.setTitle(title);\n    mDialog.setMessage(getText(message));\n    mDialog.show();\n}\n\nprivate NdefRecord newTextRecord(String text, Locale locale, boolean encodeInUtf8) {\n    byte[] langBytes = locale.getLanguage().getBytes(Charset.forName(\"US-ASCII\"));\n\n    Charset utfEncoding = encodeInUtf8 ? Charset.forName(\"UTF-8\") : Charset.forName(\"UTF-16\");\n    byte[] textBytes = text.getBytes(utfEncoding);\n\n    int utfBit = encodeInUtf8 ? 0 : (1 = 0; --i) {\n        int b = bytes[i] &amp; 0xff;\n        if (b &lt; 0x10)\n            sb.append('0');\n        sb.append(Integer.toHexString(b));\n        if (i &gt; 0) {\n            sb.append(\" \");\n        }\n    }\n    return sb.toString();\n}\n\nprivate long getDec(byte[] bytes) {\n    long result = 0;\n    long factor = 1;\n    for (int i = 0; i &lt; bytes.length; ++i) {\n        long value = bytes[i] &amp; 0xffl;\n        result += value * factor;\n        factor *= 256l;\n    }\n    return result;\n}\n\nvoid buildTagViews(NdefMessage[] msgs) {\n    if (msgs == null || msgs.length == 0) {\n        return;\n    }\n    LayoutInflater inflater = LayoutInflater.from(getActivity());\n    LinearLayout content = mTagContent;\n\n    \/\/ Parse the first message in the list\n    \/\/ Build views for all of the sub records\n    Date now = new Date();\n    List records = NdefMessageParser.parse(msgs[0]);\n    final int size = records.size();\n    for (int i = 0; i &lt; size; i++) {\n        TextView timeView = new TextView(getActivity());\n        timeView.setText(TIME_FORMAT.format(now));\n        content.addView(timeView, 0);\n        ParsedNdefRecord record = records.get(i);\n        content.addView(record.getView(getActivity(), inflater, content, i), 1 + i);\n        content.addView(inflater.inflate(R.layout.tag_divider, content, false), 2 + i);\n    }\n}\n<\/code><\/pre>\n<p>}<\/p>\n<p>I removed the following because a fragment does not have the method:<\/p>\n<pre><code>@Override \npublic void onNewIntent(Intent intent) {\n  setIntent(intent);\n  resolveIntent(intent);\n}\n<\/code><\/pre>\n<\/li>\n","protected":false},"excerpt":{"rendered":"<p>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 { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6667","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=6667"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6667\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}