-3

What causes NullPointerException in this code..........? and gives unfortunately has stopped

ArrayAdapter spinneradapter = ArrayAdapter.createFromResource(this,
            R.array.Repeating, android.R.layout.simple_spinner_item);
    spinner.setAdapter(spinneradapter);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub

                TextView myText = (TextView) view;
                Toast.makeText(CreateActivity.this,
                        "you selected" + myText.getText(), 1000).show();

        }
cHao
  • 84,970
  • 20
  • 145
  • 172
rrr
  • 1
  • 3
  • If you have a NPE, you have a stack trace, right? That'd tell you exactly where it was triggered. – cHao Dec 04 '14 at 23:52
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Dec 04 '14 at 23:57
  • i think the problem is that this spinner is defined in xml that differ from the xml of activity where it is (it is defined in activity that connect with xml that differ from spinner xml) ,, could this Cause null pointer exception?? and how to connect spinner view with it's xml ?? – rrr Dec 05 '14 at 13:15

1 Answers1

-1

I think the problem is with this line.I cant pin point anything without the logs but give this a shot

TextView myText = (TextView) view;

is not referencing any layout element

Try

TextView myText = (TextView) findViewById(R.id.view);//where view is your textview
archon92
  • 447
  • 3
  • 13