我有这段代码:
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) {
}
}
}
出现此错误
错误:(109,35)错误:找不到符号方法openFileInput(String)
在其他活动中运行的代码相同,但这个↑是类。
如果我从活动中读取变量(play.java)
gifInputStream = context.getResources().openRawResource(images[Play.indexG2])
我只能阅读一次因为indexG2被声明为private final static integer indexG2;
答案 0 :(得分:3)
openFileInput()
是Context
上的一种方法。您继承自View
,而Context
又未继承自getContext()
。使用Context()
检索getContext().openFileInput()
,以便您的通话变为commandText="AddActivityLog"
commandType="StoredProcedure"
。