2012-12-06 34 views
0

我努力使圖像的畫廊,我寫的代碼,一切都很好,但是當我運行的AVD該應用並點擊其中啓動應用程序崩潰圖片在Android

活動按鈕庫

我的代碼是我的xml:

<ImageSwitcher android:id="@+id/switcher" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
/> 

<Gallery android:id="@+id/gallery" 
    android:background="#55000000" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 

    android:gravity="center_vertical" 
    android:spacing="16dp" 
/> 

</RelativeLayout> 

和我的課:

public class ScrollingQuran extends Activity implements 
AdapterView.OnItemSelectedListener{ 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 

    setContentView(R.layout.qurangallery); 

    mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 
    mSwitcher.setFactory(this); 
    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_in)); 
    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
      android.R.anim.fade_out)); 

    Gallery g = (Gallery) findViewById(R.id.gallery); 
    g.setAdapter(new ImageAdapter(this)); 
    g.setOnItemSelectedListener(this); 
} 

public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { 
    mSwitcher.setImageResource(mImageIds[position]); 
} 

public void onNothingSelected(AdapterView<?> parent) { 
} 

public View makeView() { 
    ImageView i = new ImageView(this); 
    i.setBackgroundColor(0xFF000000); 
    i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT, 
      LayoutParams.MATCH_PARENT)); 
    return i; 
} 

private ImageSwitcher mSwitcher; 

public class ImageAdapter extends BaseAdapter { 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    public int getCount() { 
     return mThumbIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView i = new ImageView(mContext); 

     i.setImageResource(mThumbIds[position]); 
     i.setAdjustViewBounds(true); 
     i.setLayoutParams(new Gallery.LayoutParams(
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    // i.setBackgroundResource(R.drawable.picture_frame); 
     return i; 
    } 

    private Context mContext; 

} 

private Integer[] mThumbIds = { 
     R.drawable.page1, R.drawable.page2}; 

private Integer[] mImageIds = { 
     R.drawable.page1, R.drawable.page2}; 

} 

可能有人請告訴我在哪裏IA要去錯了,因爲我似乎無法找出什麼

我的日誌貓:

>12-06 17:54:10.050: E/AndroidRuntime(1152): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fuelfinder/com.example.fuelfinder.ScrollingQuran}: java.lang.ClassCastException: com.example.fuelfinder.ScrollingQuran cannot be cast to android.widget.ViewSwitcher$ViewFactory 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread.access$600(ActivityThread.java:130) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.os.Looper.loop(Looper.java:137) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at java.lang.reflect.Method.invoke(Method.java:511) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at dalvik.system.NativeStart.main(Native Method) 
12-06 17:54:10.050: E/AndroidRuntime(1152): Caused by: java.lang.ClassCastException: com.example.fuelfinder.ScrollingQuran cannot be cast to android.widget.ViewSwitcher$ViewFactory 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at com.example.fuelfinder.ScrollingQuran.onCreate(ScrollingQuran.java:31) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.Activity.performCreate(Activity.java:5008) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
12-06 17:54:10.050: E/AndroidRuntime(1152):  ... 11 more 
12-06 17:56:08.160: E/Trace(1198): error opening trace file: No such file or directory (2) 
+2

你可以發佈logcat的圖像那裏。 –

+0

我得到它的工作,我從類的工具中刪除ViewSwitcher,它的工作。不太確定,但現在正在運行。無論如何謝謝你 – Jay240692

+0

嘿恭喜....很高興聽到...你解決了你的問題....這很好 –

回答

0

檢查,如果你輸入正確的佈局參數.. 我覺得應該是進口android.widget.Gallery.LayoutParams;你可能會引進一些其他的。一旦有一個看看它

不要更改類名,XML文件,並

package com.exam.samplegalery; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.animation.AnimationUtils; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.Gallery.LayoutParams; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher.ViewFactory; 

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener, ViewFactory { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     setContentView(R.layout.activity_main); 

     mSwitcher = (ImageSwitcher) findViewById(R.id.switcher); 
     mSwitcher.setFactory(this); 
     mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_in)); 
     mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, 
       android.R.anim.fade_out)); 

     Gallery g = (Gallery) findViewById(R.id.gallery); 
     g.setAdapter(new ImageAdapter(this)); 
     g.setOnItemSelectedListener(this); 
    } 

    public void onItemSelected(AdapterView<?> parent, View v, int position, 
      long id) { 
     mSwitcher.setImageResource(mImageIds[position]); 
    } 

    public void onNothingSelected(AdapterView<?> parent) { 
    } 

    public View makeView() { 
     ImageView i = new ImageView(MainActivity.this); 
     i.setBackgroundColor(0xFF000000); 
     i.setScaleType(ImageView.ScaleType.FIT_CENTER); 
     i.setLayoutParams(new ImageSwitcher.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
     return i; 
    } 

    private ImageSwitcher mSwitcher; 

    public class ImageAdapter extends BaseAdapter { 
     public ImageAdapter(Context c) { 
      mContext = c; 
     } 

     public int getCount() { 
      return mThumbIds.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView i = new ImageView(mContext); 

      i.setImageResource(mThumbIds[position]); 
      i.setAdjustViewBounds(true); 
      i.setLayoutParams(new Gallery.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
      // i.setBackgroundResource(R.drawable.picture_frame); 
      return i; 
     } 

     private Context mContext; 

    } 

    private Integer[] mThumbIds = { R.drawable.ic_action_search, R.drawable.ic_launcher }; 

    private Integer[] mImageIds = { R.drawable.ic_action_search, R.drawable.ic_action_search }; 

} 
+0

我看到我在做什麼是實現AdapterView.OnItemSelectedListener,ViewSwitcher.ViewFactory而不是ViewFactory,愚蠢的錯誤。非常感謝 – Jay240692

+0

歡迎您:)快樂編碼 –