2011-11-17 244 views
1

我對Android的代碼,返回我存儲在一個字符串array.I我字符串值工作返回填充微調使用字符串值要使用這個字符串數組來填充我的紗廠之一。通過SOAP的Web服務

我的主要意圖是,這取決於用戶在第一次微調選擇,我想用我的字符串數組來填充我的第二微調。

然而,當我嘗試我得到甚至當我填充適配器一個NullPointerException

我的Android代碼:

public class Mark2 extends Activity 
{ 
    private static final String SOAP_ACTION = "http://tempuri.org/getData"; 
    private static final String METHOD_NAME = "getData"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "http://10.0.2.2/getsubject/Service1.asmx"; 


    private Spinner s; 
    private Spinner s2; 
    private Spinner s3; 

@Override 
    protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.mainspinner); 

    s = (Spinner) findViewById(R.id.spinner1); 
    s2 = (Spinner) findViewById(R.id.spinner2); 


    String[] myList4=call(); 
    for(int i=0;i<myList4.length;i++) 
    System.out.println(myList4[i]); 

    String[] myList = new String[] {"First half","Second Half"}; 
    String[] myList2=new String[] { 
              "abc", 
            "ghf", 
            "ijk",        
            }; 

    String[] myList3=new String[] { 
          "john", 
          "ann", 
          "joe", 

            }; 

    s.setAdapter(new ArrayAdapter<String> (this,android.R.layout.simple_spinner_item, myList)); 
    s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2)); 


    final ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList4); 
    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3); 


    s.setOnItemSelectedListener(new OnItemSelectedListener() 
    { 

     @Override 
     public void onItemSelected(AdapterView<?> parent, View v,int pos, long id) 
     { 

     if(parent.getItemAtPosition(pos).toString().equalsIgnoreCase("First half")) 
     { 
      s2.setSelection(pos); 
      s3.setAdapter(adapter3); 
     } 
     else 
     { 
      s2.setAdapter(adapter2); 
      s2.setSelection(pos); 


     } 

     } 



     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 


     }}); 


} 

public String[] call() 
{ 
    SoapPrimitive responsesData = null; 

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( 
    SoapEnvelope.VER11); 
    envelope.dotNet = true; 
    envelope.setOutputSoapObject(request); 

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

    androidHttpTransport.debug = true; 

    try { 

    androidHttpTransport.call(SOAP_ACTION, envelope); 

    responsesData = (SoapPrimitive) envelope.getResponse(); 
    System.out.println(" --- response ---- " + responsesData); 

    } catch (SocketException ex) { 
    ex.printStackTrace(); 
    } catch (Exception e) { 
    e.printStackTrace(); 
    } 

    System.out.println(" ----" + responsesData); 

    String serviceResponse= responsesData .toString(); 


    String[] temp; 
    String delimiter = "#"; 
    temp= serviceResponse.split(delimiter); 
    System.out.println(" ---- length ---- " + temp.length); 

    return temp; 


    }  


    } 

我的logcat:

11-17 15:51:09.668: DEBUG/dalvikvm(125): GC_EXPLICIT freed 1220 objects/68272 bytes in 164ms 
11-17 16:20:29.649: ERROR/AndroidRuntime(620): java.lang.NullPointerException 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at com.example.Mark2$1.onItemSelected(Mark2.java:88) 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:864) 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at android.widget.AdapterView.access$200(AdapterView.java:42) 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:830) 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at android.os.Handler.handleCallback(Handler.java:587) 
11-17 16:20:29.649: ERROR/AndroidRuntime(620):  at android.os.Handler.dispatchMessage(Handler.java:92) 

編輯:行號:88是s3.setAdapter(一dapter3);

回答

1

NullPointerException當其中一個對象爲null並且您正嘗試使用它時發生。

從你的代碼,我發現,你沒有初始化的Spinner S3。

你剛纔忘了補充:

s3 = (Spinner) findViewById(R.id.spinner3); 
+0

喔狗屎男人!我怎麼想那個:(感謝您的幫助!! :-) –

+0

不要擔心它發生。 ;)..和這就是我們如何學會記住的東西.. –

+0

馬克當你的問題被解決爲接受的答案。 –