簡體   English   中英

不讀取文件

[英]Doesn't read file

首先,我要為我的英語不好而道歉,我來自斯洛伐克;)

所以我有問題

我的朋友想創建一個非常簡單的翻譯應用程序,非常非常簡單。 我們是android以及Java編程的初學者。 問題是,我們的apk在Eclipse中運行良好,因此我們決定創建apk文件並將其安裝在我們的設備中。 因此,當我們安裝它而出現問題時,設備中的apk無法讀取文件中的單詞。 因此,我們在eclipse模擬器中再次嘗試了它,但是也無法正常工作,但是在創建apk之前,它已經完全正常了。 我們的文件在res / raw / dictionary中

這是我們的代碼

package com.example.dictionary;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

TextView Vstup;
TextView Vystup;
Button presun;
String slovo;
String word;
String found;
Boolean click;
int i;
int j;
String sub;
String strf;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Vstup = (TextView)findViewById(R.id.editText1);
    Vystup = (TextView)findViewById(R.id.textView2);

    presun = (Button)findViewById(R.id.button1);
    presun.setOnClickListener(new OnClickListener() 
    {
        public void onClick(View v)
        {


                try
                {
                    slovo = Vstup.getText().toString(); 
                    InputStream is = getResources().openRawResource(R.raw.dictionary);

                    InputStreamReader inputStreamReader = new InputStreamReader(is);
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

                    while((strf = bufferedReader.readLine()) != null)
                    {
                        i = strf.indexOf(":"); // vrati prvu poziciu retazca 
                        j = strf.indexOf(",");
                        sub = strf.substring(0,i); //vyberie zo stringu podretazec od indexu 0 po i
                        if(slovo.equals(sub))
                        {
                            found = strf.substring(i+1,j);
                            word = ("Výstup: " + found);
                            Vystup.setText(word.toString());

                        }
                        else {
                            word = ("Výstup: Word not found");
                            Vystup.setText(word.toString());
                        }

                    }
                    bufferedReader.close();

                }

                catch(Exception e)
                {
                    System.out.println(e);
                }
            }               



    });


}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}



}

錯誤日志

error opening trace file: no such file or directory(2)

getResources()。openRawResource()獲取Android項目res / raw /目錄中文件的InputStream。 openRawResource()的參數是一個名為R.raw的int。 filename ,其中filename是不帶擴展名的文件的名稱。

因此,該文件將不在res / assets /目錄中。

盡管您可以仔細檢查要讀取的文件實際上是在res / raw中,但對我來說,可以構建APK並獲得R.raw似乎很奇怪。 文件名條目(如果文件實際上不存在)。 我將使用調試器來逐步檢查代碼並檢查變量。

暫無
暫無

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

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