5 Replies - 13749 Views - Last Post: 05 April 2012 - 03:17 PM

#1 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Cannot find symbol (for R classes and/or variables)?

Posted 01 April 2012 - 07:04 PM

Hi. I'm really really new to programming in Android with Java (I'm using JMonkey, since its pretty much like NetBeans with OpenGL features that I use for other projects), with four days of trying to program since I started on Wednesday. I'm really interested in this tutorial I read here http://www.android10...id-applications (or here http://thinkandroid....-applications/), to try to make some sort of chatting program. I've successfully installed the android SDK(here http://developer.and...sdk/index.html), created an emulator (a process similar to this one http://androidandme....-the-emulator/), and set the IDE to properly run the programs (http://kenai.com/projects/nbandroid/pages/Install). So I pasted the code given in the first links I wrote in this question, and still have some errors even when I build the project (I read somewhere I can'
t remember that doing so fixes the initial error of the compiler not recognizing the R class):

The ServerActivity class:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
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;

public class ServerActivity extends Activity {
 
    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;
 
    @Override
    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();
         }
    }
 
}



and, the ClientActivity class:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.EditText;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

public class ClientActivity extends Activity {
 
    private EditText serverIp;
 
    private Button connectPhones;
 
    private String serverIpAddress = "";
 
    private boolean connected = false;
 
    private Handler handler = new Handler();
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client);
 
        serverIp = (EditText) findViewById(R.id.server_ip);
        connectPhones = (Button) findViewById(R.id.connect_phones);
        connectPhones.setonclickListener(connectListener);
    }
 
    private onclickListener connectListener = new onclickListener() {
 
        @Override
        public void onclick(View v) {
            if (!connected) {
                serverIpAddress = serverIp.getText().toString();
                if (!serverIpAddress.equals("")) {
                    Thread cThread = new Thread(new ClientThread());
                    cThread.start();
                }
            }
        }
        
    };
 
    public class ClientThread implements Runnable {
 
        public void run() {
            try {
                InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                Log.d("ClientActivity", "C: Connecting...");
                Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
                connected = true;
                while (connected) {
                    try {
                        Log.d("ClientActivity", "C: Sending command.");
                        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                    .getOutputStream())), true);
                            // where you issue the commands
                            out.println("Hey Server!");
                            Log.d("ClientActivity", "C: Sent.");
                    } catch (Exception e) {
                        Log.e("ClientActivity", "S: Error", e);
                    }
                }
                socket.close();
                Log.d("ClientActivity", "C: Closed.");
            } catch (Exception e) {
                Log.e("ClientActivity", "C: Error", e);
                connected = false;
            }
        }
    }
}



The errors the compiler specifies are:

For the ServerActivity class:
error: cannot find symbol
        setContentView(R.layout.server);
  symbol:   variable server
  location: class layout

//At:

setContentView(R.layout.server);

error: cannot find symbol
        serverStatus = (TextView) findViewById(R.id.server_status);
  symbol:   variable id
  location: class R

//At:

serverStatus = (TextView) findViewById(R.id.server_status);



For the ClientActivity class:
error: cannot find symbol
        setContentView(R.layout.client);
  symbol:   variable client
  location: class layout

//At: 

setContentView(R.layout.client);

error: cannot find symbol
        serverIp = (EditText) findViewById(R.id.server_ip);
  symbol:   variable id
  location: class R

//At:

serverIp = (EditText) findViewById(R.id.server_ip);

error: cannot find symbol
        connectPhones = (Button) findViewById(R.id.connect_phones);
  symbol:   variable id
  location: class R

//At:

connectPhones = (Button) findViewById(R.id.connect_phones);



And then I get some errors that I believe happen because I'm not writing something that goes against the rules of the programming for android, in the build.xml file:

The following error occurred while executing this line:

<do-only-if-manifest-hasCode elseText="hasCode = false. Skipping...">

Compile failed; see the compiler error output for details.

//At this line in build.xml:

fork="${need.javac.fork}">



I read around, that it's necessary to declare new variables using classes and objects that are to be in the R class to also be declared inside an xml file, which I haven't clear if it is the manifest or main or strings file. I've tried looking for some kind of tutorial that explains how to properly make mention of these new variables in the xml, but haven't found anything significant that is simple to understand. I mean, it's almost obvious that, for the errors I mentioned above, the variables missing in class layout should be written in the main.xml file since it is inside the layout folder, but what about those variables in class R (namely, variable id)? Should they be written in the manifest file? And what should I write? Please please let me know how I can solve these errors to be able to run this program and make some sense of it. Any help as always is greatly appreciated =)

By the way, I wrote the two classes specified in the tutorial(s) inside a single android project. Is that correct? If not, and I should put them into separate projects, how should I fix this line in the ClientActivity class?

Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT); //Since SERVERPORT is taken from the ServerActivity class



Maybe these errors are easy to fix, thereby making me a noob at this. But since I really love Java and have programmed a fair amount of time in it, I thought I'd give android a try =)

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot find symbol (for R classes and/or variables)?

#2 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Re: Cannot find symbol (for R classes and/or variables)?

Posted 04 April 2012 - 12:50 AM

I'm not terribly experienced with Android programming, but I suggest trying to use the eclipse IDE. This will give you the advantage of seeing your errors before compile time, and quite possibly fixing your problem. The class R is automatically generated by the ADT plugin for eclipse.

So since you're not using eclipse or as far as I can tell any sort of android plugin to the IDE you are using, R would not be automatically generated. Furthermore R refers to a layout xml file which you will also have to create. (But the plugin does a huge portion of that work for you too)

https://developer.an...installing.html
^ Definitely your best bet.

Edit:
I partially recant the above, it appears the instructions you followed should set you up with the ADT plugin. However from what you reported it doesn't seem to work, so I'd say switch to eclipse. But if someone knows a better way, feel free.

This post has been edited by wd40bomber7: 04 April 2012 - 12:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 gfcf14  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 90
  • Joined: 19-January 11

Re: Cannot find symbol (for R classes and/or variables)?

Posted 04 April 2012 - 08:07 AM

I tried that, and it didn't work. Maybe because I'm not used to use the Eclipse IDE, but I did everything the link indicates, and still while generating the variables asked for in the compiler (which NetBeans can do as well), upon running the program they were erased because of 'being entered manually' . But out of 70 people you were the only one who replied trying to help, so thank you =)
Was This Post Helpful? 0
  • +
  • -

#4 wd40bomber7  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 87
  • Joined: 22-November 09

Re: Cannot find symbol (for R classes and/or variables)?

Posted 04 April 2012 - 10:35 AM

Ok from your code it looks like you need two layout files, one named server and one named client and some resource (a textview in one/both layouts) called serverIp,connect_phones, and others.

I've attached an image of what you're project in eclipse should look like. (I don't use netbeans so I cant help you there, sorry)

Your layout xml files should be next to/instead of "main.xml" in my image. While R.java should be exactly where it is for me. it is completely generated. It is not generated when you press compile, it is actually generated in real time whenever you edit layouts/resource files. Dont mess with it or try to manually replace it.

Is this how things look for you?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#5 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Cannot find symbol (for R classes and/or variables)?

Posted 04 April 2012 - 08:12 PM

Okey, i what i'm getting out if this, is that your problem is that it for some reason do not generate the code that should be inside the R.java, right?

If that the case,i can tell you that i have the same problem right now.
When i start a new project or clean a project, it dose not auto-generate the R.java-content.
I don't know why, but i do know that i always solve it by making my own R.java-file and set the class to "final"
public final class R {



}

Sometimes that works, other times i have to re-do this a couple of times, or even restart Eclipse.
But after some tries it work again.

also, as i mention, i use Eclipse and I've not used any of the other options out there, so i can't help you with that.

Hope this helps

Cheers
Was This Post Helpful? 0
  • +
  • -

#6 slehmann101  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 146
  • Joined: 30-November 11

Re: Cannot find symbol (for R classes and/or variables)?

Posted 05 April 2012 - 03:17 PM

Are any xml files capitalized?
are any resources capitalized?
Can you recompile your code?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1