簡體   English   中英

使用微調框訪問資產文件夾中的文件

[英]Using Spinner for accessing a file from assets folder

我已經在資產目錄中存儲了一些文本文件,並希望在程序中讀取它們。 我創建了一個微調器以列出可用的文本文件。 選擇要讀取的文件。

我使用的xml代碼是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff000000"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/content"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:baselineAligned="true"
            android:gravity="bottom" >

            <TableRow
                android:layout_width="wrap_content"
                android:padding="10dp" >

              <Spinner
        android:id="@+id/spinner1"
        android:background="#ffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/wm_arrays"
        android:prompt="@string/watermark_size"
          />
            </TableRow>


            <TableRow android:padding="5dp" >
                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="insert"
                    android:text="Apply" />

            </TableRow>

            <TableRow android:padding="5dp" >

                <TextView
                    android:id="@+id/displaystring"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="String: "
                    android:textColor="#ffffff" />
            </TableRow>

        </TableLayout>
    </LinearLayout>

</ScrollView>

我使用的Java代碼是這樣的:

公共無效insert(View v)引發IOException {str =(TextView)findViewById(R.id.displaystring);

            spinner1 = (Spinner) findViewById(R.id.spinner1);

            String res= String.valueOf(spinner1.getSelectedItem());

            if(res == "1B"){
                input = assetManager.open("wmark_8.txt");

            if(res == "2B"){
                input = assetManager.open("wmark_16.txt");
            }
            if(res == "4B"){

                input = assetManager.open("wmark_32.txt");
            }
            if(res == "8B"){
                input = assetManager.open("wmark_64.txt");
            }
            if(res == "512B"){
                input = assetManager.open("wmark_4096.txt");
            }
        if(res == "1024B"){
                input = assetManager.open("wmark_8192.txt");
            }
            if(res == "2048B"){
                input = assetManager.open("wmark_16384.txt");
            }

            int size = input.available();
            byte[] buffer = new byte[size];
            input.read(buffer);
            input.close();
            String text = new String(buffer);
                            str.setText("String: "+text);

}

我得到一個空指針異常,其中它實際上意味着未讀取輸入。 誰能幫我

嘗試這個..

==總是只比較兩個引用

對於其余條件,應使用.equals("") 字符串 if else if使用if else if

if(res.equals("1B")){
         input = assetManager.open("wmark_8.txt");
}else if(res.equals("2B")){
         input = assetManager.open("wmark_16.txt");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM