2013-10-03 22 views
1

首先我要爲我的英語不好道歉,我是來自斯洛伐克)不讀文件

因此,有我的問題

我的朋友想創建一個非常簡單的翻譯aplication,非常非常很簡單。我們是爲Android編程的初學者,並且完全是用java編寫的。問題是,我們的apk在Eclipse中工作得很好,因此我們決定創建apk文件並將其安裝到我們的設備中。所以當我們安裝它並出現問題時,我們的apk中的設備不會讀取文件,我們的文字在哪裏。所以我們在eclipse模擬器中再次嘗試它,並且不起作用,但是在創建apk之前它已經完全工作。我們的文件是在res /生/字典

這裏是我們的代碼

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; 
} 



} 

錯誤的logcat

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

你在logcat中看到錯誤嗎?如果是這樣,請把它寫在你的問題上。 –

+0

資產文件是否實際存在於您的資產目錄中? –

+0

無論您認爲自己的問題是什麼,該錯誤消息與您的問題無關。 – CommonsWare

回答

0

getResources()。openRawResource()獲取一個InputStream一個文件在res /你的Android項目的原始/目錄。 openRawResource()的參數是一個名爲R.raw的int。 文件名其中文件名是沒有擴展名的文件的名稱。

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

雖然你可以重新檢查你正在嘗試閱讀的文件實際上是否是res/raw,但是我覺得你可以構建APK並獲得R.raw,這似乎很奇怪。 文件名如果該文件實際上不存在,則輸入條目。我會使用調試器來遍歷代碼並檢查變量。

+0

嗯文件字典實際上是在目錄res/raw /所以我不知道什麼是錯的,並且peri hartman在資產目錄中沒有任何文件,是空的 – LadIQe