Parad0X
Parad0X

Reputation: 3664

Cannot find symbol method openFileInput(String)

I have this code:

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) {

        }

    }


}

with this error

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

The same code running in other activities, but this ↑ is class.

If I read variable from activity(play.java)

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

I can read ONLY once because indexG2 is declared private final static integer indexG2;

Upvotes: 0

Views: 1484

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006819

openFileInput() is a method on Context. You are inheriting from View, which in turn does not inherit from Context. Use getContext() to retrieve a Context(), so your call becomes getContext().openFileInput().

Upvotes: 3

Related Questions