2016-02-19 218 views
-2

我正在練習如何將字符串添加到微調器的加法器。如果我在java活動申報串,完美的作品,但如果我在string.xml做:字符串資源

<string-array name="tabs"> 
    <item>tab</item> 
    <item>tab1</item> 
    <item>tab2</item> 
    <item>tab3</item> 
    <item>tab4</item> 
    <item>tab5</item> 
</string-array> 

,並在java

ArrayAdapter<String> adapterr1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, R.array.tabs); 

不工作。

有什麼問題?由於

這是完整的代碼,因爲我有問題,白衣應用程序(崩潰)

public class MainActivity extends AppCompatActivity { 

    Spinner OptionSpinner; 
    TextView textview; 


    String[] stabs= getResources().getStringArray(R.array.tabs); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner); 
     textview = (TextView) findViewById(R.id.textview); 

     ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs); 

     adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     OptionSpinner.setAdapter(adapter1); 

     OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       textview.setText("seleccionado: " + parent.getItemAtPosition(position)); 
       if(position==1){ 
        Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class); 
        startActivity(IntentActT1); 
       } 
      } 

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

      } 
     }); 


    } 
} 
+0

數組適配器類將列表作爲其最終參數。不是資源整數。 –

+0

請參考http://stackoverflow.com/questions/10532907/android-retrieve-string-array-from-resources –

回答

0
String[] stabs= getResources().getStringArray(R.array.tabs); 
ArrayAdapter adapterr1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs); 

編輯了1

public class MainActivity extends AppCompatActivity { 

Spinner OptionSpinner; 
TextView textview; 
String[] stabs; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    OptionSpinner = (Spinner) findViewById(R.id.OptionSpinner); 
    textview = (TextView) findViewById(R.id.textview); 
    stabs= getResources().getStringArray(R.array.tabs); 

    ArrayAdapter adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stabs); 

    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    OptionSpinner.setAdapter(adapter1); 

    OptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
      textview.setText("seleccionado: " + parent.getItemAtPosition(position)); 
      if(position==1){ 
       Intent IntentActT1 = new Intent(MainActivity.this, ActTab1.class); 
       startActivity(IntentActT1); 
      } 
     } 

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

     } 
    }); 


} 
} 
+0

我想這個,但應用程序崩潰 – Ariel

+0

發佈您的完整代碼 – sasikumar

+0

好的,謝謝我做到了 – Ariel

0

構造ArrayAdapter<String>(Context, int, int)不使用作爲數組資源ID的第三個參數;它使用它作爲TextView的ID用於顯示佈局中的每個項目。相反,您可以通過getStringArray(R.array.tabs)作爲第三個參數(或getContext().getStringArray(R.array.tabs),如果此代碼不在Activity中)。