2012-07-12 33 views
-4

這裏是方法我在類A中調用一個方法,並從它的類中調用它。但是,如果我稱之爲B類同樣的方法,我得到錯誤

public String testFile(int numofpunches, String filename, int picture, int cancelpix){ 
    String string=""; 
    Boolean a; 
    FILENAME = filename; 
    try { 
     FileInputStream fis = openFileInput(filename); 
     try{ 
      int counter = fis.read(mIds, 0,numofpunches); 
      fis.close(); 
      int i=0; 
      while(i <counter){ 
       if(mIds[i] == 'c'){ 
        mThumbIds[i++] = picture; //R.drawable.crab; 
        string=string.concat("c"); 

       }else{ 

        mThumbIds[i] = cancelpix; 
        string=string.concat("x"); 


        ImageView punchit = (ImageView) 
findViewById(mypunches[i++]); 

punchit.setImageResource(R.drawable.punchboxcancelled); 




       } 
      } 
      String testfordone = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
      a= string.equals(testfordone.substring(0,numofpunches)); 
      if(a){ 
       return "done"; 
      } 
     }catch (IOException e){ 


     } 
    }catch (FileNotFoundException e) { 

     try {//initialize 
      String FILENAME = filename; 
      FileOutputStream fos = openFileOutput(FILENAME, 
      Context.MODE_PRIVATE); 
      string = "cccccccccccccccccccccccccccccccccccccc"; 
      string.substring(0,numofpunches); 

      try { 
       fos.write(string.getBytes()); 
       fos.close(); 


       int counter = 0; 
       while(counter < numofpunches){ 

        mThumbIds[counter++] = picture; 

       } 
      }catch(IOException err){ 

      } 

     }catch(FileNotFoundException error){ 


     }; 

    } 

我把它從那裏這個方法位於同一個類(A)。 現在我用相同的方法創建一個新類(PunchActivities)。在一個 通話 進口類PunchActivities的方法

PunchActivities pa = new PunchActivities(); 
String result = pa.testFile(all the required parameters); 

在運行該程序到達

FileInputStream fis = openFileInput(filename); in PunchActivities 

但隨後回來A類與空指針錯誤。 爲什麼?????任何人都知道android的規則?

回答

0

我認爲這個問題是在這一行:

 punchit.setImageResource(R.drawable.punchboxcancelled); 

因爲在這個類中,您沒有設置相同的XML作爲早期類(在的onCreate中的setContentView()方法() )。所以這個觀點在這裏找不到。

+0

指出的錯誤是在openFileInput(...)之前在 punchit.setImageResource(R.drawable.punchboxcancelled)之前調用的。 – 2012-07-12 05:08:57

+0

我發現我需要將路徑傳遞給文件。 – murli 2012-09-18 20:57:13

0

將Context作爲參數傳遞給函數並使用context.openFileInput(fileName)。和openFileOutput(fileName)一樣。

我希望這會有所幫助。

0

更好ü以下,而不是 「的FileInputStream FIS = openFileInput(文件名);在PunchActivities,」

試試這個

FileInputStream fis =new FileInputStream(filename); 
0

我發現我需要的路徑傳遞給文件。

+0

如果這是解決方案,請單擊帖子分數下方的空複選框以接受它。 – jcw 2012-11-13 18:33:35

相關問題