0

我正在尝试使用以下示例源代码将 NFC 功能添加到我的应用程序:

http://gitorious.org/0xdroid/development/blobs/150dd1693ff06a7683076eebe669277631ddf06/samples/NFCDemo/src/com/example/android/nfc/TagViewer.java

但是,当我这样做时,我会遇到几个“无法解析为类型”的错误。

...有人有什么建议吗?

附言

我有一种感觉,它源于这条线: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);

}

}
4

1 回答 1

0

看起来它在您的图书馆中找不到任何 nfc 记录。如果您使用的是 eclipse,请确保该库包含在您的项目中,并且在 java 构建屏幕的最右侧选项卡中,该库已被选中,并且位于您项目的 src 文件夹上方。如果您使用其他东西,请检查等效的类路径设置。

于 2013-04-01T05:19:26.193 回答