新手android 开发 异常集锦(持续更新中)

新手android 开发 错误集锦(持续更新中)?出现问题查出错误出在上面代码中intent传回来的值有可能是null,就

新手android 开发 错误集锦(持续更新中)

?出现问题查出错误出在上面代码中intent传回来的值有可能是null,就会产生转换的错误,最终修改方案是加入异常处理机制,就是使用try and catch。修改后程序运行正常。代码如下:

public class otheractivity extends Activity{

private TextView textview2 = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_other);

?

textview2 = (TextView)findViewById(R.id.TextView2);

?

int oneint = 0;

int twoint = 0;?

Intent intent2 = getIntent();

String onestr = intent2.getStringExtra("one");

String twostr = intent2.getStringExtra("two");

try{

oneint = Integer.parseInt(onestr);

}

catch(Exception e){}

try{

twoint = Integer.parseInt(twostr);

}

catch (Exception e) {

// TODO: handle exception

}

int res = oneint + twoint;

String resstr = String.valueOf(res);

textview2.setText(resstr);

?

}

?

}

问题就迎刃而解了。

?

?