作业

1. NullPointerException
  原因: 调用对象的方法/属性,担对象为null
  2. ClassCastException
  原因: 类型转换异常
3. ActivityNotFoundException: 
原因: 没有在清单文件中注册Activity,或注册不正确
基本常见异常的一般分析步骤:
1.在LogCat中从下向上找,尽量找到Caused by(会显示由哪种异常导致的)
2. 找出异常的类及行号,上级进入对应的行 

public class SimpleComponentActivity extends Activity {

private TextView tv_simple_msg;
private EditText et_simple_num;
private Button btn_simple_submit;
private ImageView iv_dimple_play;
private CheckBox cbx_simple_basketball;
private CheckBox cbx_simple_football;
private CheckBox cbx_simple_pingpong;
private RadioGroup rg_simple_sex;
private Boolean status = true;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_simple_component);


tv_simple_msg = (TextView) findViewById(R.id.tv_simple_msg);
tv_simple_msg.setText("今天天气不错");
// tv_simple_msg.setText(R.string.hello_world);


et_simple_num = (EditText) findViewById(R.id.et_simple_num);
et_simple_num.setText("12356789");


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


@Override
public void onClick(View v) {
//得到内容
String number = et_simple_num.getText().toString().trim();
//提示
Toast.makeText(SimpleComponentActivity.this, number,
Toast.LENGTH_SHORT).show();
}
});


iv_dimple_play = (ImageView) findViewById(R.id.iv_simple_play);
iv_dimple_play.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
if(status){
//设置背景图片
// iv_dimple_play.setBackgroundResource(R.drawable.ic_launcher);
iv_dimple_play.setBackgroundResource(android.R.drawable.alert_light_frame);
//设置前景图片
iv_dimple_play.setImageResource(android.R.drawable.ic_media_pause);


status = false;
}else{
iv_dimple_play.setBackgroundResource(android.R.drawable.alert_dark_frame);
//设置前景图片
iv_dimple_play.setImageResource(android.R.drawable.ic_media_play);


status = true;
}
}
});
cbx_simple_basketball = (CheckBox) findViewById(R.id.cbx_simple_basketball);
cbx_simple_football = (CheckBox) findViewById(R.id.cbx_simple_football);
cbx_simple_pingpong = (CheckBox) findViewById(R.id.cbx_simple_pingpong);


cbx_simple_basketball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleComponentActivity.this,"选择了学习", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SimpleComponentActivity.this, "放弃了学习", Toast.LENGTH_SHORT).show();
}
}
});


cbx_simple_football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleComponentActivity.this, "选了玩游戏", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SimpleComponentActivity.this, "弃了玩游戏", Toast.LENGTH_SHORT).show();
}
}
});


cbx_simple_pingpong.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(SimpleComponentActivity.this,"选择了睡觉", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(SimpleComponentActivity.this,"放弃了睡觉", Toast.LENGTH_SHORT).show();
}
}
});
rg_simple_sex = (RadioGroup) findViewById(R.id.rg_simple_sex);
rg_simple_sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//checkedId就是选中的RadioButton的id
//找到选中的RadioButton
RadioButton radioButton = (RadioButton)findViewById(checkedId);
//得到文本
String sex = radioButton.getText().toString();
//提示
Toast.makeText(SimpleComponentActivity.this, sex, Toast.LENGTH_SHORT).show();
}
});
}
public void confirm(View v){
StringBuffer sb = new StringBuffer();
if(cbx_simple_basketball.isChecked()){
sb.append(cbx_simple_basketball.getText().toString()).append(" ");
}else if(cbx_simple_football.isChecked()){
sb.append(cbx_simple_football.getText().toString()).append(" ");
}else if(cbx_simple_pingpong.isChecked()){
sb.append(cbx_simple_pingpong.getText().toString()).append(" ");
}
//提示
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
}

}





activity_simple_component.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <TextView
        android:id="@+id/tv_simple_msg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#123456"
        android:text="@string/tv_content"
        android:textColor="#FF0000"
        android:textSize="20sp" />


    <EditText
        android:id="@+id/et_simple_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_input_num"
        android:inputType="phone" />


    <Button
        android:id="@+id/btn_simple_submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/btn_submit" />


    <ImageView
        android:id="@+id/iv_simple_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:drawable/alert_dark_frame"
        android:src="@android:drawable/ic_media_play" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <TextView
            android:id="@+id/tv_simple_hobby"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tv_hobby" />


        <CheckBox
            android:id="@+id/cbx_simple_basketball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cbx_hobby1" />


        <CheckBox
            android:id="@+id/cbx_simple_football"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cbx_hobby2" />


        <CheckBox
            android:id="@+id/cbx_simple_pingpong"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cbx_hobby3" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="confirm"
            android:text="@string/btn_ok" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
    </LinearLayout>
    


    <RadioGroup
        android:id="@+id/rg_simple_sex"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >


        <RadioButton
            android:id="@+id/rb_simple_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男" />


        <RadioButton
            android:id="@+id/rb_simple_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女" />


        <RadioButton
            android:id="@+id/rb_simple_nomale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ET" />
    </RadioGroup>


</LinearLayout>




strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>


    <string name="app_name">zmh3</string>
    <string name="hello_world">Hello world!</string>
    <string name="test_simple">测试常用简单的Component</string>
    <string name="test_menu">测试菜单Component</string>
    <string name="test_progress">测试进度条Component</string>
    <string name="test_dialog">测试对话框Component</string>
    <string name="tv_content">这是TextView的内容</string>
    <string name="title_activity_simple_component">SimpleComponentActivity</string>
<string name="hint_input_num">请输入手机号</string>
<string name="btn_submit">提交</string>
<string name="tv_hobby">爱好:</string>
<string name="cbx_hobby1">学习</string>
<string name="cbx_hobby2">玩游戏</string>
<string name="cbx_hobby3">睡觉</string>
<string name="btn_ok">确定</string>
<string name="rbn_man">男</string>
<string name="rbn_woman">女</string>
<string name="rbn_nothing">ET</string>
   
</resources>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值