作者jiqian (JIM)
看板AndroidDev
標題[問題] getResoures與context的問題
時間Thu Jan 28 14:06:57 2016
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://webptt.com/m.aspx?n=bbs/AndroidDev/M.1453961219.A.DCE.html
1F:推 cakeboy: 跟static 比較有關吧,把code貼出來看看,你可能用到stat 01/28 14:11
2F:→ cakeboy: ic的變數 01/28 14:11
大概是這樣的~
/////// 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);
.......
}
3F:→ ssccg: 你call Check那段是在activity裡的哪裡... 01/28 14:53
4F:→ ssccg: 那段訊息是說在static方法/區塊中不能用this,是Java問題 01/28 14:56
5F:→ ssccg: 不是在說android的Context 01/28 14:56
在這!!
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
6F:→ ssccg: 用 SpotDetailActivity.this ? 01/28 15:03
7F:推 cakeboy: 你在那邊呼叫的this會變成adapter那個class 01/28 15:17
8F:→ cakeboy: 可以的話寫個mcontext 的全域變數,以後會常用到 01/28 15:18
謝謝各位的幫忙,我在外面放個CONTEXT全域變數可以用了!!
※ 編輯: jiqian (42.73.178.63), 01/28/2016 16:17:19
9F:→ y3k: 我覺得先搞懂this為什麼會錯比較實用XD 01/31 06:59