当我调用NfcAdapter.getDefaultAdapter(this)时,我得到null.为什么会这样?
码:
public class Foregrounddispatch extends Activity {private NfcAdapter mAdapter;private PendingIntent mPendingIntent;private IntentFilter[] mFilters;private String[][] mTechLists;private TextVIEw mText;private int mCount = 0;@OverrIDepublic voID onCreate(Bundle savedState) { super.onCreate(savedState); setContentVIEw(R.layout.foreground_dispatch); mText = (TextVIEw) findVIEwByID(R.ID.text); mText.setText("Scan a tag"); mAdapter = NfcAdapter.getDefaultAdapter(this); // Create a generic PendingIntent that will be deliver to this activity. The NFC stack // will fill in the intent with the details of the discovered tag before delivering to // this activity. mPendingIntent = PendingIntent.getActivity(this,new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_top),0); // Setup an intent filter for all MIME based dispatches IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_disCOVERED); try { ndef.addDataType("*/*"); } catch (MalformedMimeTypeException e) { throw new RuntimeException("fail",e); } mFilters = new IntentFilter[] { ndef,}; // Setup a tech List for all NfcF Tags mTechLists = new String[][] { new String[] { NfcF.class.getname() } };}@OverrIDepublic voID onResume() { super.onResume(); mAdapter.enableForegrounddispatch(this,mPendingIntent,mFilters,mTechLists); //CRASHES HERE BECAUSE mAdapter IS NulL}@OverrIDepublic voID onNewIntent(Intent intent) { Log.i("Foreground dispatch","discovered tag with intent: " + intent); mText.setText("discovered tag " + ++mCount + " with intent: " + intent);}@OverrIDepublic voID onPause() { super.onPause(); mAdapter.disableForegrounddispatch(this); } }
我的清单:
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.neka.znacka" androID:versionCode="1" androID:versionname="1.0"><uses-sdk androID:minSdkVersion="10" /><uses-permission androID:name="androID.permission.NFC"></uses-permission><uses-feature androID:name="androID.harDWare.nfc" androID:required="true" /><application androID:icon="@drawable/icon" androID:label="@string/app_name"> <activity androID:name=".Uvodna" androID:label="@string/app_name"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name="Simulator"> </activity></application>
有任何想法吗?
解决方法 你真的不能对模拟器和NFC做任何有趣的事情.您不希望使用TAG_disCOVERED *** 作,因为这是最后的 *** 作.在真实设备上生成的意图不能像在NFCDemo演示中那样伪造.在2.3.3中更好地支持NFC之前,NFCDemo以2.3发布.这太糟糕了.也许未来会有一些选择,但现在我们都不得不购买具备NFC功能的NFC设备. 总结以上是内存溢出为你收集整理的android – NfcAdapter.getDefaultAdapter(this)在模拟器中返回null全部内容,希望文章能够帮你解决android – NfcAdapter.getDefaultAdapter(this)在模拟器中返回null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)