我正在编写一个使用NFC来读取存储在其上的数据的应用程序.我的应用程序使用Fragments和Fragment没有onNewIntent()方法.因为,我正在阅读的数据是通过处理NFC相关操作的单独类来完成的,我唯一需要做的就是更新Fragment中的TextView.但是,此实现也可用于将新Intent传递给Fragment.
这是我当前使用接口的实现.在收到新的Intent并且NFC相关检查成功后,我正在呼叫监听器.这是托管Fragment的FragmentActivity.
public class Main extends FragmentActivity implements
ActionBar.OnNavigationListener {
private Bundle myBalanceBundle;
private NFC nfcObj;
private NewBalanceListener newBlanceListener;
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
}
@Override
protected void onResume() {
getNFCState();
super.onResume();
}
private void getNFCState() {
//Other NFC related codes
else if (nfc_state == NFC.NFC_STATE_ENABLED){
readNFCTag();
}
}
private void readNFCTag() {
//Other NFC related codes
if (getIntent().getAction().equals(NfcAdapter.ACTION_TECH_DISCOVERED)) {
nfcObj.setTag((Tag) getIntent().getParcelableExtra(
NfcAdapter.EXTRA_TAG));
nfcObj.readQuickBalance();
transitQuickReadFragment(nfcObj.getCurrentBalance());
}
}
private void transitQuickReadFragment(String balance) {
// Creates a balance bundle and …Run Code Online (Sandbox Code Playgroud) NFC的在线源代码使用Activity完成.我想问一下是否有可能使用Fragment开发?
这是我运行代码时遇到的错误
java.lang.RuntimeException:无法实例化活动ComponentInfo {com.mobile.countmein/com.mobile.countmein.activities.NFCScanner}:java.lang.ClassCastException:com.mobile.countmein.activities.NFCScanner无法强制转换为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 = …Run Code Online (Sandbox Code Playgroud)