I keep getting a fatal error saying nullpointer exception on String action = intent.getAction();
How do I get it to work ? I want to transfer a text file to another device using Android Beam. The beam detects it and launches the 'tap to beam' on the phone but never appears to complete the beam.
@TargetApi(16)
public void sendNfcFile(){
nfcAdapter = NfcAdapter.getDefaultAdapter(this);
if(!nfcAdapter.isEnabled()){
Toast.makeText(this, "Please enbable NFC in settings", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
}else if(!nfcAdapter.isNdefPushEnabled()){
Toast.makeText(this, "Please enable Android Beam", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
}else{
String filename = "personalShare.txt";
File fileToTransfer = Environment.getExternalStoragePublicDirectory(filename);
fileToTransfer.setReadable(true, false);
nfcAdapter.setBeamPushUris(new Uri[]{Uri.fromFile(fileToTransfer)}, this);
}
}
@Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
String action = intent.getAction();
if(action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED)){
Parcelable[] parcelables =
intent.getParcelableArrayExtra(
NfcAdapter.EXTRA_NDEF_MESSAGES);
NdefMessage inNdefMessage = (NdefMessage)parcelables[0];
NdefRecord[] inNdefRecords = inNdefMessage.getRecords();
NdefRecord NdefRecord_0 = inNdefRecords[0];
String inMsg = new String(NdefRecord_0.getPayload());
TextView name = (TextView) findViewById(R.id.fullname);
name.setText(inMsg);
}
}