2012-03-03 134 views
0

我正在加載由另一個類通過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 
+0

可能重複[如何在分隔類中加載FileInputStream](http://stackoverflow.com/questions/9548318/how-to-load-fileinputstream-in-a-septate-class) – 2012-03-03 21:00:39

+0

不,它不是,我改變了一些我的代碼來使它工作,但它崩潰了。 – TheBlueCat 2012-03-03 21:02:11

+0

*它崩潰*是您可以給出的問題的最窮描述。 Java具有堆棧跟蹤,表明問題的性質和位置。閱讀。 – 2012-03-03 21:03:44

回答

1

ChatService類中的FILENAME字符串爲空。因此,當您嘗試使用fis = openFileInput(FILENAME)加載文件時,您會得到一個NullPointerException

而且,你讀的循環扔掉數據:

while (fis.read(dataArray) != -1) 
     ; 

它需要收集數據和設置您的collected字符串的值。

+0

那麼,我該如何解決這個錯誤呢? – TheBlueCat 2012-03-03 21:18:47

+0

將FILENAME設置爲有效的文件名字符串,例如'字符串FILENAME =「userandpass」;'就像你在其他課程中一樣。否則你的代碼怎麼知道要加載哪個文件? – DNA 2012-03-03 21:26:36

1

堆棧跟蹤告訴你你需要知道的一切。

錯誤是一個NullPointerException(意味着您將一個空引用傳遞給期望非空引用的方法,或者您在空引用上調用方法)。

內部部分Android代碼(ContextWrapper.openFileInput()),其通過您的ChatService.onCreate()方法調用出現錯誤,在管線25

的線25是下面的行:

fis = openFileInput(FILENAME); 

所以誤差明確:FILENAME爲空。在調用此方法之前,您尚未初始化它。

+0

我把我的文件名字符串爲空,它仍然崩潰。 – TheBlueCat 2012-03-03 21:16:08

+1

當然。爲空是問題所在。因此,請將其設置爲正確的值 - 您嘗試訪問的文件的名稱。 – DNA 2012-03-03 21:27:52

1

我不確定你的程序流,如果你的兩個類在同一個線程中運行,但看起來你有程序流問題。您正嘗試打開該文件並獲取NullPointerException。在嘗試閱讀之前,確保文件已經創建並且有正確的引用。

如果他們是在單獨的線程中運行,那麼你可以嘗試這樣的事:

try { 
    int waitTries=1; 
    fis = openFileInput(FILENAME); 
    while(fis.available()<EXPECTEDSIZE && waitTries++<10) 
     Tread.sleep(50); 
} 

如果你知道這個文件應該多大(EXPECTEDSIZE是一些常數,你會設置),那麼這可能是什麼你正在尋找。

+0

我在哪裏可以放這段代碼?在login.java類中? – TheBlueCat 2012-03-03 21:19:36

+0

不,在ChatService中,因爲那是你正在閱讀文件的地方。 while循環的意義在於它強制程序在讀取之前等待整個文件的創建。另一種方法是在關閉文件後在loginClass中設置一個布爾標誌:doneWriting = true,然後我的while循環可以變成while(!Login.doneWriting) – Thorn 2012-03-03 22:42:45