1

我正在尝试使用以下示例将多条记录添加到一条记录中,NDefMessage但是当我尝试这样做时 - 我收到一条错误消息,指出“该getBytes()类型的方法未定义TextView

示例: 示例

资源:

public class ViewCountry extends Activity implements CreateNdefMessageCallback,
            OnNdefPushCompleteCallback {
    NfcAdapter mNfcAdapter;
   // TextView timeTv;
       private static final int MESSAGE_SENT = 1;
       private long rowID;
       private TextView nameTv;
       private TextView capTv;
       private TextView codeTv; 
       private TextView timeTv; 

       @Override
       public void onCreate(Bundle savedInstanceState) 
       {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.view_country);

          setUpViews();
          Bundle extras = getIntent().getExtras();
          rowID = extras.getLong(CountryList.ROW_ID); 
       }

       private void setUpViews() {
           nameTv = (TextView) findViewById(R.id.nameText);
           capTv = (TextView) findViewById(R.id.capText);
           timeTv = (TextView) findViewById(R.id.timeEdit);
           codeTv = (TextView) findViewById(R.id.codeText);

          nameTv = (TextView) findViewById(R.id.nameText);
            // Check for available NFC Adapter
            mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
            if (mNfcAdapter == null) {
                nameTv = (TextView) findViewById(R.id.nameText);
                nameTv.setText("NFC is not available on this device.");
                timeTv = (TextView) findViewById(R.id.timeEdit);
                timeTv.setText("NFC is not available on this device.");
            } else {
                // Register callback to set NDEF message
                mNfcAdapter.setNdefPushMessageCallback(this, this);
                // Register callback to listen for message-sent success
                mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
            }
        }


       @Override
        public NdefMessage createNdefMessage(NfcEvent event) {
           String message1 = nameTv.getText().toString();
            String message2 = timeTv.getText().toString();
            byte[] textBytes1 = nameTv.getBytes();
            byte[] textBytes2 = timeTv.getBytes();
            NdefRecord textRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
                    message1.getBytes(), new byte[]{}, textBytes1);
            NdefRecord textRecord2 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, 
                    message2.getBytes(), new byte[]{}, textBytes2);


           //   String text = ("SSID");

            NdefMessage msg = new NdefMessage(
                    new NdefRecord[]{
                            textRecord1,
                            textRecord2,                
                            NdefRecord.createApplicationRecord("com.nfc.linked")


                    }
                     */
                     //,NdefRecord.createApplicationRecord("com.nfc.linked")
                   );
                   return msg;
               }


        /**
         * Implementation for the OnNdefPushCompleteCallback interface
         */
        @Override
        public void onNdefPushComplete(NfcEvent arg0) {
            // A handler is needed to send messages to the activity when this
            // callback occurs, because it happens from a binder thread
            mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
        }



        /** This handler receives a message from onNdefPushComplete */
        private final Handler mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case MESSAGE_SENT:
                    Toast.makeText(getApplicationContext(), "Core Device Rules Sent!", Toast.LENGTH_LONG).show();
                    break;
                }
            }
        };



        @Override
        public void onNewIntent(Intent intent) {
            // onResume gets called after this to handle the intent
            setIntent(intent);
        }



        /**
         * Parses the NDEF Message from the intent and prints to the TextView
         */
        void processIntent(Intent intent) {
            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                    NfcAdapter.EXTRA_NDEF_MESSAGES);
            // only one message sent during the beam
            NdefMessage msg = (NdefMessage) rawMsgs[0];
            // record 0 contains the MIME type, record 1 is the AAR, if present
            nameTv.setText(new String(msg.getRecords()[0].getPayload()));
        }




        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // If NFC is not available, we won't be needing this menu
            if (mNfcAdapter == null) {
                return super.onCreateOptionsMenu(menu);
            }
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.options, menu);
            return true;
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case R.id.menu_settings:
                    Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
                    startActivity(intent);
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }



        }




       @Override
       protected void onResume()
       {
          super.onResume();
          new LoadContacts().execute(rowID);
       } 

       private class LoadContacts extends AsyncTask<Long, Object, Cursor> 
       {
          DatabaseConnector dbConnector = new DatabaseConnector(ViewCountry.this);

          @Override
          protected Cursor doInBackground(Long... params)
          {
             dbConnector.open();
             return dbConnector.getOneContact(params[0]);
          } 

          @Override
          protected void onPostExecute(Cursor result)
          {
             super.onPostExecute(result);

             result.moveToFirst();
             // get the column index for each data item
             int nameIndex = result.getColumnIndex("name");
             int capIndex = result.getColumnIndex("cap");
             int codeIndex = result.getColumnIndex("code");
             int timeIndex = result.getColumnIndex("time");

             nameTv.setText(result.getString(nameIndex));
             capTv.setText(result.getString(capIndex));
             timeTv.setText(result.getString(timeIndex)); 
             codeTv.setText(result.getString(codeIndex));

             result.close();
             dbConnector.close();
          }
       } 


       private void deleteContact()
       {

          AlertDialog.Builder alert = new AlertDialog.Builder(ViewCountry.this);

          alert.setTitle(R.string.confirmTitle); 
          alert.setMessage(R.string.confirmMessage); 

          alert.setPositiveButton(R.string.delete_btn,
             new DialogInterface.OnClickListener()
             {
                public void onClick(DialogInterface dialog, int button)
                {
                   final DatabaseConnector dbConnector = 
                      new DatabaseConnector(ViewCountry.this);

                   AsyncTask<Long, Object, Object> deleteTask =
                      new AsyncTask<Long, Object, Object>()
                      {
                         @Override
                         protected Object doInBackground(Long... params)
                         {
                            dbConnector.deleteContact(params[0]); 
                            return null;
                         } 

                         @Override
                         protected void onPostExecute(Object result)
                         {
                            finish(); 
                         }
                      };

                   deleteTask.execute(new Long[] { rowID });               
                }
             }
          );

          alert.setNegativeButton(R.string.cancel_btn, null).show();
       }

    }
4

2 回答 2

2

对于 TextView 类型,方法 getBytes() 未定义

只是 TextView(as object) 不提供getBytes()方法。但是,如果要将 TextView 的内容转换为字节数组,则需要先获取 TextView 的内容,然后可以使用 getBytes()

byte[] arr = nameTv.getText().toString().getBytes();

注意:如果要将整个对象转换为字节数组,可以使用

例子:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream writter = new ObjectOutputStream(baos);
writter.writeObject(<yourObject>);
byte[] arr = baos.toByteArray();
于 2013-04-01T11:58:00.087 回答
1

那是因为 的对象TextView没有getBytes()方法。

在您拥有的代码中nameTv.getBytes(),尝试将其替换为message1.getBytes(). 看起来这就是您可能想要的。

于 2013-04-01T12:00:40.350 回答