마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

커스텀 alertdialog를 만들었는데요 로그캣 하악

–1 추천

06-20 02:39:06.807: E/AndroidRuntime(6652): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.noticekorea.mpm100/com.noticekorea.mpm100.MainActivity}: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button

06-20 02:39:06.807: E/AndroidRuntime(6652): Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
06-20 02:39:06.807: E/AndroidRuntime(6652):  at com.noticekorea.mpm100.MainActivity.onCreate(MainActivity.java:52)

 

public class MainActivity extends Activity implements View.OnClickListener {
  
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     requestWindowFeature(Window.FEATURE_NO_TITLE);
     setContentView(R.layout.activity_main);
     // TODO Auto-generated method stub     
    
     getDate();
     getTime();
     loadGps();
  getLocation();
  
  Button btn2 = (Button)findViewById( R.id.save );
        btn2.setOnClickListener( new Button.OnClickListener()
        {
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    showDialog(2);
   }       
        });        
 }
  
 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  switch(id)
  {
         
  case 1:
   AlertDialog.Builder builder2 = new AlertDialog.Builder(this);   
   builder2.setMessage("Want to Save really??");
   builder2.setTitle("HEY");
   //builder2.setView(View.inflate(this, R.layout.dlg1, null));
   builder2.setIcon(R.drawable.ic_launcher);
   builder2.setNegativeButton("CANCEL", null);
   builder2.setPositiveButton("OK", null);   
   // 확인 버튼 클릭시 이벤트에 대한 처리부
      builder2.setPositiveButton("OK", new DialogInterface.OnClickListener() {
      
           public void onClick(DialogInterface dialog, int which) {
                                          // 'YES'
                              dbSave();                            
                    }
     
                    }).setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                                          // 'No'
 
                   
                    }
                    });
                    return builder2.create();
       }
  return null;
        }

@Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  
  switch(v.getId()) {
     
  case R.id.view :
   
    Intent intent = new Intent(MainActivity.this, DataActivity.class);
             //startActivityForResult(intent, 0);    //값을 전달 다시 받아오기 위해 forResult를 사용
    startActivity(intent);
   
  }
  
 }
}

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

<ImageButton
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_weight="1"
        android:background="@drawable/save_btn"
        android:onClick="onClick"
        />

 <ImageButton
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_weight="1"
        android:background="@drawable/main_btn"
        android:onClick="onClick"/>
 
</RelativeLayout>
그리워요 안드로메다 (250 포인트) 님이 2013년 6월 20일 질문

2개의 답변

0 추천
 
채택된 답변

Caused by ImageButton cannot be cast to Button

이미지 버튼은 버튼으로 캐스트 될 수  없습니다.

다음과 같이 수정하시기 바랍니다.

Button btn2 = (Button)findViewById( R.id.save );

-> ImageButton btn2 = (ImageButton )findViewById( R.id.save );

 

Gradler (109,780 포인트) 님이 2013년 6월 20일 답변
그리워요 안드로메다님이 2013년 6월 20일 채택됨
0 추천
R.id.save 버튼을 xml에서는 ImageButton으로 하고 Java 코드에서는 Button으로 해서 발생한 에러네요. 같은걸로 일치시키세요.
monal (4,210 포인트) 님이 2013년 6월 20일 답변
아 이런이런 정말 감사합니다.
...