简体   繁体   English

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

[英]How do I instantiate a class in android which is also an activity?

In android, how can I create a constructor for a class which is also an Activity ? 在android中,如何为也是Activityclass创建constructor

My problem is, 我的问题是

I want to have two activity classes (estimateFare and Mapping) which both send data to a an activity class ( CalculateFare ). 我想要两个活动类(estimateFare和Mapping),它们都将数据发送到活动类( CalculateFare )。 Mapping takes the data from real time network info, whereas estimateFare takes data from user input. 映射从实时网络信息中获取数据,而estimateFare从用户输入中获取数据。 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 . 我希望CalculateFare能够使用任何一个类中的数据来执行计算,尽管我在onCreate方法和CalculateFare的构造函数方面遇到了麻烦。 When the class is called during runtime of the application I get the following message in the LogCat ; 当在应用程序运行时调用该类时,我在LogCat收到以下消息;

Unable to instantiate activity ComponentInfo. 

A snippet from CalculateFare is as follows; 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()
{

Any Advice much appreciated 任何建议,不胜感激

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. 当框架需要活动时创建另一个实例时,实例化恰好是活动的CalculateFare对象将无济于事。

In android, how can I create a constructor for a class which is also an Activity? 在android中,如何为同时也是Activity的类创建构造函数?

You don't. 你不知道

I want to have two activity classes (estimateFare and Mapping) which both send data to a an activity class (CalculateFare). 我想要两个活动类(estimateFare和Mapping),它们都将数据发送到活动类(CalculateFare)。

None of those classes will be touching each other except via startActivity() . 除了通过startActivity()之外,所有这些类都不会互相影响。 Pass data between activities via Intent extras or via a central data model (eg, database). 通过Intent Extras或通过中央数据模型(例如数据库)在活动之间传递数据。

When the class is called during runtime of the application I get the following message in the LogCat 在应用程序运行时调用该类时,我在LogCat中收到以下消息

That is because you need to delete your constructor. 那是因为您需要删除构造函数。

I tried to instantiate Activity inside itself to pass it to a method of another class. 我试图实例化其内部的Activity,以将其传递给另一个类的方法。 I got OutOfMemoryError. 我收到了OutOfMemoryError。 So I had that method inside Activity itself and passed it as this object. 因此,我在Activity本身内部具有该方法,并将其作为该对象传递。

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: 然后阅读onCreate方法中的其他内容:

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM