我正在使用viewpager来创建一个自定义厨房,其中包含我保存在sqlite数据库中的图像,其路径如下:@ drawable.foto1。
我可以正确获取这些数据,但我的应用程序崩溃并从我用于保存图像路径的整数中显示NumberFormatException。我的代码是:
public class Tab3Up extends Activity {
private String mString[];
private Integer ArrayFotos[];
private Context context;
private HipotecaDbAdapter dbAdapter;
private Cursor cursor;
private long id ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pageslide);
Intent intent = getIntent();
Bundle extra = intent.getExtras();
if (extra == null) return;
dbAdapter = new HipotecaDbAdapter(this);
dbAdapter.abrir();
id = extra.getLong(HipotecaDbAdapter.C_COLUMNA_ID);
cursor = dbAdapter.getRegistro(id);
Log.i("IDENTIFICADOR BD ","EL ID ES: "+id);
ArrayList<String> array = new ArrayList<String>();
array.add(cursor.getString(cursor.getColumnIndex(HipotecaDbAdapter.C_COLUMNA_FOTO1)));
mString = (String[]) array.toArray(new String[array.size()]);
Log.d("tag", "Trying to get count on line 50 class Test");
ArrayFotos=new Integer[mString.length];
for(int i=0;i<mString.length;i++){
ArrayFotos[i]=Integer.parseInt(mString[i]);
}
ViewPagerAdapter adapter = new ViewPagerAdapter(Tab3Up.this,ArrayFotos);
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
// private Integer imgArra[] = { R.drawable.foto1, R.drawable.foto2,
// R.drawable.foto3, R.drawable.foto4, };
//fotos[0] = (Integer[])array.toArray(new Integer[array.size()]);
public class ViewPagerAdapter extends PagerAdapter {
Activity activity;
Integer imageArray[];
public ViewPagerAdapter(Activity act,Integer ArrayFotos[]) {
imageArray = ArrayFotos;
activity = act;
}
public int getCount() {
Log.d("tag", "Trying to get count on line 50 class Test");
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
}
我日志中的主要错误:
02-20 09:52:58.439: E/AndroidRuntime(17861): FATAL EXCEPTION: main
02-20 09:52:58.439: E/AndroidRuntime(17861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bdoffline/com.example.bdoffline.Tab3Up}: java.lang.NumberFormatException: Invalid int: "@drawable/foto1"
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread.access$600(ActivityThread.java:134)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.os.Looper.loop(Looper.java:154)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread.main(ActivityThread.java:4624)
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 09:52:58.439: E/AndroidRuntime(17861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
02-20 09:52:58.439: E/AndroidRuntime(17861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
02-20 09:52:58.439: E/AndroidRuntime(17861): at dalvik.system.NativeStart.main(Native Method)
02-20 09:52:58.439: E/AndroidRuntime(17861): Caused by: java.lang.NumberFormatException: Invalid int: "@drawable/foto1"
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.Integer.invalidInt(Integer.java:138)
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.Integer.parse(Integer.java:375)
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.Integer.parseInt(Integer.java:366)
02-20 09:52:58.439: E/AndroidRuntime(17861): at java.lang.Integer.parseInt(Integer.java:332)
02-20 09:52:58.439: E/AndroidRuntime(17861): at com.example.bdoffline.Tab3Up.onCreate(Tab3Up.java:58)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.Activity.performCreate(Activity.java:4479)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
02-20 09:52:58.439: E/AndroidRuntime(17861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
02-20 09:52:58.439: E/AndroidRuntime(17861): ... 11 more
希望这有帮助!!谢谢!
答案 0 :(得分:0)
您尝试将字符串“@ drawable / foto1”转换为整数,并确定它失败,因为它不代表整数。
答案 1 :(得分:0)
您不应将字符串"@drawable/foto1"
存储到数据库中。而是存储R.drawable.foto1
的值,它是一个整数,您可以直接将其应用于setBackgroundResource()
。