创建Android的Activity类的一个对象 [英] Creating an object of Android Activity class

查看:147
本文介绍了创建Android的Activity类的一个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类延伸活动,我想在一个普通的Java类中创建该类的对象,但它抛出了我的异常:

I have a class which extends Activity and I am trying to create an object of that class in a normal java class but it's throwing me an exception :

无法建立处理程序内螺纹已经不叫尺蠖。prepare

我是什么做错了吗?

在此先感谢。

推荐答案

您应该阅读上的应用程序基础的 Android应用程序

You should read up on the application fundamentals of android apps

我想不出一个例子在那里你会需要创建一个活动对象自己。你应该使用Context.startActivity()调用启动的活动。

I cannot think of an example where you would need to create an activity object yourself. you should be using the Context.startActivity() call to start an activity.

不管怎么说,要回答你的问题 - 活动实现了一个消息队列(使用一个处理器),其中信息可以发送到活动的正在运行的线程来执行某些任务。这意味着它执行活动code保持周围等待这些消息(这些消息的一个例子是用户的响应您的应用程序UI)线程。为了做到这一点需要使用一个弯针线其中环(或在某种程度上等待)消息作用于。为您的应用程序的主线程,也使得用户界面为你的应用程序是一个弯针线。

Anyways, to answer your question - an activity implements a message queue (using a Handler) where messages can be sent to the activity's running thread to perform certain tasks. That means the thread which executes Activity code stays around waiting for these messages (an example of these messages are the users' response to your applications UI). In order to do that you need to use a Looper thread which "loops" (or in a way waits) for messages to act on. The main thread for your application which also renders the UI for your application is a looper thread.

如果由于某种原因,你有需要手动创建活动的对象,那么你应该反思一下,你是如何设计应用程序。使用startActivity是所有必需的。

If for some reason you are having the need to create an activity object manually then you should rethink how you are designing your application. Using startActivity is all that is required.

这篇关于创建Android的Activity类的一个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆