-4

ここに方法があります

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 を A にインポートし、メソッドを次のように呼び出します。

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

実行時に、プログラムは

FileInputStream fis = openFileInput(filename); in PunchActivities 

しかし、NullPointer エラーでクラス A に戻ります。どうして?????アンドロイドのルールを知っている人はいますか??

4

4 に答える 4

0

問題は次の行にあると思います。

      punchit.setImageResource(R.drawable.punchboxcancelled);

このクラスでは、以前のクラス(onCreate()のsetContentView()メソッド)と同じxmlを設定していないためです。したがって、このビューはここでは見つかりません。

于 2012-07-12T05:06:12.220 に答える
0

関数のパラメーターとしてContextを渡し、を使用しますcontext.openFileInput(fileName)。そして、同じことがopenFileOutput(fileName)

お役に立てば幸いです。

于 2012-07-12T05:06:28.193 に答える
0

ファイルへのパスを渡す必要があることがわかりました。

于 2012-09-18T20:57:23.817 に答える
0

「FileInputStream fis = openFileInput(filename); in PunchActivities」の代わりに、次のようにしたほうがよいでしょう。

これを試して

FileInputStream fis =new FileInputStream(filename);
于 2012-07-12T05:12:08.103 に答える