2014-06-16 62 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

相关问题