Android – 不能创build处理程序内部没有调用Looper.prepare()的线程

我正在开发一个简单的应用程序,使用谷歌地图V2 API只是为了获得基本知识,我面临这个错误:

09-09 21:21:41.154: E/AndroidRuntime(3796): FATAL EXCEPTION: Thread-441 09-09 21:21:41.154: E/AndroidRuntime(3796): java.lang.ExceptionInInitializerError 09-09 21:21:41.154: E/AndroidRuntime(3796): at cz.vongrad.dataSourceLayer.DataMapper.loadLocation(DataMapper.java:111) 09-09 21:21:41.154: E/AndroidRuntime(3796): at cz.vongrad.dataSourceLayer.DBFacade.loadLocations(DBFacade.java:32) 09-09 21:21:41.154: E/AndroidRuntime(3796): at cz.vongrad.domainLayer.Controller.loadLocations(Controller.java:94) 09-09 21:21:41.154: E/AndroidRuntime(3796): at cz.vongrad.locator.MapActivity$1.run(MapActivity.java:199) 09-09 21:21:41.154: E/AndroidRuntime(3796): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 09-09 21:21:41.154: E/AndroidRuntime(3796): at android.os.Handler.<init>(Handler.java:121) 09-09 21:21:41.154: E/AndroidRuntime(3796): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607) 09-09 21:21:41.154: E/AndroidRuntime(3796): at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607) 09-09 21:21:41.154: E/AndroidRuntime(3796): at android.os.AsyncTask.<clinit>(AsyncTask.java:190) 09-09 21:21:41.154: E/AndroidRuntime(3796): ... 4 more 09-09 21:21:41.164: E/EmbeddedLogger(239): App crashed! 

DataMapper的:

 class LoadLocations extends AsyncTask<String, Void, ArrayList<Friend>>{ private JSONObject jSONObject; @Override protected ArrayList<Friend> doInBackground(String... args) { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("user_name", args[1])); jSONObject = jSONParser.makeHttpRequest(args[0], "POST", params); Log.d(TAG, jSONObject.toString()); ArrayList<Friend> tempFriends = new ArrayList<Friend>(); try { if(jSONObject.getInt("success") == 1){ JSONArray jsonArray = jSONObject.getJSONArray("users"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject o = jsonArray.getJSONObject(i); tempFriends.add(new Friend(o.getString("user_name"), new LatLng(o.getDouble("loc_y"), o.getDouble("loc_x")), false)); } } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tempFriends; } } public ArrayList<Friend> loadLocation(String URL, String userName) throws InterruptedException, ExecutionException { return new LoadLocations().execute(URL, userName).get(); } 

MapActivity:

 public class MapActivity extends Activity implements LocationSource, LocationListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } private Thread loadFriendLocations = new Thread() { @Override public void run() { Log.d(TAG, "LastKnownLocation: " + locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)); try { while(true) { controller.loadLocations(LOAD_LOCATIONS); Log.d(TAG, "Loading new locations"); Message msg = uIHandler.obtainMessage(); msg.what = ADD_MAP_PIN; uIHandler.sendMessage(msg); sleep(10000); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; final Handler uIHandler = new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==ADD_MAP_PIN){ for (Friend friend : controller.getFriends()) { addMapPin(friend); } } super.handleMessage(msg); } }; 

奇怪的是,我可以在我的模拟器(API 16)上运行这个应用程序,它运行的很好,但是当我尝试在物理设备(API 15)上运行它时,它会得到这个错误。 AsyncTask在单独的类中运行,因此可能会导致此问题。 我将不胜感激任何build议。 提前致谢。

Solutions Collecting From Web of "Android – 不能创build处理程序内部没有调用Looper.prepare()的线程"

你不能从后台线程执行一个AsyncTask 。 请参阅AsyncTask文档的“线程规则”部分。

我有一个类似的问题,问题是我正在从主线程的系列线程。 其实主UI线程启动了一个服务,运行在一个服务上,随后启动另一个线程,最终有一个FileObserver(另一个线程),我不得不通过popupToast消息与UI线程进行通信。 我尝试了几个小时,并尝试下面,它作为一种魅力。

 //Let this be the code in your n'th level thread from main UI thread Handler h = new Handler(Looper.getMainLooper()); h.post(new Runnable() { public void run() { Toast.makeText(context, "Your message to main thread", Toast.LENGTH_SHORT).show(); } }); 

这里的主要关键是Looper类的getMainLooper()函数,它将为您提供针对主UI线程的Looper。

在你的背景中做这个…

 runOnUiThread(new Runnable() { public void run() { if (items.equals("null")) { Toast.makeText(getApplicationContext(), "No Event Found",Toast.LENGTH_LONG).show(); } else { } } });