나는 단순히 태그의 내용을 읽으려고하고있다. 그러나 이것을 더 오래 연구할수록 이것이 어떻게 작동 하는지를 이해하지 못하는 유일한 느낌입니다. 나는 그것을 사용하기 전에, 내가 몇 가지 초기화 할 필요가 이제NFC를 사용하여 태그를 스캔하는 방법은 무엇입니까?
public String readTag(Tag tag) {
MifareUltralight mifare = MifareUltralight.get(tag);
try {
mifare.connect();
byte[] payload = mifare.readPages(4);
return new String(payload, Charset.forName("US-ASCII"));
} catch (IOException e) {
Log.e(TAG, "IOException while writing Scanner message...", e);
} finally {
if (mifare != null) {
try {
mifare.close();
}
catch (IOException e) {
Log.e(TAG, "Error closing tag...", e);
}
}
}
return null;
}
: 다음 developer.android.com 사이트를 통해 찾고
는, 나는 다음과 같은 방법을 찾은 다음
public void configScanner()
{
Intent intent = getIntent();
tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
try {
ndef.addDataType("*/*"); /* Handles all MIME based dispatches. You should specify only the ones that you need. */
}
catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] {ndef, };
techListsArray = new String[][] { new String[] { Ndef.class.getName() } };
}
를 내가 단순히 그것을 실행하려고합니다.
public void scanClick(View v)
{
textview1.setText(readTag(tag));
}
는 또한 일부 매니페스트에서 물건과 내 자신의 XML 파일을 추가 한
매니페스트 그러나
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/scan" />
scan.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
, 내가 실행 readTag(tag)
을 실행할 때 충돌이 발생합니다. 나는 그 물건을 작동시키는 법을 이해하지 못합니다. 로그 캣 출력 :
으로는 요구
04-11 17:13:07.650: E/AndroidRuntime(672): FATAL EXCEPTION: main
04-11 17:13:07.650: E/AndroidRuntime(672): java.lang.IllegalStateException: Could not execute method of the activity
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$1.onClick(View.java:3591)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View.performClick(View.java:4084)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$PerformClick.run(View.java:16966)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Handler.handleCallback(Handler.java:615)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.os.Looper.loop(Looper.java:137)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-11 17:13:07.650: E/AndroidRuntime(672): at dalvik.system.NativeStart.main(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): Caused by: java.lang.reflect.InvocationTargetException
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 17:13:07.650: E/AndroidRuntime(672): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 17:13:07.650: E/AndroidRuntime(672): at android.view.View$1.onClick(View.java:3586)
04-11 17:13:07.650: E/AndroidRuntime(672): ... 11 more
04-11 17:13:07.650: E/AndroidRuntime(672): Caused by: java.lang.NullPointerException
04-11 17:13:07.650: E/AndroidRuntime(672): at android.nfc.tech.MifareUltralight.get(MifareUltralight.java:94)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.example.android_test.MainActivity.readTag(MainActivity.java:108)
04-11 17:13:07.650: E/AndroidRuntime(672): at com.example.android_test.MainActivity.scanClick(MainActivity.java:155)
04-11 17:13:07.650: E/AndroidRuntime(672): ... 14 more
당신이 로그 캣 출력을 게시 할 수 당신이 추가? –
알았어요. 오류가 무엇이고 원인이 무엇인지 바로 말합니다. 이 특정 오류에 대해서는 거의 알지 못하므로 과 함께 InvokeTargetException –