MenuInflater不工作R无法解析为变量

时间:2014-10-25 14:08:21

标签: java android xml

我已经看了很多关于这个主题的帖子,但似乎没有任何工作,我已经被困在这个问题上2天了。这些代码行在“R”上变为红色下划线,表示无法将其解析为变量

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

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

此外,java文件底部的这行代码具有相同的错误

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

我在res下创建了一个菜单文件夹和一个menu.xml文件。 这是我的环境的所有代码和屏幕截图。我不确定你需要看哪个文件所以我提供了所有文件以防万一。

环境截图 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清单

<?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>

1 个答案:

答案 0 :(得分:4)

您的包裹不匹配:

在活动中:package com.example.stocks;

在清单中: package="com.example.stockquotes"

使用其中一个并重建您的项目。