2011-11-07 47 views
0

這是我收到的錯誤。如何從陣列中填充微調器

空指針異常 11-06 19:03:34.050:警告/ System.err的(5342):在RetrievingAmazonXMLDataActivity.onCreate(RetrievingAmazonXMLDataActivity.java:88)

for (int i = 0; i < tokens.length; i++) { 
       String a = dumpTitles("ProductName", i); 
       element = a.split("!"); 
       allProducts.add(element); 
      } 

      w = (String[][])allProducts.toArray(new String[allProducts.size()][]); 


    Spinner spinnerProducts = (Spinner) findViewById(R.id.spinner2); 
      spinnerProducts.setOnItemSelectedListener(this); 

// **** error line below 


    ArrayAdapter<String> productsArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, productArrayToShow); 

      productsArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
      spinnerProducts.setAdapter(productsArrayAdapter); 

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

      try { 
       selected = parent.getItemAtPosition(position).toString(); 


       productArrayToShow = w[position]; 

      } catch (Exception e) {} 
     } 

回答

0

你真的不說你的代碼中有什麼「productArrayToShow」,但我會假設它是你想在適配器中顯示的二維數組字符串。最簡單的方法就是製作一個1D陣列:

int rows = productArrayToShow.length; 
int cols = productArrayToShow[0].length; 
String[] productArray = new String[rows*cols]; 
int count = 0; 
for(int r=0; r<rows; ++r) 
    for(int c=0; c<cols; ++c) 
    productArray[count++] = productArrayToShow[r][c];