Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By Tolls

Thread: Cannot resolve symbol...

  1. #1
    jblaven is offline Member
    Join Date
    Mar 2014
    Posts
    9
    Rep Power
    0

    Question Cannot resolve symbol...

    Hi guys,

    I'm working through a JSON tutorial in Android Studio and I am getting the error: cannot resolve symbol JSONTask on line 45. I'm fairly confident I've followed the code, but am unsure why my code doesn't behave as in the video.

    I think my error originates on line 50, which gives the error: Modifier 'public' not allowed here. Did I place this class in the wrong location, and is it also causing the other error?

    Here is a link to the tutorial video: https://www.youtube.com/watch?v=_7r_vdwmW0o

    Here is my code:

    Java Code:
    package com.example.joelavender.jsonparsingdemo;
    
    import android.net.Uri;
    import android.os.AsyncTask;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.google.android.gms.appindexing.Action;
    import com.google.android.gms.appindexing.AppIndex;
    import com.google.android.gms.common.api.GoogleApiClient;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.HttpURLConnection;
    
    public class MainActivity extends AppCompatActivity {
    
        /**
         * ATTENTION: This was auto-generated to implement the App Indexing API.
         * See https://g.co/AppIndexing/AndroidStudio for more information.
         */
        private GoogleApiClient client;
        private TextView tvData;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button btnHit = (Button) findViewById(R.id.btnHit);
            tvData = (TextView) findViewById(R.id.tvJsonItem);
    
            //This if (btnHit != null) {} was the last thing added.  Remove if needed and try other suggestion given by app
            if (btnHit != null) {
                btnHit.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        new JSONTask().execute("\"http://jsonparsing.parseapp.com/jsonData/moviesDemoItem.txt\"");
                    }
                });
            }
    
            public class JSONTask extends AsyncTask<String,String, String >{
    
    
                @Override
                protected String doInBackground(String... params) {
                    HttpURLConnection connection = null;
                    BufferedReader reader = null;
    
                    try {
                        URL url = new URL(params[0]);
                        connection = (HttpURLConnection) url.openConnection();
                        // Make connection with url
                        connection.connect();
    
                        //Store data from InputStream into stream
                        InputStream stream = connection.getInputStream();
    
                        reader = new BufferedReader(new InputStreamReader(stream));
    
                        StringBuffer buffer = new StringBuffer();
    
                        String line ="";
                        while ((line = reader.readLine()) != null){
                            buffer.append(line);
                        }
    
                        return buffer.toString();
    
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (connection != null) {
                            connection.disconnect();
                        }
                        try {
                            if(reader != null) {
                                reader.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    return null;
                }
    
                @Override
                protected void onPostExecute(String result) {
                    super.onPostExecute(result);
                    tvData.setText(result);
    
                }
            }
    
    
    
    
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        }
    
        @Override
        public void onStart() {
            super.onStart();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            client.connect();
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Main Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app URL is correct.
                    Uri.parse("android-app://com.example.joelavender.jsonparsingdemo/http/host/path")
            );
            AppIndex.AppIndexApi.start(client, viewAction);
        }
    
        @Override
        public void onStop() {
            super.onStop();
    
            // ATTENTION: This was auto-generated to implement the App Indexing API.
            // See https://g.co/AppIndexing/AndroidStudio for more information.
            Action viewAction = Action.newAction(
                    Action.TYPE_VIEW, // TODO: choose an action type.
                    "Main Page", // TODO: Define a title for the content shown.
                    // TODO: If you have web page content that matches this app activity's content,
                    // make sure this auto-generated web page URL is correct.
                    // Otherwise, set the URL to null.
                    Uri.parse("http://host/path"),
                    // TODO: Make sure this auto-generated app URL is correct.
                    Uri.parse("android-app://com.example.joelavender.jsonparsingdemo/http/host/path")
            );
            AppIndex.AppIndexApi.end(client, viewAction);
            client.disconnect();
        }
    }
    Any point in the right direction would be a great help!

  2. #2
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Cannot resolve symbol...

    You are trying to define the class JSONTask inside method onCreate(). This is invalid Java syntax.

    I expect you intended to create an inner class called JSONTask, which you can do by defining the class inside the MainActivity class, but outside of any methods.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Cannot resolve symbol...

    Defining a class inside the body of a method is allowed; it makes it a local class (local to that method); you can't make it a public class though; local variables can never be public.

    kind regards,

    Jos
    trcooke likes this.
    Build a wall around Donald Trump; I'll pay for it.

  4. #4
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Cannot resolve symbol...

    Well I never....

    Java Code:
    class Test {
        public static void main(String argv[]) {
            class InMethod {
                public String sayHello() { return "Hello"; }
            }
            InMethod inMethod = new InMethod();
            System.out.println(inMethod.sayHello());
        }
    }
    Bash Code:
    $ java Test
    Hello

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot resolve symbol...

    It's the same mechanism that allows anonymous inner classes, I believe.
    So you'd have a Test$InMethod.class as your compiler output from that, whereas an anonymous one would be Test$1.class.

    Actually, a quick check and it's Test$1InMethod.class...so yes, I'd say that the same mechanism.
    trcooke likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Cannot resolve symbol...

    And to continue with your example, if you ever want to get an instance to the
    class object within the method, have it implement an interface.

    Java Code:
    interface Response {
       public String sayHello();
    }
    public class Test {
       public static void main(String argv[]) {
          Test t = new Test();
          Response r = t.getInstance();
          System.out.println(r.sayHello());
       }
    
       public Response getInstance() {
          class InMethod implements Response {
             public String sayHello() {
                return "Hello";
             }
          }
          return new InMethod();
       }
    }
    Of course, if you are going to do that you may as well make it an inner or nested class or some other option
    to provide increased visibility.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Cannot resolve symbol...

    Interesting examples, but I'm not seeing a scenario where you might use this over a regular private inner class?

    Unless your class needed access to a variable scoped to the method only and not the class. e.g.
    Java Code:
    class Test {
        public static void main(String argv[]) {
            String world = "World";
            class InMethod {
                public String sayHello() { return "Hello, " + world; }
            }
            InMethod inMethod = new InMethod();
            System.out.println(inMethod.sayHello());
        }
    }
    Bash Code:
    $ java Test
    Hello, World
    But even then, it's a rare case where it's desired or even useful.

    I might have to put it down as an odd side effect of how inner classes, which are useful, are implemented. Much like default methods on an interface, just because you can doesn't mean you should.

  8. #8
    trcooke is offline Tim Cooke
    Join Date
    Jul 2014
    Location
    Belfast
    Posts
    101
    Rep Power
    0

    Default Re: Cannot resolve symbol...

    But getting back to the OP's question.

    Either:
    1. The JSONTask class belongs outside of any method, so move it outside of any method.
    2. The JSONTask class belongs inside the method but must not have a scope modifier, so delete the scope modifier.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Cannot resolve symbol...

    Quote Originally Posted by trcooke View Post
    Interesting examples, but I'm not seeing a scenario where you might use this over a regular private inner class?
    I've never seen it used.
    I only encountered it myself for the first time a few weeks ago on Code Ranch.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. How to resolve : Cannot find symbol....?
    By Kareng in forum New To Java
    Replies: 5
    Last Post: 10-08-2012, 06:20 PM
  2. Cannot resolve symbol error
    By name in forum New To Java
    Replies: 4
    Last Post: 12-07-2011, 05:35 PM
  3. Yet another Cannot Resolve Symbol
    By Gabinka in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 10:28 PM
  4. Cannot Resolve Symbol error...
    By seral1969 in forum New To Java
    Replies: 4
    Last Post: 02-18-2008, 12:17 AM
  5. Error: cannot resolve symbol, help me
    By mathias in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 08-06-2007, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •