运行时崩溃,不知道是什么原因,求解答
一运行就崩溃
一运行就崩溃
2016-03-16
<?xml version="1.0" encoding ="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ToggleButton
android:textOn="开"
android:textOff="关"
android:id="@+id/toggleButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="false"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="@+id/toggleButton1"
android:background="@drawable/off"/>
</LinearLayout>
package com.textviewkongjian;
import android.app.Activity;
import android.graphics.AvoidXfermode;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.MultiAutoCompleteTextView;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements OnCheckedChangeListener{
private ToggleButton tbt;
private ImageButton imabt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化ToggleButton控件
tbt=(ToggleButton) findViewById(R.id.toggleButton1);
imabt=(ImageButton) findViewById(R.id.imageView1);
/*
* 监听ToggleButton开关(给当前的tbt设置监听器)
*/
tbt.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*当tbt被点击时这个方法会执行
* buttonView代表被点击控件的本身
* isChecked代表被点击控件的状态
* 当点击tbt的时候,更换imabt的背景
*/
imabt.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
}
}
举报