简体   繁体   English

不读取文件

[英]Doesn't read file

first I want to apologize for my bad english, I am from Slovakia ;) 首先,我要为我的英语不好而道歉,我来自斯洛伐克;)

So there is my problem 所以我有问题

My friend wants to create a very simple translate aplication, very very very simple. 我的朋友想创建一个非常简单的翻译应用程序,非常非常简单。 We are beginners in programming for android and at all in java. 我们是android以及Java编程的初学者。 The problem is, that our apk works great in Eclipse so we decided to create apk file and install it in our device. 问题是,我们的apk在Eclipse中运行良好,因此我们决定创建apk文件并将其安装在我们的设备中。 So when we installed it and there was a problem, our apk in device doesnt read file where are our words. 因此,当我们安装它而出现问题时,设备中的apk无法读取文件中的单词。 So we tried it again in eclipse emulator and doesnt work too, but before creating apk it was fully working. 因此,我们在eclipse模拟器中再次尝试了它,但是也无法正常工作,但是在创建apk之前,它已经完全正常了。 Our file is in res/raw/dictionary 我们的文件在res / raw / dictionary中

Here is our code 这是我们的代码

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 logcat 错误日志

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

getResources().openRawResource() gets an InputStream for a file in the res/raw/ directory of your Android project. getResources()。openRawResource()获取Android项目res / raw /目录中文件的InputStream。 The argument to openRawResource() is an int named R.raw. openRawResource()的参数是一个名为R.raw的int。 filename where filename is the name of the file without the extension. filename ,其中filename是不带扩展名的文件的名称。

So, the file won't be in the res/assets/ directory. 因此,该文件将不在res / assets /目录中。

Although you can doublecheck that the file you're trying to read is actually in res/raw, it seems odd to me that you could build the APK and get an R.raw. 尽管您可以仔细检查要读取的文件实际上是在res / raw中,但对我来说,可以构建APK并获得R.raw似乎很奇怪。 filename entry if the file wasn't actually there. 文件名条目(如果文件实际上不存在)。 I'd use a debugger to step through the code and check the variables. 我将使用调试器来逐步检查代码并检查变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM