如何在android中实例化一个类,它也是一个活动?
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将数据移动到一个单独的类中,该类根本不是活动,而是在活动之间共享。数据可以被构造为来自用户的数据和来自网络的数据。例如,请参阅此线程在活动之间共享数据的方法。
当框架在需要活动时创建另一个实例时,实例化恰好是活动的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.
你不知道。
除了通过
startActivity()
之外,这些类都不会相互接触。通过 Intent extras 或通过中央数据模型(例如数据库)在活动之间传递数据。这是因为您需要删除构造函数。
You don't.
None of those classes will be touching each other except via
startActivity()
. Pass data between activities viaIntent
extras or via a central data model (e.g., database).That is because you need to delete your constructor.
活动不能有用户定义的构造函数,只能有默认的构造函数;它们只能通过意图创建间接实例化。如果需要在 Activity 之间传递数据,可以通过将 extras 放入 Bundle 中来实现,例如:
然后可以
在启动 Activity 之前通过 Put extras 从 Bundle 中提取数据:
然后在 onCreate 方法中读取 extras:
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:
then you can extract the data from bundle by
Put extras before starting the activity:
and then read the extras in the onCreate method:
我尝试在其内部实例化 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.