죄송합니다. 안드로이드 펍 개발자분들 제가 경황이 없어서 소스코드만 올렸네요..
어플이 시작하자마자 죽어버리는데요 원인이 뭔지모르겠네요
에러는 08-13 12:46:10.178: E/AndroidRuntime(16376): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.ToggleButton 현재 이게 문제가 되는것같습니다.
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity {
private static final int REQUEST_ENABLE_BT = 0;
private static final BluetoothAdapter mBluetoothAdapter = null;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//ImageButton bluetooth=(ImageButton) findViewById(R.id.bluetoothbtn);
//bluetooth.setOnClickListener((OnClickListener) this);
//if(btService ==null){
//btService=new Bluetooth(this,mHandler);
//}
//bluetooth.setSelected(true);
final BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
final boolean hasBluetooth = (mBluetoothAdapter == null);
final ToggleButton bluetooth = (ToggleButton) findViewById(R.id.bluetoothbtn);
final ToggleButton earphone=(ToggleButton) findViewById(R.id.earphonnbtn);
final ToggleButton phonemic=(ToggleButton) findViewById(R.id.phonemic);
//bluetooth.setChecked(false);
bluetooth.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
// Perform action on clicks
if (bluetooth.isChecked())
{
if (hasBluetooth && !mBluetoothAdapter.isEnabled())
{
// prompt the user to turn BlueTooth on
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
else
{
if (hasBluetooth && mBluetoothAdapter.isEnabled())
{
boolean isDisabling = mBluetoothAdapter.disable();
if (!isDisabling)
{
}
}
}
}
});
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if ((requestCode == REQUEST_ENABLE_BT) && (resultCode == RESULT_OK))
{
boolean isEnabling = mBluetoothAdapter.enable();
if (!isEnabling)
{
// an immediate error occurred - perhaps the bluetooth is already on?
}
else if (mBluetoothAdapter.getState() == BluetoothAdapter.STATE_TURNING_ON)
{
}
}
}
ture false일때 버튼이구요
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/gbutton" android:state_checked="false"/>
<item android:drawable="@drawable/rbutton" android:state_checked="true"/>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/circle_gray_red"/>
</layer-list>
레이아웃상에서의 코드입니다.
<ToggleButton
android:id="@+id/phonemic"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_above="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:textOn=""
android:textOff=""
android:background="@drawable/t_gray_red" />
<ToggleButton
android:id="@+id/bluetoothbtn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/textView2"
android:layout_marginBottom="10dp"
android:background="@drawable/t_gray_red" />
<ToggleButton
android:id="@+id/earphonebtn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_below="@+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="11dp"
android:textOn=""
android:textOff=""
android:background="@drawable/t_gray_red" />
며칠째 에러의 원인이 뭔지를 모르겠네요 ㅜㅜ