Deus Ex Machina
Deus Ex Machina

Reputation: 393

Android App Keeps Crashing When Attempting to Start New Activity

Unfortunately I have been banging my head against the wall for quite some time trying to figure out why the app crashes every time I press the "Display" button. As you can see an intent is called when the button is pressed, which should subsequently "change" to the GraphicDisplay Activity. Any help is appreciated.

package com.dwolford.app7;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupMenu;
import android.widget.Spinner;


public class Main extends Activity {

    Button quit;
    Button display;
    String word;
    EditText wordEntry;
    PopupMenu popupMenu;
    Button colorScheme;
    Spinner spin;
    Integer[] items = new Integer[51];
    int spinnerValue = 90;//The current value of the spinner which is selected by the user


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int spinnerValues = 50;



        spin = (Spinner) findViewById(R.id.spinner);
        for(int i = 0; i < 51; i++)//Populate spinner with values from 50 to 100
        {
            items[i] = spinnerValues;
            spinnerValues++;
        }
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, items);
        spin.setAdapter(adapter);
        spin.setSelection(40);
        spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        //Creating popup menu on button click
        colorScheme = (Button)findViewById(R.id.colorScheme);
        colorScheme.setOnClickListener(new View.OnClickListener() {
            @Override
            @SuppressLint("NewApi")
            public void onClick(View v) {

                popupMenu = new PopupMenu(Main.this, colorScheme);
                popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
                popupMenu.show();
            }
        });

        wordEntry = (EditText)findViewById(R.id.word);

        display = (Button)findViewById(R.id.display);
        display.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                word = wordEntry.getText().toString();//Get word String

                Intent intent = new Intent(Main.this, GraphicDisplay.class);

                int scale = (Integer)spin.getSelectedItem();
                String colorSchemeVal = popupMenu.toString();
                //Pass Word, scale, and colorscheme using intent
                intent.putExtra("colorSchemeVal", colorSchemeVal);//Pass current value for color scheme
                intent.putExtra("scale", scale);//Pass scale to Graphic Activity
                intent.putExtra("word", word);//Pass word to Graphic Activity via intent
                startActivity(intent);
            }
        });

        quit = (Button)findViewById(R.id.quit);
        quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @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 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Upvotes: 0

Views: 102

Answers (1)

yshahak
yshahak

Reputation: 5096

I think I see your problem. You call inside display ClickListener:

  String colorSchemeVal = popupMenu.toString();

but you never initialize "popupMenu", unless you hit "colorScheme".

Upvotes: 1

Related Questions