개요는 스플래시뷰 이후에 메인화면에서 버튼을 누르면 다른 뷰(FoodChoice.class)로 넘어가는 것을 구현할려고 합니다.
xml 파일에는 이상이 없습니다. id지정도 맞게 했구요.. 코드상에 오류는 없는데 실행하면 자동종료됩니다.
logcat 에 오류를 찾아보니 Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast
to android.widget.Button
이라고 나오는데 여기서 어떻게 해야할지 모르겠네요. 답변 부탁드립니다^^
----> MainActivity.java
package com.devkim.choicemaster;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button goFoodBtn, goWhoBtn, favoriteBtn, helpBtn, exitBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.startActivity(new Intent(this, SplashView.class));
goFoodBtn = (Button) findViewById(R.id.goFoodBtn);
goFoodBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), FoodChoice.class);
startActivity(intent);
}
});
}
}
----> SplashView.java
package com.devkim.choicemaster;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
public class SplashView extends Activity {
@SuppressLint("HandlerLeak")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splashview);
Handler handler = new Handler() {
public void handleMessage(Message msg) {
finish();
}
};
handler.sendEmptyMessageDelayed(0, 3000);
}
}
----> main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/AbsoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="Deprecated,HardcodedText" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="1dp"
android:contentDescription="TODO"
android:scaleType="fitXY"
android:src="@drawable/main"
tools:ignore="HardcodedText,ContentDescription" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="54dp"
android:contentDescription="TODO"
android:src="@drawable/main3"
tools:ignore="HardcodedText,ContentDescription" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="2dp"
android:layout_y="27dp"
android:src="@drawable/main2"
tools:ignore="ContentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="391dp"
android:layout_x="1dp"
android:layout_y="149dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="75dp"
android:contentDescription="TODO"
tools:ignore="HardcodedText" >
<ImageButton
android:id="@+id/goFoodBtn"
android:layout_width="104dp"
android:layout_height="60dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:contentDescription="TODO"
android:src="@drawable/button1"
tools:ignore="ContentDescription" />
<ImageButton
android:id="@+id/goWhoBtn"
android:layout_width="104dp"
android:layout_height="60dp"
android:src="@drawable/button2"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/favoriteBtn"
android:layout_width="104dp"
android:layout_height="60dp"
android:layout_marginLeft="35dp"
android:layout_marginRight="35dp"
android:contentDescription="TODO"
android:src="@drawable/button3"
tools:ignore="ContentDescription" />
<ImageButton
android:id="@+id/helpBtn"
android:layout_width="104dp"
android:layout_height="60dp"
android:src="@drawable/button4"
tools:ignore="ContentDescription" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/exitBtn"
android:layout_width="104dp"
android:layout_height="60dp"
android:layout_marginLeft="108dp"
android:src="@drawable/button5"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
</AbsoluteLayout>
----> splashview.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="0dp"
android:scaleType="fitXY"
android:src="@drawable/main" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="136dp"
android:layout_y="263dp"
android:text="Loading..."
android:textColor="#FFFFFF" />
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="138dp"
android:layout_y="209dp" />
</AbsoluteLayout>
Caused by: java.lang.ClassCastException: android.widget.ImageButton cannot be cast to android.widget.Button
ClassCastException 는 매칭하는 클래스가 서로 맞지 않아서 나는 오류 입니다.
내용을 보니 ImageButton 타입의 클래스변수를 Button 타입의 클래스변수에 넣으려다보니 오류가 발생한다고 하는군요.
이를 바탕으로 올려두신 소스를 보니
layout 문서에서는 ImageButton 으로 선언하셨는데, 액티비티 클래스에서는 button 으로 선언하셔서 활용하시려고 하시네요.
두개를 동일한 것으로 통일하시면 해결 될것 같네요.
참고적으로..
button 과 imagebutton 이 보통 버튼이기 때문에 겉으로 봐서는 같은 상속관계가 있겠지하고는 생각될수 있겠지만..
내부적으로 button 은 textview 를.. imagebutton 은 imageview 를 부모로 가지고 있기 때문에, 다른얘들로 분류된답니다.