Question

hi this is my java file ......i m developing a nfc writer application.....here down is my java code file

package com.example.nfc_tag_writer;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import java.nio.charset.Charset;

public class SendTextViaNFCActivity extends Activity implements CreateNdefMessageCallback, OnNdefPushCompleteCallback {

    private static final int MESSAGE_SENT = 1;
    String CURSOR_HERE;
    String SAVED_TEXT;
    String beamText;
    private final Handler mHandler;
    NfcAdapter mNfcAdapter;
    EditText myText;
class AnonymousClass_1 extends Handler {
    final /* synthetic */ SendTextViaNFCActivity this$0;

    AnonymousClass_1(SendTextViaNFCActivity r1_SendTextViaNFCActivity) {
        super();
        this$0 = r1_SendTextViaNFCActivity;
    }

    public void handleMessage(Message msg) {
        switch(msg.what) {
        case MESSAGE_SENT:
            Toast.makeText(this$0.getApplicationContext(), "Message sent!", MESSAGE_SENT).show();
        }
    }
}


public SendTextViaNFCActivity() {
    super();
    SAVED_TEXT = "SAVED_TEXT";
    CURSOR_HERE = "###CURSOR_HERE###";
    beamText = "";
    mHandler = new AnonymousClass_1(this);
}

public NdefRecord createMimeRecord(String mimeType, byte[] payload) {
    return new NdefRecord((short) 2, mimeType.getBytes(Charset.forName("US-ASCII")), new byte[0], payload);
}

public NdefMessage createNdefMessage(NfcEvent event) {
    NdefRecord[] r2_NdefRecord_A = new NdefRecord[2];
    r2_NdefRecord_A[0] = createMimeRecord("application/com.example.android.beam", myText.getText().toString().getBytes());
    r2_NdefRecord_A[1] = NdefRecord.createApplicationRecord("diewland.nfc.text");
    return new NdefMessage(r2_NdefRecord_A);
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send_text_via_nfc);
    myText = (EditText) findViewById(R.id.my_text);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null) {
        myText.setText("NFC is not available on this device.");
        myText.setEnabled(false);
    }
    mNfcAdapter.setNdefPushMessageCallback(this, this, new Activity[0]);
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this, new Activity[0]);
}

public boolean onCreateOptionsMenu(Menu menu) {
    if (mNfcAdapter == null) {
        return super.onCreateOptionsMenu(menu);
    } else {
        getMenuInflater().inflate(R.menu.options, menu);
        return true;
    }
}

public void onNdefPushComplete(NfcEvent arg0) {
    mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
}

public void onNewIntent(Intent intent) {
    setIntent(intent);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.menu_settings:
        startActivity(new Intent("android.settings.NFCSHARING_SETTINGS"));
        return true;
    case R.id.menu_reset:
        myText.setText("");
        return true;
    case R.id.menu_share:
        if (!"".equals(myText.getText().toString())) {
            Intent sendIntent = new Intent();
            sendIntent.setAction("android.intent.action.SEND");
            sendIntent.putExtra("android.intent.extra.TEXT", myText.getText().toString());
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, "Share via"));
            return true;
        } else {
            return true;
        }
    }
    return super.onOptionsItemSelected(item);
}

protected void onPause() {
    super.onPause();
    myText.getText().insert(myText.getSelectionStart(), CURSOR_HERE);
    Editor editor = getPreferences(0).edit();
    editor.putString(SAVED_TEXT, myText.getText().toString());
    editor.commit();
}

public void onResume() {
    super.onResume();
    Intent intent = getIntent();
    String type = intent.getType();
    if (!"android.intent.action.SEND".equals(intent.getAction()) || type == null) {
        if ("android.nfc.action.NDEF_DISCOVERED".equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
        String restoredText = getPreferences(0).getString(SAVED_TEXT, null);
        if (restoredText != null) {
            myText.setText(restoredText.replaceAll(CURSOR_HERE, beamText));
        }
    } else if ("text/plain".equals(type)) {
        myText.setText(intent.getStringExtra("android.intent.extra.TEXT"));
    }
    myText.setSelection(myText.getText().length());
}

void processIntent(Intent intent) {
    beamText = new String(((NdefMessage) intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES")[0]).getRecords()[0].getPayload());
}

}

i m getting followings errorss

03-19 08:09:56.989: V/NFC(767): this device does not have NFC support
03-19 08:09:56.999: D/AndroidRuntime(767): Shutting down VM
03-19 08:09:56.999: W/dalvikvm(767): threadid=1: thread exiting with uncaught exception (group=0x414c4700)
03-19 08:09:57.029: E/AndroidRuntime(767): FATAL EXCEPTION: main
03-19 08:09:57.029: E/AndroidRuntime(767): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nfc_tag_writer/com.example.nfc_tag_writer.SendTextViaNFCActivity}: java.lang.NullPointerException
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.os.Looper.loop(Looper.java:137)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread.main(ActivityThread.java:5103)
03-19 08:09:57.029: E/AndroidRuntime(767):  at java.lang.reflect.Method.invokeNative(Native Method)
03-19 08:09:57.029: E/AndroidRuntime(767):  at java.lang.reflect.Method.invoke(Method.java:525)
03-19 08:09:57.029: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-19 08:09:57.029: E/AndroidRuntime(767):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-19 08:09:57.029: E/AndroidRuntime(767):  at dalvik.system.NativeStart.main(Native Method)
03-19 08:09:57.029: E/AndroidRuntime(767): Caused by: java.lang.NullPointerException
03-19 08:09:57.029: E/AndroidRuntime(767):  at com.example.nfc_tag_writer.SendTextViaNFCActivity.onCreate(SendTextViaNFCActivity.java:75)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.Activity.performCreate(Activity.java:5133)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-19 08:09:57.029: E/AndroidRuntime(767):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-19 08:09:57.029: E/AndroidRuntime(767):  ... 11 more

please can anyone help me i m getting the my errors in logcat...i m unable to fix them.....the app is unfornatelly stopping r crashing

Was it helpful?

Solution

Your checking that the mNfcAdapter is not null, yet still try to access it afterward even if it is. Try moving these lines

mNfcAdapter.setNdefPushMessageCallback(this, this, new Activity[0]);
mNfcAdapter.setOnNdefPushCompleteCallback(this, this, new Activity[0]);

into an else to the previous if:

if (mNfcAdapter == null) {
    myText.setText("NFC is not available on this device.");
    myText.setEnabled(false);
} else {
    mNfcAdapter.setNdefPushMessageCallback(this, this, new Activity[0]);
    mNfcAdapter.setOnNdefPushCompleteCallback(this, this, new Activity[0]);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top