当我打开它时,我的应用程序一直崩溃

时间:2016-04-20 11:13:38

标签: java android compiler-errors

我在我的板球得分应用程序中添加了2个微调器,并且没有编译器错误,但是我的应用程序在启动时一直崩溃。当我添加第一个微调器时工作正常,但是当我添加第二个微调器时,应用程序开始崩溃。请帮忙。

package com.example.visha.cricketcounter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private Spinner spinner;
    private Spinner spinner2;

    EditText text = (EditText)findViewById(R.id.editText);
    String value = text.getText().toString();
    EditText text2 = (EditText)findViewById(R.id.editText2);
    String value2 = text2.getText().toString();
    private static final String[]paths = {"15", "20", "30"};
    private  final String[]paths1 = {value, value2};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        spinner = (Spinner)findViewById(R.id.spinner);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item,paths);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(this);

        spinner2 = (Spinner)findViewById(R.id.spinner2);
        ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_spinner_item,paths1);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(adapter2);
        spinner2.setOnItemSelectedListener(this);

    }

@Override
    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

        switch (position) {
            case 0:
                // Whatever you want to happen when the first item gets selected
                break;
            case 1:
                // Whatever you want to happen when the second item gets selected
                break;
            case 2:
                // Whatever you want to happen when the thrid item gets selected
                break;

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

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

3 个答案:

答案 0 :(得分:3)

您应该实现OnItemSelectedListener来访问onItemSelected方法。

答案 1 :(得分:1)

你在this之类的方法中传递setOnItemSelectedListener(this)意味着你将侦听器的引用作为类传递,但你没有实现监听器,impalement OnItemSelectedListener如下所示并尝试,

public class MainActivity extends AppCompatActivity implements  AdapterView.OnItemSelectedListener

答案 2 :(得分:1)

您的活动必须实施界面onItemSelectedListener并覆盖方法OnNothingSelected

实施例

public class MainActivity extends AppCompatActivity implements onItemSelectedListener{ 
....

然后使用主活动中的鼠标光标让IDE自动插入所有缺少的方法实现