Click here to Skip to main content
15,868,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a UDP client modulerunning in java.My concern is to make that code run on android.To do that i have exported all the code to the MainActivity class and it doesnot show any errors.However wheb i run the code on emmulatore it says app stopped working.Below is the code:

Java
package com.example.com.hsn.controller.wifi.broadcastsender;
import java.net.*;
import java.io.*;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


	Button broadcast;
	TextView display;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		broadcast=(Button) findViewById(R.id.btnstart);
		display=(TextView) findViewById(R.id.tvdisplay);
		broadcast.setOnClickListener(new View.OnClickListener() {
			
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				DatagramSocket aSocket = null;
				//if (args.length < 3) {
				//	System.out.println("Usage: java UDPClient <message> <host name=""> <port number="">");
				//	System.exit(1);

				//}
				try {
					aSocket = new DatagramSocket();
					byte [] m = "Hello Server".getBytes();
					InetAddress aHost =  InetAddress.getByName("192.168.0.255"); 
					int serverPort = 8888;
					DatagramPacket request =
						new DatagramPacket(m, 50, aHost, serverPort);
				aSocket.setBroadcast(true);
					aSocket.send(request);
					
					byte[] buffer = new byte[50];
					DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
					aSocket.receive(reply);
				System.out.println("Reply:" + new String(reply.getData()));
					display.setText("Reply:" + new String(reply.getData()));
				}
				catch (SocketException e) {
					System.out.println("Socket:" + e.getMessage());
			}
				catch (IOException e) {
					System.out.println("IO:" + e.getMessage());
				}
				finally {
					if (aSocket != null)
						aSocket.close();
				}
				
				
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}



Any help would really be appreciated.
Thanks in advance
Posted
Updated 5-Nov-14 1:52am
v2
Comments
Richard MacCutchan 5-Nov-14 7:51am    
You need to look at the logcat messages in the IDE (eclipse or Android Studio) to see what the system is complaining about.

1 solution

Do as Richard MacCutchan suggests. Odds are it's a null exception.

In the absence of logcat output my guess is that the view hasn't been inflated and one or both of broadcast or display are null. You don't check these for null after your findViewByID calls. Might be a good idea to do so.

If that is the problem you could try moving your code to the onStart override for the activity. By that point the view(s) should be in place.

Java
@Override
protected void onStart() {
  super.onStart();
  // Move code from onCreate() to here.
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900