简体   繁体   中英

Calling an id from another XML layout on android using LayoutInflater not working

Well in my case i have 2 XML Layouts so far,activity_main.xml and reserve.xml. I want to call the ImageButton id from reserve.xml to be executed from the MainActivity that uses my activity_main.xml layout.

I am trying to change layouts on a click from the action bar like activity_main.xml to reserve.xml and vice versa. Because i don't want to use intents. So i'm setting the view on onOptionsItemSelected function.

So because of this i tried to use LayoutInflater And View to call the id,but its giving me a nullPointerException error.

/ THIS IS WHERE I SET THE VIEW OR CHANGE MY LAYOUTS /

   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.
        switch (item.getItemId()) {

            case R.id.home:
             //Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_LONG).show();
                inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.activity_main, null);
                this.setContentView(view);
            return true;

            case R.id.reserve:
                 //Toast.makeText(getApplicationContext(), "Reservation", Toast.LENGTH_LONG).show();
                //setContentView(R.layout.reserve);
                inflater =(LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = inflater.inflate(R.layout.reserve, null);
                this.setContentView(view);
            return true;


        }
        return super.onOptionsItemSelected(item);
    }

/ THIS IS WHERE I CALL THE ID AND THE HIGHLIGHTED LINE GIVES ME THE ERROR /

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

    ActionBar bar = getActionBar(); // or MainActivity.getInstance().getActionBar()
    //bar.setBackgroundDrawable(new ColorDrawable(0x000));
    bar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
    bar.setDisplayShowTitleEnabled(true);


        date = (EditText) view.findViewById(R.id.date);
        datepicker = (ImageButton) view.findViewById(R.id.imageButton1);

        Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
        /** Listener for click event of the button */
        datepicker.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
              //showDialog(DATE_DIALOG_ID);
                Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();
            }
        });

        /** Get the current date */
        final Calendar cal = Calendar.getInstance();
        pYear = cal.get(Calendar.YEAR);
        pMonth = cal.get(Calendar.MONTH);
        pDay = cal.get(Calendar.DAY_OF_MONTH);

        /** Display the current date in the TextView */
        updateDisplay();

}

I just want to ask if is there any possible way i can do this if this is my idea of changing pages on my app?

Thanks! :D

you have to call findViewById on the view which have been inflated for example

final View view = inflater.inflate(R.layout.reverse, null);
TextView one = (TextView)view.findViewById(R.id.WHATEVER);

I am not sure if you can swipe Activities like that. Why not use Intent and save yourself from all that trouble or use Tabs instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM