2014-10-23 2 views
0

"메뉴를 해결할 수 없거나 필드가 아닙니다."라는 오류가 계속 나타납니다. 또한 R가 해결 될 수 없다고하거나 필드가 아닌 메시지가있다. " 내가제대로 작동하려면 Android 메뉴를 가져올 수 없습니다.

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

} 

(메뉴 코드가 게시 된 코드 하단에)? 내가 어떤 아이디어를 놓친 거지 이해하지 않는다 프로젝트를 수입 com.example.airportinfo.R를 제거하고 청소

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

[R은 해결할 수 없습니다 - 안드로이드 오류] (http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) – Simas

+0

Project-> clean (on Eclipse) –

답변

1

시도를 strings.xml의

UPDATE :.

시도를 이 구문은 다음과 같습니다.

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

오류가있는 경우 xml 파일을 소중히 확인하는 것이 좋습니다.

+0

해당 코드 줄을 제거하면 "R은 변수로 해결할 수 없습니다"라는 메시지가 나타나고 android 또는 com.example.airportinfo에서 가져올 것인지 묻습니다. – dfasfsd

+0

업데이트 된 답변을 참조하십시오. – Sini

관련 문제