第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在簡(jiǎn)單的類(lèi)中,無(wú)法打開(kāi) Assets 文件夾中的文件以加載到 Android 應(yīng)用程序中的數(shù)組

在簡(jiǎn)單的類(lèi)中,無(wú)法打開(kāi) Assets 文件夾中的文件以加載到 Android 應(yīng)用程序中的數(shù)組

慕仙森 2024-01-05 15:20:26
我想從我的簡(jiǎn)單java類(lèi)“MyContent”中讀取,并且有一個(gè)沒(méi)有參數(shù)的靜態(tài)方法,因?yàn)橐坏┠阏{(diào)用里面的變量,它就會(huì)執(zhí)行靜態(tài)方法內(nèi)的代碼。我試圖通過(guò)讀取資產(chǎn)文件夾中的文件并將代碼放入其中,以便數(shù)據(jù)適配器能夠讀取它。我的內(nèi)容類(lèi):public class MyContent extends Application {public static final List<Element> ITEMS = new ArrayList<Element>();private static Random random = new Random(System.currentTimeMillis());public static final Map<String, Element> ITEM_MAP = new HashMap<String, Element>();AssetManager assetManager = getAssets();static {    try {        InputStream inputStream = getAssets().open("data.csv");        InputStreamReader inputStreamReader=new InputStreamReader((inputStream));        BufferedReader bufferedReader=new BufferedReader((inputStreamReader));        String tt="";        while ((tt=bufferedReader.readLine())!=null){            MyContent.addItemElement(MyContent.createElement(tt));        }    } catch (FileNotFoundException e) {        e.printStackTrace();    } catch (IOException e) {        e.printStackTrace();    }}}問(wèn)題是 AssetManager assetManager = getAssets(); 不能是靜態(tài)的,但 InputStream inputStream = getAssets().open("data.csv"); 我需要將它們放入靜態(tài)方法中,誰(shuí)能告訴我如何處理這個(gè)問(wèn)題?
查看完整描述

1 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

getAssets()需要context預(yù)先初始化應(yīng)用程序,然后才能]調(diào)用它。下面的代碼流程應(yīng)該能夠?qū)崿F(xiàn)您的目標(biāo):


public class MyContent extends Application {


    public static final Map<String, Element> ITEM_MAP;


    @Override

    public void onCreate() {

        super.onCreate();


        ITEM_MAP = new HashMap<String, Element>();

        AssetManager assetManager = getAssets();

        try {

            InputStream inputStream = getAssets().open("data.csv");

            InputStreamReader inputStreamReader=new InputStreamReader((inputStream));

            BufferedReader bufferedReader=new BufferedReader((inputStreamReader));

            String tt="";

            while ((tt=bufferedReader.readLine())!=null){

                MyContent.addItemElement(MyContent.createElement(tt));

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}


編輯1

在你的里面AndroidManifest.xml,記得在標(biāo)簽下添加你的應(yīng)用程序類(lèi)application,例如


<application

        android:name=". MyContent"

   ...

</application>


查看完整回答
反對(duì) 回復(fù) 2024-01-05
  • 1 回答
  • 0 關(guān)注
  • 132 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)