2014-06-16 105 views
0

的下拉列表中我有一個微調器,文本字段和一個按鈕。點擊該按鈕後,必須將在texfield中輸入的文本添加到微調器中。我用ArrayList來添加文本。但是,點擊按鈕時,應用程序崩潰。另外兩個spinners只是虛擬的。我需要將文本字段中的值添加到android

的活動

public class MainActivity extends ActionBarActivity { 
    Spinner spinner1,spinner2,spinner3; 
    Button add; 
    EditText subject; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    // Spinner element 
     spinner1 = (Spinner) findViewById(R.id.spinner1); 
     spinner2 = (Spinner) findViewById(R.id.spinner2); 
     spinner3 = (Spinner) findViewById(R.id.spinner3); 
     add = (Button) findViewById(R.id.button1); 
     subject = (EditText) findViewById(R.id.editText1); 

    // Spinner click listener 

     //spinner2.setOnItemSelectedListener(this); 
     //spinner3.setOnItemSelectedListener(this); 
     spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> adapter, View v, 
        int position, long id) { 
       // On selecting a spinner item 
       String item = adapter.getItemAtPosition(position).toString(); 

       // Showing selected spinner item 
       Toast.makeText(getApplicationContext(), 
         "Selected Subject : " + item, Toast.LENGTH_LONG).show(); 
      } 

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

      } 
     }); 
     add.setOnClickListener(new OnClickListener() { 


     Context context; 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String content = subject.getText().toString(); 
      List list = new ArrayList(); 
      list.add(content); 
      ArrayAdapter dataAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item, list); 

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

       spinner1.setAdapter(dataAdapter); 


     } 
     }); 



     } 
    public void addListenerOnSpinnerItemSelection() { 
     spinner1 = (Spinner) findViewById(R.id.spinner1); 
     spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
       //do something here 
      } 
      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
       //optionally do something here 
      } 
     }); 
     } 



    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
      return rootView; 
     } 
    } 

} 

的XML如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Spinner 
     android:id="@+id/spinner1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <Spinner 
     android:id="@+id/spinner2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <Spinner 
     android:id="@+id/spinner3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <CheckedTextView 
     android:id="@+id/checkedTextView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="attended all classes??" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="169dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text="" > 
    </EditText> 
    <Button 
     android:id="@+id/button1" 
     android:layout_width="169dp" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:text="Add" 


     /> 

</LinearLayout> 
+1

感謝您的提問。如果您可以在錯誤上發佈您的Logcat堆棧跟蹤,那麼我們可以給您一個更準確的答案 – drees

+0

'context'爲'null'。你可以使用'v.getContext()' – codeMagic

回答

0

您申報方面,而不是實例化它,因此它是空。如果你看到的NullPointerException在你的logcat嘗試以下

ArrayAdapter dataAdapter = new ArrayAdapter(context,android.R.layout.simple_spinner_item, list); 

因此,與

ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item, list); 
+0

謝謝你解決它。 – user3359953

0

取代它作爲一個評論說,你應該總是與你的問題,當你的應用程序崩潰發佈logcat的。在這裏,看起來問題是contextnull。你在這裏聲明

Context context; 

但你永遠不會初始化它。但是無論如何,無需考慮這個額外的變量,因爲您可以在onClick()內部獲得從View點擊v.getContext()

ArrayAdapter dataAdapter = new ArrayAdapter(v.getContext(), 
     android.R.layout.simple_spinner_item, list); 

See the docs

返回視圖中運行的背景下,通過它可以訪問當前的主題,資源等

有用的鏈接

What is a NullPointerException

Unfortunately my app has stopped working

相關問題