java 创建 Android Activity 类的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1215875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an object of Android Activity class
提问by Sam97305421562
I have a class which extends Activityand I am trying to create an object of that class in a normal java class but it's throwing me an exception :
我有一个扩展的类,Activity我试图在普通的 java 类中创建该类的对象,但它抛出了一个异常:
Can't create handler inside thread that has not called looper.prepare
Can't create handler inside thread that has not called looper.prepare
What am I doing wrong?
我究竟做错了什么?
Thanks in advance.
提前致谢。
回答by Prashast
You should read up on the application fundamentalsof android apps
您应该阅读Android 应用程序的应用程序基础知识
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.
我想不出一个你需要自己创建一个活动对象的例子。您应该使用 Context.startActivity() 调用来启动活动。
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.
无论如何,要回答您的问题 - 活动实现了一个消息队列(使用处理程序),其中可以将消息发送到活动的运行线程以执行某些任务。这意味着执行 Activity 代码的线程会一直等待这些消息(这些消息的一个例子是用户对您的应用程序 UI 的响应)。为了做到这一点,您需要使用一个 Looper 线程,它“循环”(或以某种方式等待)消息来执行操作。您的应用程序的主线程也为您的应用程序呈现 UI,它是一个 Looper 线程。
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.
如果由于某种原因您需要手动创建活动对象,那么您应该重新考虑如何设计您的应用程序。只需要使用 startActivity 即可。
回答by Robert Harvey
The handler runs in whatever thread created it. So if you're not creating the instance of the new class in the UI thread then the handler isn't running in the UI thread and you will have a problem.
处理程序在创建它的任何线程中运行。因此,如果您没有在 UI 线程中创建新类的实例,则处理程序不会在 UI 线程中运行,您就会遇到问题。
I once tried to inflate GUIs in a separate thread for performance reasons. I didn't touch any Window at that point, but when inflating I got the same error message and I just ran Looper.prepare() in my Thread and all was well.
出于性能原因,我曾经尝试在单独的线程中扩充 GUI。那时我没有触摸任何窗口,但是在充气时我收到了相同的错误消息,我只是在我的线程中运行了 Looper.prepare() 并且一切都很好。
A Looper runs the message loop of a thread. If you don't call Looper.prepare() (and then Looper.loop()) in a thread, that thread won't have a message loop, so can't have Handler objects that accept messages.
Looper 运行线程的消息循环。如果您不在线程中调用 Looper.prepare()(然后调用 Looper.loop()),则该线程不会有消息循环,因此不能有接受消息的 Handler 对象。

