2013-11-07 7 views
0

내가 변환 통화이 안드로이드 코드를 작성하고, 나는 점점 오전 NullPointerException이 :NullPointerException이 안드로이드

이 내 로그 캣 오류가 줄에서 73

public class Currency_convert extends Activity 
{ 
    public int to; 
    public int from; 
    public String [] val; 
    public String s; 
    public Handler handler; 
    public double am=0.0; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Spinner s1 = (Spinner) findViewById(R.id.spinner11); 
     Spinner s2 = (Spinner) findViewById(R.id.spinner22); 
     final EditText e=(EditText) findViewById(R.id.amountt); 
     // am=Double.parseDouble(e.getText().toString()); 
     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, R.array.name, android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice); 
     val = getResources().getStringArray(R.array.value); 
     s1.setAdapter(adapter); 
     s2.setAdapter(adapter); 
     s1.setOnItemSelectedListener(new spinOne(1)); 
     s2.setOnItemSelectedListener(new spinOne(2)); 
     Button b = (Button) findViewById(R.id.button11); 
     b.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       TextView t = (TextView) findViewById(R.id.textView44); 
       if(from == to) 
       { 
        Toast.makeText(getApplicationContext(), "Invalid", 4000).show(); 
       } 
       else 
       {          
         try { 
         s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");      
        //s=getJson("http://www.google.com/ig/calculator?hl=en&q=1USD=?INR"); 
          JSONObject jObj; 
         jObj = new JSONObject(s); 
         String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 
          am=Double.parseDouble(e.getText().toString()); 
         double totalR=(Double.parseDouble(exResult))*am; 
         String r=String.valueOf(totalR); 
         t.setText(r); 
         } catch (JSONException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         catch (ClientProtocolException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         }              
        }           
       }        
     }); 
    } 
    public String getJson(String url)throws ClientProtocolException, IOException { 

     StringBuilder build = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     HttpResponse response = client.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 
     InputStream content = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
     String con; 
     while ((con = reader.readLine()) != null) { 
        build.append(con); 
       } 
     return build.toString(); 
    } 
    private class spinOne implements OnItemSelectedListener 
    { 
     int ide; 
     spinOne(int i) 
     { 
      ide =i; 
     } 
     public void onItemSelected(AdapterView<?> parent, View view, 
       int index, long id) { 
      if(ide == 1) 
       from = index; 
      else if(ide == 2) 
       to = index; 

     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 
     } 

    } 
} 

오류가 실행되는 것을 코드라고한다 이 줄의 시간 오류 :

s2.setAdapter(adapter); 

나는 무엇이 잘못 되었는가!

11-07 10:25:22.194: E/AndroidRuntime(2004): FATAL EXCEPTION: main 
11-07 10:25:22.194: E/AndroidRuntime(2004): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dashboard_our/com.example.dashboard_our.Currency_convert}: java.lang.NullPointerException 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.os.Looper.loop(Looper.java:137) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at java.lang.reflect.Method.invoke(Method.java:525) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at dalvik.system.NativeStart.main(Native Method) 
11-07 10:25:22.194: E/AndroidRuntime(2004): Caused by: java.lang.NullPointerException 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at com.example.dashboard_our.Currency_convert.onCreate(Currency_convert.java:73) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.Activity.performCreate(Activity.java:5133) 
11-07 10:25:22.194: E/AndroidRuntime(2004):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
+3

id가 spinner11 및 spinner22 인 두 개의 스피너가 main.xml에 있습니까? – Blackbelt

+0

당신은 logcat을 게시 할 수 있습니까? – Emmanuel

+0

@blackbelt 예 할부 –

답변

1

findViewById(R.id.spinner22)null을 반환 것 같다 : 여기

는 로그 캣입니다. XML 레이아웃에 spinner22라는 ID를 가진 객체가 없을 때 발생합니다.

1

이 줄은 실패 :

 Spinner s2 = (Spinner) findViewById(R.id.spinner22); 

그것은 로그 캣없이 볼 힘들지만, 진단 할 수있는 다음과 같은 일을 볼 수 있습니다/문제를 해결할 :

  1. spinner22가 유효한 ID가 아닌 귀하의 레이아웃에
  2. 해당 id가있는 항목은 회전자가 아닙니다
  3. 프로젝트를 청소 했습니까?
  4. 실패한 줄에 중단 점이있는 디버거를 통해 실행하고 어떤 일이 발생하는지 확인하십시오.
관련 문제