需要有关这种情况的更多信息

时间:2011-12-16 13:50:48

标签: android runtime-error toast

我的一些编码如下。第一个按钮(button2)用于退出,在我的模拟器和手机上正常工作但是(按钮1)应该从TextView接收输入并添加777然后显示为Toast消息在它们中的任何一个都不起作用。

public class TimerCodeActivity extends Activity {

protected TextView timebox;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timerlayout);

    timebox = (TextView) findViewById(R.id.timebox);

    Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener(){

        @Override    
        public void onClick(View v) {
            finish();
            System.exit(0);}
    });    

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){    

        @Override
        public void onClick(View v) {  
            String timeboxRepresentation = (String) timebox.getText();

             int Digit = Integer.parseInt(timeboxRepresentation.substring(0,2));
             int total = Digit + 777;

             Toast.makeText(TimerCodeActivity.this, String.valueOf(total), Toast.LENGTH_LONG).show();
        }
    });
}}

提前感谢你的帮助。

1 个答案:

答案 0 :(得分:1)

试试这个:

public class TimerCodeActivity extends Activity {

protected TextView timebox;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timerlayout);

    timebox = (TextView) findViewById(R.id.timebox);

    Button button2 = (Button) findViewById(R.id.button2);
    button2.setOnClickListener(new OnClickListener(){

        @Override    
        public void onClick(View v) {
            finish();
        }
    });    

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){    

        @Override
        public void onClick(View v) {
            // Use toString to get the TextView content  
            String timeboxRepresentation = timebox.getText().toString();

             int Digit = Integer.parseInt(timeboxRepresentation.substring(0,2));
             int total = Digit + 777;

             Toast.makeText(TimerCodeActivity.this, String.valueOf(total), Toast.LENGTH_LONG).show();
        }
    });
}}

然后删除

System.exit(0);

没用。