从其他类调用方法
嘿,我需要一些帮助来解释为什么我不能从 ClassTwo 调用changeText() 这里是 classOne
public class ClassOne extends Activity {
/** Called when the activity is first created. */
ClassTwo classTwo = new ClassTwo();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
classTwo.changeText();
}
}
,这里是 classTwo
public class ClassTwo extends ClassOne {
public void changeText(){
TextView textOne = (TextView) findViewById(R.id.textview1);
textOne.setText("this is how we call methods from other classes");
}
}
错误所在
com.game.ClassTwo.<init>(ClassTwo.java:5)
com.game.ClassTwo.<init>(ClassOne.java:10)
hey i need some help with why i carnt call changeText() from ClassTwo
here is classOne
public class ClassOne extends Activity {
/** Called when the activity is first created. */
ClassTwo classTwo = new ClassTwo();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
classTwo.changeText();
}
}
and here is classTwo
public class ClassTwo extends ClassOne {
public void changeText(){
TextView textOne = (TextView) findViewById(R.id.textview1);
textOne.setText("this is how we call methods from other classes");
}
}
the errors are at
com.game.ClassTwo.<init>(ClassTwo.java:5)
com.game.ClassTwo.<init>(ClassOne.java:10)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,虽然
ClassTwo
扩展了ClassOne
,但您的实例classTwo
未作为活动正确初始化。它不是视图层次结构的一部分,因此 findViewById 不会返回任何内容。 (您不应该通过调用new
创建 Activity 子类的实例,并期望任何 Android 方法执行任何有用的操作。)重新设计您的代码,使
ClassTwo
不再起作用。 Activity 子类并重新设计changeText()
以接受Activity
实例作为参数。例如:The problem is that while
ClassTwo
extendsClassOne
, your instanceclassTwo
is not initialized properly as an activity. It's not part of the view hierarchy sofindViewById
will not return anything. (You shouldn't create an instance of an Activity subclass by invokingnew
and expect any of the Android methods to do anything useful.)Redesign your code so
ClassTwo
isn't an Activity subclass and redesignchangeText()
to accept anActivity
instance as an argument. For instance:ClassTwo
是ClassOne
的子级。它应该是同一种物体。默认示例是家具。您有一个Table
类 (classone
) 和一个KitchenTable
类,显然它是 table 的子类。现在很容易看出厨房餐桌可以做与桌子相同的事情,但您要做的是在桌子类内部调用厨房餐桌代码。这很奇怪,因为并非所有桌子实际上都是厨房桌子。如果桌子是野营桌怎么办?
回到现实世界:如果你想要这个结构,你应该对 classtwo self 中的
classTwo
进行特定的所有调用。如果你不这样做,那么你可能想要一些不以这种扩展方式重新发布的东西:记住,如果你执行new ClassTwo
,你正在创建一个新的activity
!这个活动是从classtwo到calssone的点,它有一个classtwo,等等。你可能需要重新定义你的类结构。找出你想要什么,为什么想要它等等。
ClassTwo
is a child ofClassOne
. It is supposed to be the same kind of object. Default example would be furniture. You have aTable
class (classone
) and aKitchenTable
class, that is a child of table obviously.Now it is easy to see that a kitchentable can do the same stuff a table can do, but what you are trying to do is call kitchentable code inside of the table class. This is strange, because not all tables are in fact kitchentables. What if the table would be a camping-table?
SO back to the real world: If you want this structure, you should do all calling specific to things
classTwo
in classtwo self. If you don't then you might want something that is not releted in this extends way: remember, if you donew ClassTwo
, you are making a newactivity
! This activity is points from classtwo to calssone, which has a classtwo, etc etc.You probably need to redefine your class structure. Find out what you want, why you want it, etc.
这是我的代码问题:
[不应该,特别是当您在 ClassOne 中创建 ClassTwo 的实例时]
要从 ClassOne 调用方法,您可以使用“super”关键字。例如,
Here's my problem with your code:
[it shouldn't, especially if you are making an instance of ClassTwo in ClassOne]
To call a method from ClassOne, you would use the "super" keyword. For example,