以下是出现 NullPointerException 错误的代码:
InputStream is = getAssets().open("twentyone.txt");
InputStreamReader iz=new InputStreamReader(is);
BufferedReader br = new BufferedReader(iz);
可能出了什么问题?
*编辑:printStackTrace
03-19 18:20:18.662: E/AndroidRuntime(929): Caused by: java.lang.NullPointerException
编辑2:代码直到异常:
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");
对于遇到同样问题的任何人,请执行以下操作:
将其放入具有 Activity 的类的 onCreate 函数中:Context context=getApplicationContext();
将上下文传递给新类的函数(在我的例子中为“process(String a,Context context)”)
然后在处理函数中输入:
InputStream is = context.getAssets().open("twentyone.txt");
我花了4个小时才弄清楚这么愚蠢的事情。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)