2013-08-05 77 views
0

嗨,我是Android應用程序的新手。我想添加縮放效果到我的圖像。我從[https://github.com/jasonpolites/gesture-imageview] [1]獲取了示例教程,並從那裏複製了示例代碼。但是當我試圖在eclipse中運行java代碼時,會引發異常。 代碼附在這裏。使用gestureImageView和ViewGroup時Android運行時異常

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"/>  

和日誌文件

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 

請幫我從錯誤騎走。

回答

0
ViewGroup lout = (ViewGroup) findViewById(R.id.gestureImageView); 

你gestureImageView不是一個ViewGroup。它從哪個類擴展而來?它是視圖嗎?如果是,請使用

View lout = (View) findViewById(R.id.gestureImageView); 
+0

查看適合那裏。非常感謝。 – maria