从其他类调用方法

发布于 2025-01-04 13:59:57 字数 822 浏览 3 评论 0原文

嘿,我需要一些帮助来解释为什么我不能从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

饭团 2025-01-11 13:59:57

问题是,虽然 ClassTwo 扩展了 ClassOne,但您的实例 classTwo 未作为活动正确初始化。它不是视图层次结构的一部分,因此 findViewById 不会返回任何内容。 (您不应该通过调用 new 创建 Activity 子类的实例,并期望任何 Android 方法执行任何有用的操作。)

重新设计您的代码,使 ClassTwo 不再起作用。 Activity 子类并重新设计 changeText() 以接受 Activity 实例作为参数。例如:

package com.game;
import android.widget.TextView;
public class ClassTwo {
    public void changeText(ClassOne activity) {
        TextView textOne = (TextView) activity.findViewById(R.id.textview1);
        textOne.setText("this is how we call methods from other classes");
    }
}

The problem is that while ClassTwo extends ClassOne, your instance classTwo is not initialized properly as an activity. It's not part of the view hierarchy so findViewById will not return anything. (You shouldn't create an instance of an Activity subclass by invoking new and expect any of the Android methods to do anything useful.)

Redesign your code so ClassTwo isn't an Activity subclass and redesign changeText() to accept an Activity instance as an argument. For instance:

package com.game;
import android.widget.TextView;
public class ClassTwo {
    public void changeText(ClassOne activity) {
        TextView textOne = (TextView) activity.findViewById(R.id.textview1);
        textOne.setText("this is how we call methods from other classes");
    }
}
说不完的你爱 2025-01-11 13:59:57

ClassTwoClassOne 的子级。它应该是同一种物体。默认示例是家具。您有一个 Table 类 (classone) 和一个 KitchenTable 类,显然它是 table 的子类。

现在很容易看出厨房餐桌可以做与桌子相同的事情,但您要做的是在桌子类内部调用厨房餐桌代码。这很奇怪,因为并非所有桌子实际上都是厨房桌子。如果桌子是野营桌怎么办?

回到现实世界:如果你想要这个结构,你应该对 classtwo self 中的 classTwo 进行特定的所有调用。如果你不这样做,那么你可能想要一些不以这种扩展方式重新发布的东西:记住,如果你执行new ClassTwo,你正在创建一个新的activity!这个活动是从classtwo到calssone的点,它有一个classtwo,等等。

你可能需要重新定义你的类结构。找出你想要什么,为什么想要它等等。

ClassTwo is a child of ClassOne. It is supposed to be the same kind of object. Default example would be furniture. You have a Table class (classone) and a KitchenTable 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 do new ClassTwo, you are making a new activity! 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.

香橙ぽ 2025-01-11 13:59:57

这是我的代码问题:

  • ClassTwo extends ClassOne
    [不应该,特别是当您在 ClassOne 中创建 ClassTwo 的实例时]

要从 ClassOne 调用方法,您可以使用“super”关键字。例如,

 class ClassOne{
   public void myMethod() {
      // do stuff
   }
}

class ClassTwo extends ClassOne{
   @Override public void myMethod() {
      super.myMethod(); //call myMethod from ClassOne
      // add more stuff
   }
}

Here's my problem with your code:

  • ClassTwo extends ClassOne
    [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,

 class ClassOne{
   public void myMethod() {
      // do stuff
   }
}

class ClassTwo extends ClassOne{
   @Override public void myMethod() {
      super.myMethod(); //call myMethod from ClassOne
      // add more stuff
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文