问题
Hi i am new to Android application. I want to add zooming effect to my image. I took a sample tutorial from [https://github.com/jasonpolites/gesture-imageview][1] and copied sample code from there. But when I was tried to run the java code in eclipse, an exception is raised. Codes are attached here.
ViewIndia.java
public class ViewIndia extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_india);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
GestureImageView view = new GestureImageView(this);
view.setImageResource(R.drawable.india);
view.setLayoutParams(params);
ViewGroup lout = (ViewGroup) findViewById(R.id.gestureImageView);
lout.addView(view);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_india, menu);
return true;
}
}
activity_view_india.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:gesture-image="http://schemas.polites.com/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ViewIndia" >
<com.polites.android.GestureImageView
android:id="@+id/gestureImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/india"
gesture-image:min-scale="0.1"
gesture-image:max-scale="10.0"
gesture-image:strict="true"/>
and the log file
08-05 01:41:53.671: E/AndroidRuntime(1001): FATAL EXCEPTION: main
08-05 01:41:53.671: E/AndroidRuntime(1001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.viewindia/com.example.viewindia.ViewIndia}: java.lang.ClassCastException: com.polites.android.GestureImageView cannot be cast to android.view.ViewGroup
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.os.Looper.loop(Looper.java:137)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-05 01:41:53.671: E/AndroidRuntime(1001): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 01:41:53.671: E/AndroidRuntime(1001): at java.lang.reflect.Method.invoke(Method.java:525)
08-05 01:41:53.671: E/AndroidRuntime(1001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-05 01:41:53.671: E/AndroidRuntime(1001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-05 01:41:53.671: E/AndroidRuntime(1001): at dalvik.system.NativeStart.main(Native Method)
08-05 01:41:53.671: E/AndroidRuntime(1001): Caused by: java.lang.ClassCastException: com.polites.android.GestureImageView cannot be cast to android.view.ViewGroup
08-05 01:41:53.671: E/AndroidRuntime(1001): at com.example.viewindia.ViewIndia.onCreate(ViewIndia.java:25)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.Activity.performCreate(Activity.java:5133)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-05 01:41:53.671: E/AndroidRuntime(1001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
08-05 01:41:53.671: E/AndroidRuntime(1001): ... 11 more
Please help me to ride off from the error.
回答1:
ViewGroup lout = (ViewGroup) findViewById(R.id.gestureImageView);
Your gestureImageView is not a ViewGroup. Which Class does it extend from? Is it View? if yes, use
View lout = (View) findViewById(R.id.gestureImageView);
来源:https://stackoverflow.com/questions/18051392/android-run-time-exception-while-using-gestureimageview-and-viewgroup