如何在android中实例化一个类,它也是一个活动?

发布于 2024-12-22 05:14:44 字数 1376 浏览 0 评论 0原文

在android中,如何为一个创建一个构造函数,它也是一个Activity

我的问题是,

我想要两个活动类(estimateFare 和 Mapping),它们都将数据发送到活动类 (CalculateFare)。映射从实时网络信息中获取数据,而estimateFare从用户输入中获取数据。我希望 CalculateFare 能够使用任一类中的数据来执行计算,尽管我在使用 onCreate 方法和 CalculateFare 构造函数时遇到了麻烦。当在应用程序运行时调用该类时,我在 LogCat 中收到以下消息;

Unable to instantiate activity ComponentInfo. 

CalculateFare 的一个片段如下;

    public class CalculateFare extends Activity {
        TextView t;
        static long length;
        static int dist;
        static int mMonth;
        static int mDay;
        public static double fare;
        double rate1 = 0, rate2a, rate2b, rate3, rate4a, rate4b;
        int remDist = 0;
        double remLength;
        private static int TTS_DATA_CHECK = 1;
        private TextToSpeech tts = null;
        private boolean ttsIsInit = false;



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fare);

}

public CalculateFare(long length, int dist, int mMonth, int mDay)
{
}

public static void setLength(long l)
{
    length = l;
}

public static void setDist(int d)
{
    dist = d;
}

public static void setDate(int m, int d)
{
    mMonth = m;
    mDay = d;
}

public void calc()
{

任何建议非常感谢

In android, how can I create a constructor for a class which is also an Activity?

My problem is,

I want to have two activity classes (estimateFare and Mapping) which both send data to a an activity class (CalculateFare). Mapping takes the data from real time network info, whereas estimateFare takes data from user input. I want CalculateFare to be able to use the data from either class to perform the calculation, although I have run into trouble with the onCreate method and the constructor for CalculateFare. When the class is called during runtime of the application I get the following message in the LogCat;

Unable to instantiate activity ComponentInfo. 

A snippet from CalculateFare is as follows;

    public class CalculateFare extends Activity {
        TextView t;
        static long length;
        static int dist;
        static int mMonth;
        static int mDay;
        public static double fare;
        double rate1 = 0, rate2a, rate2b, rate3, rate4a, rate4b;
        int remDist = 0;
        double remLength;
        private static int TTS_DATA_CHECK = 1;
        private TextToSpeech tts = null;
        private boolean ttsIsInit = false;



public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fare);

}

public CalculateFare(long length, int dist, int mMonth, int mDay)
{
}

public static void setLength(long l)
{
    length = l;
}

public static void setDist(int d)
{
    dist = d;
}

public static void setDate(int m, int d)
{
    mMonth = m;
    mDay = d;
}

public void calc()
{

Any Advice much appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

风向决定发型 2024-12-29 05:14:44

将数据移动到一个单独的类中,该类根本不是活动,而是在活动之间共享。数据可以被构造为来自用户的数据和来自网络的数据。例如,请参阅此线程在活动之间共享数据的方法。

当框架在需要活动时创建另一个实例时,实例化恰好是活动的CalculateFare 对象将无济于事。

Move your data into a separate class that isn't an activity at all but is shared between activities. The data can be structured into those data that come from the user and those that come from the network. See, for example, this thread for approaches to sharing data between activities.

Instantiating a CalculateFare object that happens to be an activity won't help when the framework creates another instance when it needs an Activity.

晨光如昨 2024-12-29 05:14:44

在android中,如何为一个同时也是Activity的类创建构造函数?

你不知道。

我想要两个活动类(estimateFare 和 Mapping),它们都将数据发送到活动类 (CalculateFare)。

除了通过 startActivity() 之外,这些类都不会相互接触。通过 Intent extras 或通过中央数据模型(例如数据库)在活动之间传递数据。

当在应用程序运行时调用该类时,我在 LogCat 中收到以下消息

这是因为您需要删除构造函数。

In android, how can I create a constructor for a class which is also an Activity?

You don't.

I want to have two activity classes (estimateFare and Mapping) which both send data to a an activity class (CalculateFare).

None of those classes will be touching each other except via startActivity(). Pass data between activities via Intent extras or via a central data model (e.g., database).

When the class is called during runtime of the application I get the following message in the LogCat

That is because you need to delete your constructor.

川水往事 2024-12-29 05:14:44

活动不能有用户定义的构造函数,只能有默认的构造函数;它们只能通过意图创建间接实例化。如果需要在 Activity 之间传递数据,可以通过将 extras 放入 Bundle 中来实现,例如:

bundle.putInt("dist",dist);

然后可以

int dist = bundle.getInt("dist");

在启动 Activity 之前通过 Put extras 从 Bundle 中提取数据:

Intent i = new Intent(this, CalculateFare.class);
Bundle b = new Bundle();

b.putInt("dist",dist);
i.putExtras(b);
startActivity(i);

然后在 onCreate 方法中读取 extras:

public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);

   Bundle b = getIntent().getExtras();
   int dist;

   if (b != null)
   {
      dist = b.getInt("dist");
   }
}

Activities can't have user defined constructors, only the default one; they can be instantiated only indirectly via intent creation. If you need to pass data between activities, do it by putting extras to bundles, for example:

bundle.putInt("dist",dist);

then you can extract the data from bundle by

int dist = bundle.getInt("dist");

Put extras before starting the activity:

Intent i = new Intent(this, CalculateFare.class);
Bundle b = new Bundle();

b.putInt("dist",dist);
i.putExtras(b);
startActivity(i);

and then read the extras in the onCreate method:

public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);

   Bundle b = getIntent().getExtras();
   int dist;

   if (b != null)
   {
      dist = b.getInt("dist");
   }
}
无妨# 2024-12-29 05:14:44

我尝试在其内部实例化 Activity 以将其传递给另一个类的方法。我遇到了内存不足错误。所以我在 Activity 本身内部拥有该方法并将其作为该对象传递。

I tried to instantiate Activity inside itself to pass it to a method of another class. I got OutOfMemoryError. So I had that method inside Activity itself and passed it as this object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文