我正在尝试创建一个图像库,我已经编写了代码,一切都很好,但是当我在avd上运行应用程序并单击启动活动的按钮时,应用程序崩溃
我的代码适用于我的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};
}
有人可以告诉我我哪里出错了,因为我似乎无法发现任何事情
我的日志猫:
>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)
答案 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 };
}