2015-09-03 50 views
0

我有這樣的代碼:找不到符號法openFileInput(字符串)

package com.example.android.game; 


import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Movie; 
import android.os.SystemClock; 
import android.util.AttributeSet; 
import android.view.View; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 


public class GifView extends View{ 

    private final static String STORETEXT6="index2.txt"; 

    public static int indexGW; 
    public static int indexGW2; 
    public static int indexG2; 


    int[] images = {R.drawable.ihla1, 
     R.drawable.kockaa1, 
     R.drawable.kruha1 
    }; 

    private InputStream gifInputStream; 
    private Movie gifMovie; 
    private int movieWidth, movieHeight; 
    private long movieDuration; 
    private long movieStart; 
    private Integer index2; 

    public GifView(Context context) { 
     super(context); 
     init(context); 
    } 


    public GifView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public GifView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context); 
    } 

    private void init(Context context) { 
     setFocusable(true); 


     read(); 

     gifInputStream = context.getResources().openRawResource(images[indexG2]); 


     gifMovie = Movie.decodeStream(gifInputStream); 
     movieWidth = gifMovie.width(); 
     movieHeight = gifMovie.height(); 
     movieDuration = gifMovie.duration(); 
    } 






    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     setMeasuredDimension(movieWidth, movieHeight); 
    } 



    @Override 
    protected void onDraw(Canvas canvas) { 

     long now = SystemClock.uptimeMillis(); 

     if(movieStart == 0) { 
      movieStart = now; 
     } 

     if(gifMovie != null) { 

      int dur = gifMovie.duration(); 
      if(dur == 0) { 
       dur = 1000; 
      } 

      int relTime = (int)((now - movieStart) % dur); 

      gifMovie.setTime(relTime); 

      gifMovie.draw(canvas, 0, 0); 
      invalidate(); 
     } 
    } 

    public void read() { 

     try { 

      InputStream in3 = openFileInput(STORETEXT6); 

      if (in3 != null) { 

       InputStreamReader tmp = new InputStreamReader(in3); 

       BufferedReader reader = new BufferedReader(tmp); 

       String str; 

       StringBuilder buf3 = new StringBuilder(); 

       while ((str = reader.readLine()) != null) { 

        buf3.append(str); 

       } 

       in3.close(); 

       indexG2=Integer.valueOf(buf3.toString().trim()); 



      } 

     } catch (java.io.FileNotFoundException e) { 

       // that's OK, we probably haven't created it yet 

     } catch (Throwable t) { 

     } 

    } 


} 

與此錯誤

Error:(109, 35) error: cannot find symbol method openFileInput(String)

在其他活動上運行相同的代碼,但是這是↑類。

如果我讀活動(play.java)可變

gifInputStream = context.getResources().openRawResource(images[Play.indexG2]) 

因爲indexG2聲明private final static integer indexG2;

+0

謝謝你正在運行,但仍然只打一次電話或結束通話。爲什麼?我在Play.java中稱這個類爲...↓ GifView gifView =(GifView)findViewById(R.id.gif_view); 我需要重複調​​用帶變量的gifView(定時器或onClick事件) – Parad0X

回答

3

openFileInput()我只能讀一次是Context的方法。您從View繼承,而後者並不從Context繼承。使用getContext()檢索Context(),因此您的電話變爲getContext().openFileInput()

+0

謝謝你正在運行,但仍然只打一次呼叫或結束呼叫。 爲什麼?我調用這個類的Play.java ...↓GifView gifView =(GifView)findViewById(R.id.gif_view);我需要重複調​​用帶有變量的gifView(Timer) – Parad0X