TomCB
TomCB

Reputation: 4063

onDiscoveryStarted Nullpointer exception

I'm trying the Android-Network-Intents library from pocmo -->here the link

It looks great, but I'm running into a nullpointer exception:

11-25 13:28:37.744    7618-7634/be.appwise.networkintents E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-684
Process: be.appwise.networkintents, PID: 7618
java.lang.NullPointerException
                at be.appwise.networkintents.MainActivity$1.onDiscoveryStarted(MainActivity.java:88)
                at com.androidzeitgeist.ani.discovery.DiscoveryThread.run(DiscoveryThread.java:66)

Internet permission is in place.

This is my code (based on their sample project)

public class MainActivity extends ActionBarActivity {
    private static final String TAG = "MainActivity";
    private static final String EXTRA_MESSAGE = "message";

    Discovery discovery;
    boolean discoveryStarted;

    @InjectView(R.id.txtInput)
    EditText txtInput;

    @InjectView(R.id.txtReceived)
    TextView txtReceived;

    @InjectView(R.id.txtFeedback)
    TextView txtFeedback;

    @OnClick(R.id.btnSend)
    public void send(View view) {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_MESSAGE, "Testing Network intents");
        Transmitter transmitter = new Transmitter();
        try {
            transmitter.transmit(intent);
        } catch (TransmitterException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        discovery = new Discovery();
        discovery.setDisoveryListener(discoveryListener);
    }

    @Override
    protected void onResume() {
        super.onResume();

        //Enable discovery
        try {
            discovery.enable();
            discoveryStarted = true;
        } catch (DiscoveryException e) {
            discoveryStarted = false;
            e.printStackTrace();
            txtFeedback.setText("error discovery " + e.getMessage());
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        //Disable discovery
        if (discoveryStarted) {
            discovery.disable();
        }
    }

    DiscoveryListener discoveryListener = new DiscoveryListener() {

        @Override
        public void onDiscoveryStarted() {
            txtFeedback.setText("onDiscoveryStarted ");
        }

        @Override
        public void onDiscoveryStopped() {
            txtFeedback.setText("onDiscoveryStopped ");
        }

        @Override
        public void onDiscoveryError(Exception e) {
            txtFeedback.setText("onDiscoveryError ");
        }

        @Override
        public void onIntentDiscovered(InetAddress inetAddress, Intent intent) {
            if (intent.hasExtra(EXTRA_MESSAGE)) {
                txtFeedback.setText("onIntentDiscovered ");
                String message = intent.getStringExtra(EXTRA_MESSAGE) + " from " + inetAddress;
                txtReceived.setText(message);
            } else {
                txtFeedback.setText("Intent discovered, no message");
            }
        }
    };
}

Upvotes: 0

Views: 82

Answers (1)

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

Your txtFeedback is null. Set it with findViewById on your onCreate after you call setContentView. You might want to check the xml again to check if you're using the right id on: @InjectView(R.id.txtFeedback)

Upvotes: 2

Related Questions