我禁用或/和启用按钮时Android应用程序崩溃

时间:2014-04-12 17:48:11

标签: java android


MainActivity:

    public class MainActivity extends Activity  implements OnKeyboardActionListener{
    public int start = 0;
private View view;
static Button submit;
final Activity activity = this;
public int stop;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment()).commit();
    }

    //EditText editText = (EditText) findViewById(R.id.result);
    submit = (Button) activity.findViewById(R.id.submit);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/ **  *包含简单视图的占位符片段。 * /

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }


    public void submitNumber(final View view){

        if(stop == 0){
            level lev= new level(0) ;
            operation opreration = new operation();
            int sum= 1;
            TextView score = (TextView) findViewById(R.id.score);
            TextView number2 = (TextView) findViewById(R.id.randNumber2);
            TextView number = (TextView) findViewById(R.id.randNumber);
            TextView opr = (TextView) findViewById(R.id.opr);

            int num1 = Integer.parseInt(number.getText().toString());
            int num2 = Integer.parseInt(number2.getText().toString());

            EditText editText = (EditText) findViewById(R.id.result);
            editText.setOnKeyListener(new OnKeyListener(){
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (keyCode==KeyEvent.KEYCODE_ENTER) {
                        submitNumber(view);
                    }

                    return false;

                }
            });
            int resu = 0;
            try 
            {
                resu = Integer.parseInt(editText.getText().toString().trim());
            } catch (NumberFormatException e) {

            }
            boolean bob = operation.opr(num1,num2,resu, opr.getText().toString().trim());
            //System.out.println(bob);
            if (bob) {      
                try {                   
                    sum = Integer.parseInt(score.getText().toString()) +1;
                    score.setText(""+ sum );
                } catch (NumberFormatException e) {

                }
            }



            int levelSum =  lev.level(sum);
            //System.out.println(sum + "   " + levelSum);
            Random rnd = new Random();
            int rand1 = rnd.nextInt(levelSum);
            int rand2 = rnd.nextInt(levelSum);
            String op = lev.operation(sum);

            opr.setText(""+ op);
            number2.setText("" + rand1);
            number.setText("" + rand2);

            editText.setText("");
            System.out.println(start);
            if (start == 0){
                submit.setEnabled(false);
                //new Thread(new Runnable() {
                    //public void run() {
                        final TextView time = (TextView) findViewById(R.id.timer);
                        timer t= new timer();
                        submit.setEnabled(false);   
                        t.timer(time);
                        start =0;

                    }
                //}).start();}

            }
    }

    @Override
    public void onKey(int arg0, int[] arg1) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPress(int arg0) {
        // TODO Auto-generated method stub
        View view = null;

        switch (arg0)
        {
        case KeyEvent.KEYCODE_ENTER:
            submitNumber(view);
        }

    }

    @Override
    public void onRelease(int arg0) {
        View view = null;

        switch (arg0)
        {
        case KeyEvent.KEYCODE_ENTER:
            submitNumber(view);
        }

    }

    @Override
    public void onText(CharSequence arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void swipeDown() {
        // TODO Auto-generated method stub

    }

    @Override
    public void swipeLeft() {
        // TODO Auto-generated method stub

    }

    @Override
    public void swipeRight() {
        // TODO Auto-generated method stub

    }

    @Override
    public void swipeUp() {
        // TODO Auto-generated method stub

    }



}

定时器:

package com.example.sum;

import android.os.CountDownTimer;
import android.widget.TextView;

public class timer {

    void timer(final TextView time){
        new CountDownTimer(3000, 1000) {

             public void onTick(long millisUntilFinished) {
                 time.setText("" + millisUntilFinished / 1000);
             }

             public void onFinish() {
                 stop stop = new stop();
                 stop.stop();
                 time.setText("done!");

             }
          }.start();
    }




}

package com.example.sum;

import android.app.Activity;
import android.widget.Button;

public class stop extends MainActivity {

    void stop() {
        // TODO Auto-generated constructor stub
        System.out.println(start);
        //submit.setEnabled(true);
        stop =0;
        //submit.setText("NG ");
        start = 0;
    }

}

0 个答案:

没有答案