Search code examples
javaandroidandroid-activityandroid-tabhostforceclose

Force close when clicking a Spinner inside a new Activity.


I've created a TabHost with three tabs in it. If I click one of my tabs, an Activity with a Spinner will be opened. If I click that Spinner, my application force closes.

My Activity:

setContentView(R.layout.employeepayslip);
    btn=(Button)findViewById(R.id.button1);  //Submit

    Bundle extras = getIntent().getExtras();
    idviewslip = extras.getString("idviewslip");

     empid=(TextView)findViewById(R.id.empid1);
     empname=(TextView)findViewById(R.id.empname1);
     empdesignation=(TextView)findViewById(R.id.empdesignation);

    sp1=(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String>Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,months);
    sp1.setAdapter(Adapter);
    sp1.setOnItemSelectedListener(this);

    sp2=(Spinner) findViewById(R.id.spinner2);
    ArrayAdapter<String>Adapter1=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
    sp2.setAdapter(Adapter1);
    sp2.setOnItemSelectedListener(this);

Solution

  • I just replaced the following

    setContentView(R.layout.employeepayslip);
    

    to

    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.employeepayslip, null);
        this.setContentView(viewToLoad);