FileInputStream在另一个类中?
我正在加载由另一个类通过FileOutputstream方法保存的文件。无论如何,我想在另一个类中加载该文件,但它会给我语法错误或崩溃我的应用程序。FileInputStream在另一个类中?
唯一的教程,我可以找到他们在同一个类中保存和加载文件,但我想加载它在另一个类,并找不到如何解决加载到另一个类的问题。
感谢
我的代码:
public class LogIn extends Activity implements OnClickListener {
EditText eTuser;
EditText eTpassword;
CheckBox StaySignedIn;
Button bSubmit;
String user;
String pass;
FileOutputStream fos;
FileInputStream fis = null;
String FILENAME = "userandpass";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
eTuser = (EditText) findViewById(R.id.eTuser);
eTpassword = (EditText) findViewById(R.id.eTpassword);
StaySignedIn = (CheckBox) findViewById(R.id.Cbstay);
bSubmit = (Button) findViewById(R.id.bLogIn);
bSubmit.setOnClickListener(this);
File file = getBaseContext().getFileStreamPath(FILENAME);
if (file.exists()) {
Intent i = new Intent(LogIn.this, ChatService.class);
startActivity(i);
}
// if if file exist close bracket
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // end of catch bracket
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // end of catch
} // create ends here
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bLogIn:
String user = eTuser.getText().toString();
String pass = eTpassword.getText().toString();
Bundle userandpass = new Bundle();
userandpass.putString("user", user);
userandpass.putString("pass", pass);
Intent login = new Intent(LogIn.this, logincheck.class);
login.putExtra("pass", user);
login.putExtra("user", pass);
startActivity(login);
if (StaySignedIn.isChecked())
;
String userstaysignedin = eTuser.getText().toString();
String passstaysignedin = eTpassword.getText().toString();
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(userstaysignedin.getBytes());
fos.write(passstaysignedin.getBytes());
fos.close();
} catch (IOException e) {
// end of try bracket, before the Catch IOExceptions e.
e.printStackTrace();
} // end of catch bracket
} // switch and case ends here
}// Click ends here
}// main class ends here
B类(类加载数据)
public class ChatService extends Activity {
String collected = null;
FileInputStream fis = null;
String FILENAME;
TextView userandpass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.chatservice);
userandpass = (TextView) findViewById(R.id.textView1);
try {
fis = openFileInput(FILENAME);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] dataArray = null;
try {
dataArray = new byte[fis.available()];
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (fis.read(dataArray) != -1)
;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
{
// while statement
}
userandpass.setText(collected);
}// create ends here
} //类到此为止
的logcat:
03-03 21:03:34.725: E/AndroidRuntime(279): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gta5news.bananaphone/com.gta5news.bananaphone.ChatService}: java.lang.NullPointerException
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.os.Handler.dispatchMessage(Handler.java:99)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.os.Looper.loop(Looper.java:123)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-03 21:03:34.725: E/AndroidRuntime(279): at java.lang.reflect.Method.invokeNative(Native Method)
03-03 21:03:34.725: E/AndroidRuntime(279): at java.lang.reflect.Method.invoke(Method.java:521)
03-03 21:03:34.725: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-03 21:03:34.725: E/AndroidRuntime(279): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-03 21:03:34.725: E/AndroidRuntime(279): at dalvik.system.NativeStart.main(Native Method)
03-03 21:03:34.725: E/AndroidRuntime(279): Caused by: java.lang.NullPointerException
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ContextImpl.makeFilename(ContextImpl.java:1599)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ContextImpl.openFileInput(ContextImpl.java:399)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.content.ContextWrapper.openFileInput(ContextWrapper.java:152)
03-03 21:03:34.725: E/AndroidRuntime(279): at com.gta5news.bananaphone.ChatService.onCreate(ChatService.java:25)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-03 21:03:34.725: E/AndroidRuntime(279): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-03 21:03:34.725: E/AndroidRuntime(279): ... 11 more
ChatService
类中的FILENAME
字符串为空。因此,当您尝试使用fis = openFileInput(FILENAME)
加载文件时,您会得到一个NullPointerException
。
而且,你读的循环扔掉数据:
while (fis.read(dataArray) != -1)
;
它需要收集数据和设置您的collected
字符串的值。
那么,我该如何解决这个错误呢? – TheBlueCat 2012-03-03 21:18:47
将FILENAME设置为有效的文件名字符串,例如'字符串FILENAME =“userandpass”;'就像你在其他课程中一样。否则你的代码怎么知道要加载哪个文件? – DNA 2012-03-03 21:26:36
堆栈跟踪告诉你你需要知道的一切。
错误是一个NullPointerException(意味着您将一个空引用传递给期望非空引用的方法,或者您在空引用上调用方法)。
内部部分Android代码(ContextWrapper.openFileInput()
),其通过您的ChatService.onCreate()
方法调用出现错误,在管线25
的线25是下面的行:
fis = openFileInput(FILENAME);
所以误差明确:FILENAME
为空。在调用此方法之前,您尚未初始化它。
我把我的文件名字符串为空,它仍然崩溃。 – TheBlueCat 2012-03-03 21:16:08
当然。为空是问题所在。因此,请将其设置为正确的值 - 您尝试访问的文件的名称。 – DNA 2012-03-03 21:27:52
我不确定你的程序流,如果你的两个类在同一个线程中运行,但看起来你有程序流问题。您正尝试打开该文件并获取NullPointerException。在尝试阅读之前,确保文件已经创建并且有正确的引用。
如果他们是在单独的线程中运行,那么你可以尝试这样的事:
try {
int waitTries=1;
fis = openFileInput(FILENAME);
while(fis.available()<EXPECTEDSIZE && waitTries++<10)
Tread.sleep(50);
}
如果你知道这个文件应该多大(EXPECTEDSIZE是一些常数,你会设置),那么这可能是什么你正在寻找。
我在哪里可以放这段代码?在login.java类中? – TheBlueCat 2012-03-03 21:19:36
不,在ChatService中,因为那是你正在阅读文件的地方。 while循环的意义在于它强制程序在读取之前等待整个文件的创建。另一种方法是在关闭文件后在loginClass中设置一个布尔标志:doneWriting = true,然后我的while循环可以变成while(!Login.doneWriting) – Thorn 2012-03-03 22:42:45
可能重复[如何在分隔类中加载FileInputStream](http://stackoverflow.com/questions/9548318/how-to-load-fileinputstream-in-a-septate-class) – 2012-03-03 21:00:39
不,它不是,我改变了一些我的代码来使它工作,但它崩溃了。 – TheBlueCat 2012-03-03 21:02:11
*它崩溃*是您可以给出的问题的最穷描述。 Java具有堆栈跟踪,表明问题的性质和位置。阅读。 – 2012-03-03 21:03:44