我正在尝试使用以下示例源代码向我的应用添加NFC功能:
然而,当我这样做时,我得到了几个“无法解决的类型”错误。
......有人有什么建议吗?
P.S。
我有一种感觉,它来自于行import com.nfc.linked.record.ParsedNdefRecord;
但我不确定如何解决这个问题。
错误:
ParsedNdefRecord cannot be resolved to a type line 97 Java Problem
title_scanned_tag cannot be resolved or is not a field line 77 Java Problem
ParsedNdefRecord cannot be resolved to a type line 100 Java Problem
NdefMessageParser cannot be resolved line 97 Java Problem
The import com.nfc.linked.record cannot be resolved line 30 Java Problem
title cannot be resolved or is not a field line 51 Java Problem
list cannot be resolved or is not a field line 50 Java Problem
来源:
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.graphics.BitmapFactory;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.nfc.linked.record.ParsedNdefRecord;
import java.util.List;
public class AppActivity extends Activity implements OnClickListener {
static final String TAG = "ViewTag";
static final int ACTIVITY_TIMEOUT_MS = 1 * 1000;
private TextView mTitle;
private LinearLayout mTagContent;
private Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTagContent = (LinearLayout) findViewById(R.id.list);
mTitle = (TextView) findViewById(R.id.title);
resolveIntent(getIntent());
b=(Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
void resolveIntent(Intent intent) {
String action = intent.getAction();
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage[] msgs;
if (rawMsgs != null) {
msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
}
} else {
// Unknown tag type
byte[] empty = new byte[] {};
NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
msgs = new NdefMessage[] {msg};
}
setTitle(R.string.title_scanned_tag);
buildTagViews(msgs);
} else {
Log.e(TAG, "Unknown intent " + intent);
finish();
return;
}
}
void buildTagViews(NdefMessage[] msgs) {
if (msgs == null || msgs.length == 0) {
return;
}
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout content = mTagContent;
// Clear out any old views in the content area, for example if you scan
// two tags in a row.
content.removeAllViews();
// Parse the first message in the list
// Build views for all of the sub records
List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
final int size = records.size();
for (int i = 0; i < size; i++) {
ParsedNdefRecord record = records.get(i);
content.addView(record.getView(this, inflater, content, i));
inflater.inflate(R.layout.main, content, true);
}
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
resolveIntent(intent);
}
@Override
public void setTitle(CharSequence title) {
mTitle.setText(title);
}
@Override
public void onClick(View v) {
Intent intent = new Intent();
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new Notification.Builder(this)
.setTicker("Link Access Granted!")
.setContentTitle("Linking Manager")
.setContentText("Link Access Granted!")
.setSmallIcon(R.drawable.ic_stat_notify_template3)
.setContentIntent(pIntent).getNotification();
noti.flags=Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
}
答案 0 :(得分:0)
看起来它没有在你的库中找到nfc记录的任何内容。如果您正在使用eclipse,请确保该库包含在您的项目中,并且在java构建屏幕的最右侧选项卡中,将检查库以及项目的src文件夹。如果您正在使用其他内容,请检查等效的类路径设置。