无法正常使用Android菜单

时间:2014-10-23 18:38:48

标签: android

我一直收到错误"菜单无法解析或不是字段。" 还有一条消息说R无法解析或不是字段。" 我不明白我缺少什么,有什么想法吗? (菜单代码位于发布代码的底部)

     package com.example.airportinfo;

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;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.airportinfo.R;


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 = GetAirportInfo(symbolsStr);
            mHandler.post(mUpdateResults);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

        // call web service to get results
        String urlStr = "http://services.faa.gov/airport/status/" + symbols + "?format=application/json";
        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();
        }

        String result = ProcessJSON(response.toString());

        return result;
    }

    private String ProcessJSON (String json) throws IOException, JSONException
    {
        String result = "";
        JSONObject responseObject = new JSONObject(json);
        result += "Airport: " + responseObject.getString("name") + "\n";
        result += "City: " + responseObject.getString("city") + ", " + responseObject.getString("state") + "\n";

        JSONObject statusObject = responseObject.getJSONObject("status");
        result += "Status: " + statusObject.getString("reason") + "\n";

        JSONObject weatherObject = responseObject.getJSONObject("weather");
        result += "Weather: " + weatherObject.getString("weather") + ", " +
                weatherObject.getString("temp") + "\n";
        result += "Wind: " + weatherObject.getString("wind") + "\n";
        result += "Visibility: " + weatherObject.getString("visibility") + " Miles\n";

        JSONObject metaObject = weatherObject.getJSONObject("meta");
        result += "Updated: " + metaObject.getString("updated") + "\n";

        return result;
    }

    @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;
    }

}

的strings.xml

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

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

</resources>

1 个答案:

答案 0 :(得分:1)

尝试删除import com.example.airportinfo.R并清理项目。

更新:

试试这个语法:

@Override
public boolean     onCreateOptionsMenu(Menu menu) {
  super.createOnOptionsMenu(menu);
  MenuInflater inflater =     getMenuInflater();
  inflater.inflate(R.menu.YOURMENU,     menu);
  return true;
}

如果错误在那里,我建议您珍贵地检查您的xml文件。