第二个活动在按下主活动中的按钮时调用:
public class Graphics extends ActionBarActivity {
DrawTheField v;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new DrawTheField(this);
setContentView(v);
}这是Graphics类:
public class DrawTheField extends View {
String FILEballpossession = "ballpossession.txt";
public DrawTheField(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// graphics stuff here
FileInputStream fins = null;
try {
fins = openFileInput(FILEballpossession);
byte[] reader = new byte[fins.available()];
while (fins.read(reader) != -1) {
ball_possession = new String(reader);
}
if (fins != null) {
fins.close();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}文件ballpossession.txt是在主活动java中创建的。openFileInput方法在Activity中可用,但在此DrawTheField类中不可用,因为它恰好是一个视图。如何为这个类“扩展视图”和“扩展活动”?
发布于 2014-09-03 05:06:59
你不会的。Java不支持多重继承。
http://javapapers.com/core-java/why-multiple-inheritance-is-not-supported-in-java/
JAVA省略了C++的许多很少使用、理解不多、令人困惑的特性,在我们的经验中,这些特性带来的痛苦要比有益的fi更多。这主要包括运算符重载(尽管它确实有方法重载)、多重继承和广泛的自动强制。
发布于 2014-09-03 16:01:15
只需将扩展View的类作为扩展Activity的类中的内部类,或者如果您想将两个类文件分开,然后将扩展activity的类作为对象传递给扩展view的类。
我还意识到,您读取一个字符串,然后在您的活动中读取文件,并将字符串传递给视图类。
https://stackoverflow.com/questions/25632299
复制相似问题