我正在做的正是这样的:
我有一个“ Welcome”类,它调用另一个包含“ write”方法的类“ textWriter”。 Welcome将两个字符串传递给“ write”,“ name”是要写入的文本文件的名称,“ something”是要打印的东西。
我在prt = new PrintStream(output);
上不断收到空指针错误
这是logcat
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): FATAL EXCEPTION: main
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): java.lang.NullPointerException
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.textWriter.write(textWriter.java:24)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.Welcome.setSubChoices(Welcome.java:37)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.rhobot.budget.Welcome$1.onClick(Welcome.java:76)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.view.View.performClick(View.java:2408)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.view.View$PerformClick.run(View.java:8816)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Handler.handleCallback(Handler.java:587)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.os.Looper.loop(Looper.java:123)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at java.lang.reflect.Method.invoke(Method.java:521)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-22 16:40:22.561: ERROR/AndroidRuntime(2245): at dalvik.system.NativeStart.main(Native Method)
这是欢迎类:
public class Welcome extends Activity
{
P p = new P(); //shared prefs helper
loadArray la = new loadArray(); //array loader
textWriter tw = new textWriter();
private EditText txtBalance;
private Button btnEnter;
int subItemCount, addItemCount;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.welcomescreen);
capture();
}
public static String sub_choice_key = "sub_choice_key.txt";
public static String sub_compare = "sub_compare.txt";
public static String add_choice_key = "add_choice_key.txt";
public static String add_compare = "add_compare.txt";
public void setSubChoices() throws FileNotFoundException {
// TODO create a master list of choices
tw.write(sub_choice_key,"-1");
tw.write(sub_choice_key,"-2");
tw.write(sub_choice_key,"-3");
tw.write(sub_compare,"-1");
tw.write(sub_compare,"-2");
tw.write(sub_compare,"-3");
p.pisI("subItemCount", 3, this);
}...
这是textWriter类
public class textWriter extends Activity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
public void write(String name, String something) {
try {
FileOutputStream output;
output = openFileOutput(name,MODE_APPEND);
PrintStream prt;
prt = new PrintStream(output);
prt.println(something);
prt.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我想强调一下,这些也是两个不同的文件,我正在导入所有我相信的正确内容。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在我的Android清单中。
我究竟做错了什么?是背景吗?还是缺乏?如果是这样,我应该如何实施呢?
我这样做是因为我想在其他程序中使用此textWrite作为写入数据的简便(少混乱)方式。如果我这样做,它可以为我节省大约50行代码。
提前致谢-
技术学
最佳答案
就像凯文说的那样,问题在于缺少有效的上下文。
如果希望您的类结构保持不变,可以做的一个简单修复是从extends Activity
中删除textWriter
,而是在构造函数中传递Context,即:
public class textWriter{
private Context m_context;
public textWriter(Context ctx) {
m_context = ctx;
}
public void write(String name, String something) {
try {
FileOutputStream output;
output = m_context.openFileOutput(name,MODE_APPEND);
PrintStream prt;
prt = new PrintStream(output);
prt.println(something);
prt.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后,您需要像这样构造它:
textWriter tw = new textWriter(this);