Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android从Res加载文件错误_Android_File_File Io - Fatal编程技术网

Android从Res加载文件错误

Android从Res加载文件错误,android,file,file-io,Android,File,File Io,我正在尝试从res/raw加载文本文件。我看过一些代码片段,并尝试了几种实现方法,但似乎没有一种适合我。我目前正在尝试的代码是 TextView helloTxt = (TextView)findViewById(R.id.hellotxt); helloTxt.setText(readTxt()); } private String readTxt() { InputStream inputStream = getResources().openR

我正在尝试从res/raw加载文本文件。我看过一些代码片段,并尝试了几种实现方法,但似乎没有一种适合我。我目前正在尝试的代码是

TextView helloTxt = (TextView)findViewById(R.id.hellotxt);
        helloTxt.setText(readTxt());
    }

    private String readTxt() {

     InputStream inputStream = getResources().openRawResource(R.raw.hello);

     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

     int i;
     try {
         i = inputStream.read();
         while (i != -1) {
             byteArrayOutputStream.write(i);
             i = inputStream.read();
         }
         inputStream.close();
     }
     catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     }

     return byteArrayOutputStream.toString();
但它和其他所有公司一样面临着同样的问题

a)
(TextView)findViewById(R.id.hellotxt)
说它贬值了,Eclipses建议迁移代码

b) 无法识别
getResources()
,建议添加一个名为getResources()的方法

最初我想使用assets文件夹,但得到了与b)相同的错误,但使用了getAssets()


这是我正在实现的一个单独的类文件,名为
public class PassGen{}
,目前有一个方法名为
public String returnPass(){}

应该从上下文调用函数getAssets和getResources


如果从活动类中调用它,则不需要前缀,否则需要将上下文传递给需要函数的类,并调用例如context.getAssets()。

应该从上下文调用函数getAssets和getResources

如果从活动类中调用它,则不需要前缀,否则需要将上下文传递给需要函数的类,并调用例如context.getAssets()。

活动类:

public class ReadFileActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Read read = new Read(getApplicationContext());

    TextView helloTxt = (TextView) findViewById(R.id.hellotxt);
    helloTxt.setText(read.readTxt());
}
}
阅读课:

public class Read {

Context ctx;

public Read(Context applicationContext) {
    // TODO Auto-generated constructor stub

    this.ctx = applicationContext;
}


public String readTxt() {

    InputStream inputStream = ctx.getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}
}
活动类别:

public class ReadFileActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Read read = new Read(getApplicationContext());

    TextView helloTxt = (TextView) findViewById(R.id.hellotxt);
    helloTxt.setText(read.readTxt());
}
}
阅读课:

public class Read {

Context ctx;

public Read(Context applicationContext) {
    // TODO Auto-generated constructor stub

    this.ctx = applicationContext;
}


public String readTxt() {

    InputStream inputStream = ctx.getResources().openRawResource(R.raw.hello);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}
}

上面的代码是在安卓2.3.3上运行的。如果是在活动类中,而不是在单独的附加类中,那么在不扩展活动的类上使用ad是什么意思。阅读下面的帖子和评论上面的代码是在安卓2.3.3上运行的。如果在活动类中,而不是在单独的附加类中,它会运行。你所说的类上的广告是指不扩展活动的类。阅读文章底部的部分,并在下面发表评论,那么上下文是什么呢?我查阅了Android开发者网站,但它只是说它返回AssetManager上下文文档的摘要:只需从Activity类传递“this”就可以了。那么上下文是什么呢?我查阅了Android开发者网站,但它只是说它返回AssetManager上下文文档的摘要:只需从Activity类传递“this”就可以了。