[問題] getResoures與context的問題

看板AndroidDev作者 (JIM)時間10年前 (2016/01/28 14:06), 10年前編輯推噓2(207)
留言9則, 3人參與, 最新討論串1/1
R.raw中已經放好一個JSON 要用的class跟Activity在同一個package 請問要怎麼去使用getResoures() this dot下去後 只有getClass()這個比較像能叫用的 但是這時候this就會說 cannot be refenrces from a static context 對於context這塊真的很不熟悉 還請各位指點,感謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 42.73.178.63 ※ 文章網址: https://www.ptt.cc/bbs/AndroidDev/M.1453961219.A.DCE.html

01/28 14:11, , 1F
跟static 比較有關吧,把code貼出來看看,你可能用到stat
01/28 14:11, 1F

01/28 14:11, , 2F
ic的變數
01/28 14:11, 2F
大概是這樣的~ /////// Activity /////////////// CheckJSON.Check(this,string); /////// Class CheckJSON //////// public class CheckJSON { public static double Check(Context context, String string){ double c = 0.0; InputStream is = context.getResources().openRawResource(R.raw.ca); ....... }

01/28 14:53, , 3F
你call Check那段是在activity裡的哪裡...
01/28 14:53, 3F

01/28 14:56, , 4F
那段訊息是說在static方法/區塊中不能用this,是Java問題
01/28 14:56, 4F

01/28 14:56, , 5F
不是在說android的Context
01/28 14:56, 5F
在這!! public class SpotDetailActivity extends AppCompatActivity { private class MyInfoWindowAdapter implements GoogleMap.InfoWindowAdapter { public View getInfoWindow(Marker marker) { CALL CHECK }}} ※ 編輯: jiqian (42.73.178.63), 01/28/2016 14:58:14

01/28 15:03, , 6F
用 SpotDetailActivity.this ?
01/28 15:03, 6F

01/28 15:17, , 7F
你在那邊呼叫的this會變成adapter那個class
01/28 15:17, 7F

01/28 15:18, , 8F
可以的話寫個mcontext 的全域變數,以後會常用到
01/28 15:18, 8F
謝謝各位的幫忙,我在外面放個CONTEXT全域變數可以用了!! ※ 編輯: jiqian (42.73.178.63), 01/28/2016 16:17:19

01/31 06:59, , 9F
我覺得先搞懂this為什麼會錯比較實用XD
01/31 06:59, 9F
文章代碼(AID): #1MgR03tE (AndroidDev)