0

即時通訊使用viewpager創建一個自定義廚房的圖像,我保存在一個像這樣的路徑的sqlite數據庫:@ drawable.foto1。帶有可繪製圖像的NumberFormatException int值

我可以正確地獲得這個數據,但我的應用程序崩潰,並顯示從我使用的整數NumberFormatException異常保存image.My代碼的路徑是:

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

是「@ drawable/foto1」的整數嗎? – Blackbelt

回答

0

您不應該將字符串"@drawable/foto1"存儲到您的數據庫中。而是存儲R.drawable.foto1這是一個整數的值,您可以直接將它應用於setBackgroundResource()

+0

最後作品!我使用字符串值和這個getresource後獲得id,這是一個int,它的工作原理! – Napster

0

您嘗試將字符串「@ drawable/foto1」轉換爲整數,並且確實已經失敗,因爲它不代表整數。