Android应用在尝试启动新活动时不断崩溃

时间:2015-07-02 10:45:06

标签: android android-activity

不幸的是,我一直在撞墙,试图找出为什么每次按下"显示"时应用程序崩溃的原因。按钮。正如您所看到的,当按下按钮时会调用一个意图,随后应该更改"更改"到GraphicDisplay活动。任何帮助表示赞赏。

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);
    }
}

1 个答案:

答案 0 :(得分:1)

我想我看到了你的问题。你在里面调用显示ClickListener:

  String colorSchemeVal = popupMenu.toString();

但你永远不会初始化&#34; popupMenu&#34;,除非你点击&#34; colorScheme&#34;。

相关问题