gpt4 book ai didi

java - 找不到符号方法 openFileInput(String)

转载 作者:行者123 更新时间:2023-12-01 22:07:49 26 4
gpt4 key购买 nike

我有这个代码:

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)

相同的代码在其他 Activity 中运行,但这个 ↑ 是类。

如果我从 Activity(play.java) 读取变量

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

我只能读取一次,因为indexG2被声明为private final static integer indexG2;

最佳答案

openFileInput()Context 上的一个方法。您从 View 继承,而 View 又不从 Context 继承。使用 getContext() 检索 Context(),因此您的调用变为 getContext().openFileInput()

关于java - 找不到符号方法 openFileInput(String),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32380421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com