onDiscoveryStarted Nullpointer异常

时间:2014-11-25 12:32:44

标签: android udp multicast

我正在尝试使用pocmo中的Android-Network-Intents库 - > here the link

看起来不错,但我遇到了 nullpointer异常

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)

已获得互联网许可。

这是我的代码(基于他们的示例项目)

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");
            }
        }
    };
}

1 个答案:

答案 0 :(得分:2)

您的txtFeedback为空。致电findViewById后,在onCreate上设置setContentView。您可能需要再次检查xml以检查您是否使用了正确的ID:@InjectView(R.id.txtFeedback)