giving error:

Put your problem here if it does not fit any of the other categories.

giving error:

Postby akhilkumars » Fri Jun 25, 2010 1:27 pm

1)R.id.server_status cannot be resolved &
2)R.layout.server cannot be resolved



the code is:

package com.example.ServerActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;

import android.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;


public class ServerActivity extends Activity {
/** Called when the activity is first created. */

private TextView serverStatus;

// default ip
public static String SERVERIP = "10.0.2.15";

// designate a port
public static final int SERVERPORT = 8080;

private Handler handler = new Handler();

private ServerSocket serverSocket;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);

SERVERIP = getLocalIpAddress();

Thread fst = new Thread(new ServerThread());
fst.start();
}

public class ServerThread implements Runnable {

public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
// listen for incoming clients
Socket client = serverSocket.accept();
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Connected.");
}
});

try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
handler.post(new Runnable() {
@Override
public void run() {
// do whatever you want to the front end
// this is where you can be creative
}
});
}
break;
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Oops. Connection interrupted. Please reconnect your phones.");
}
});
e.printStackTrace();
}
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}

// gets the ip address of your phone's network
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); }
}
}
} catch (SocketException ex) {
Log.e("ServerActivity", ex.toString());
}
return null;
}

@Override
protected void onStop() {
super.onStop();
try {
// make sure you close the socket upon exiting
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
akhilkumars
Freshman
Freshman
 
Posts: 4
Joined: Fri Jun 25, 2010 1:09 pm

Top

Re: giving error:

Postby Benzebuth » Fri Jun 25, 2010 4:50 pm

do you have those two created somewhere in or as xml ?
Image
User avatar
Benzebuth
Freshman
Freshman
 
Posts: 6
Joined: Fri May 28, 2010 11:50 am

Re: giving error:

Postby nicholas.hauschild » Sat Jun 26, 2010 4:11 am

Delete the import that says 'import android.R;' and then import the R file that is in YOUR apps package.
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: giving error:

Postby akhilkumars » Sat Jun 26, 2010 7:02 am

I am new to android development...
Could you please tell me how to do it...

Thanks a lot
akhilkumars
Freshman
Freshman
 
Posts: 4
Joined: Fri Jun 25, 2010 1:09 pm

Re: giving error:

Postby nicholas.hauschild » Sat Jun 26, 2010 5:42 pm

Ummm.....I just did tell you....

There exists a line in your code that reads 'import android.R;'

Delete that line and replace it with 'import com.example.R;'

If that doesn't work, please let us know.
nicholas.hauschild
Master Developer
Master Developer
 
Posts: 310
Joined: Fri Dec 04, 2009 4:50 am

Re: giving error:

Postby akhilkumars » Tue Jun 29, 2010 11:44 am

i have sorted out the problem...was a small one...just copy pasted the code without making the UI.

Thank You very much for bearing me
akhilkumars
Freshman
Freshman
 
Posts: 4
Joined: Fri Jun 25, 2010 1:09 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests