0

I've looked at a bunch of posts on this subject, but nothing seems to be working and I've been stuck on this problem for 2 days now. these lines of code are getting red underline on the "R" saying it cannot be resolved as a variable

    symbols = (EditText) findViewById(R.id.editText1);
    results = (TextView) findViewById(R.id.textView2);

    Button button = (Button) findViewById(R.id.button1);

Also this line of code at the bottom of the java file has the same error

       getMenuInflater().inflate(R.menu.activity_main, menu);

I created a menu folder under res and a menu.xml file as well. Here's all the code and a screenshot of my environment. I'm not sure which file you need to see so I provided them all just in case.

Screenshot of Environment http://postimg.org/image/6d1l0909r/

MainActivity.Java

package com.example.stocks;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.net.MalformedURLException;
import android.os.Handler;

public class MainActivity extends Activity implements Runnable {
    String symbolsStr = "";
    String resultsStr = "";
    EditText symbols = null;
    TextView results = null;

    final Handler mHandler = new Handler();
    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            results.setText(resultsStr);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        symbols = (EditText) findViewById(R.id.editText1);
        results = (TextView) findViewById(R.id.textView2);

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                symbolsStr = symbols.getText().toString();
                Thread thread = new Thread(MainActivity.this);
                thread.start();
            }
        });
    }

    @Override
    public void run() {
        try {
            resultsStr = GetQuotes(symbolsStr);
            mHandler.post(mUpdateResults);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private String GetQuotes(String symbols) throws MalformedURLException,
            IOException {
        StringBuilder response = new StringBuilder();

        // call web service to get results
        String urlStr = "http://download.finance.yahoo.com/d/quotes.csv?s="
                + symbols + "&f=nsl1op";
        URL url = new URL(urlStr);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    httpConn.getInputStream()), 8192);
            String strLine = null;
            while ((strLine = input.readLine()) != null) {
                response.append(strLine);
                response.append("\n");
            }
            input.close();
        }

        return response.toString();
    }

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

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="44dp"
        android:text="Enter Symbols separated by commas:" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="16dp"
        android:ems="10" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="44dp"
        android:text="Look Up" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:layout_marginTop="33dp"
        android:text="Results" />

</RelativeLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">StockQuotes</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

menu.xml

<?xml version="1.0" encoding="UTF-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:id="@+id/item1"></item>
        <item android:id="@+id/item2"></item>
        <item android:id="@+id/item3"></item>
    </menu>

android manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.stockquotes"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.stockquotes.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
gman plex
  • 39
  • 9
  • Here they are some hints for your problems: [http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable][1] [1]: http://stackoverflow.com/questions/7824730/r-cannot-be-resolved-to-a-variable – GioLaq Oct 25 '14 at 14:13

1 Answers1

4

Your packages mismatch:

In Activity: package com.example.stocks;

In Manifest: package="com.example.stockquotes"

Use 1 of them and rebuild your project.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • This fixed The R problem, but it caused this "getMenuInflater().inflate(R.menu.activity_main, menu);" to have an red error under activity_main" that says activity_main cannot be resolved or is not a field – gman plex Oct 25 '14 at 14:29
  • @gmanplex Because it should point to your actionBar layout not your activity's. It's probably located in `res/menu/menu.xml` (filename may differ). Then use `inflate(R.menu.menu.xml, menu);`. – Simas Oct 25 '14 at 14:35
  • Oh ok. One more thing. It now runs with no errors but when it launches it crashes on startup and I get RuntimeException and ClassNotFoundException? http://postimg.org/image/y5vx4k6hv/ – gman plex Oct 25 '14 at 15:01
  • @gmanplex Make sure to rename your package everywhere. Including this: `android:name="com.example.stockquotes.MainActivity"` in your manifest. – Simas Oct 25 '14 at 15:06