问题描述
这是获取 NullPointerException 错误的代码:
Here is the code that is getting the NullPointerException error:
InputStream is = getAssets().open("twentyone.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);
可能出了什么问题?
*printStackTrace
* the printStackTrace
03-19 18:20:18.662: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
编辑 2:代码直到异常:
Edit 2: Code till the exception:
public class ListViewAa3 extends ListViewA{
public String[] process(String cti)throws IOException{
String ctid=cti;
Log.d("Outside try invoked","tag1");
try{
Log.d("beginning of try invoked","tag2");
try{
InputStream is = getAssets().open("USCOUNTIES.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);}catch(Exception e){e.printStackTrace();}
推荐答案
好的,我明白了.我必须将主要活动的上下文传递给这个类,然后使用 context.getAssets.open("twentyone.txt");
OK, I got it. I had to pass the context of the main activity to this class, then use context.getAssets.open("twentyone.txt");
对于有同样问题的任何人,请执行以下操作:将其放在具有活动的类的 onCreate 函数中:Context context=getApplicationContext();
To anyone who has the same problem, do this:Put this in the onCreate function of the class with activity: Context context=getApplicationContext();
将上下文传递给新类的函数(在我的例子中是process(String a,Context context)")然后在进程函数中输入:
Pass context to the new class's function(in my case "process(String a,Context context)")Then type this in the process function:
InputStream is = context.getAssets().open("twentyone.txt");
我花了 4 个小时才弄清楚这么愚蠢的事情.
Took me 4 hours to figure such a silly thing out.
这篇关于InputStream 返回 NullPointerException (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!