简体   繁体   中英

App crashes on orientation change in android

My application crashes when I flip orientation. If the commented out line in onCreate runs I can't flip orientation at all. without it I can flip but can't change my background image but when it flips back it still crashes.

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private DrawerLayout mDrawerLayout;
    private ListView mListView;
    private ImageView bgimage;
    int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
    private ActionBarDrawerToggle toggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(savedInstanceState != null){
            Toast.makeText(this, "made it", Toast.LENGTH_SHORT).show();
            int n = savedInstanceState.getInt("imageId");
            Toast.makeText(this, ""+n, Toast.LENGTH_SHORT).show();
            bgimage = (ImageView) findViewById(R.id.imgOne);
            int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
            Toast.makeText(this, ""+drwables.length, Toast.LENGTH_SHORT).show();
            //This code here
            //bgimage.setImageResource(drwables[n]);
        }
        else{
            bgimage = (ImageView) findViewById(R.id.imgOne);
        }
            setContentView(R.layout.activity_main);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            mListView = (ListView) findViewById(R.id.left_drawer);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

            String[] names = getResources().getStringArray(R.array.bandImages);
            ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, R.layout.nav_list_row, R.id.textView, names);
            mListView.setAdapter(itemsAdapter);
            mListView.setOnItemClickListener(this);


            toggle = new ActionBarDrawerToggle(this, mDrawerLayout, (Toolbar) findViewById(R.id.toolbar), R.string.open, R.string.closed) {
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(getTitle());
                    invalidateOptionsMenu();

                }

                public void onDrawerOpened(View view) {
                    super.onDrawerOpened(view);
                    getSupportActionBar().setTitle(getTitle());
                    invalidateOptionsMenu();
                }
            };

            mDrawerLayout.addDrawerListener(toggle);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            toggle.syncState();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        int n = Integer.parseInt(bgimage.getTag().toString());
        savedInstanceState.putInt("imageId", n);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mListView);
        menu.findItem(R.id.action_about).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        bgimage = (ImageView) findViewById(R.id.imgOne);
        bgimage.setImageResource(drwables[position]);
        bgimage.setTag(position);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        int id = item.getItemId();
        if(R.id.action_about == id){
            Toast.makeText(this, "Lab 2 Spring 2016, Zack G Johnson", Toast.LENGTH_SHORT).show();
            return true;
        }
        else{
            return super.onOptionsItemSelected(item);
        }
    }


}

Try to place setContentView method before finding and using layout elements.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //put it here
    setContentView(R.layout.activity_main);

    if(savedInstanceState != null){
        Toast.makeText(this, "made it", Toast.LENGTH_SHORT).show();
        int n = savedInstanceState.getInt("imageId");
        Toast.makeText(this, ""+n, Toast.LENGTH_SHORT).show();
        bgimage = (ImageView) findViewById(R.id.imgOne);
        int[] drwables = {R.drawable.cindy, R.drawable.fred, R.drawable.kate, R.drawable.keith, R.drawable.matt, R.drawable.rickey};
        Toast.makeText(this, ""+drwables.length, Toast.LENGTH_SHORT).show();
        //This code here
        //bgimage.setImageResource(drwables[n]);
    }
    else{
        bgimage = (ImageView) findViewById(R.id.imgOne);
    }

    ...

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