Android:无法在未调用looper.prepare的线程内创建处理程序
我知道存在这种问题,但在这种情况下我很困惑。我使用以下代码:
package com.example.GetALocation2;
import com.example.GetALocation2.MyLocation.LocationResult;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class GetALocation2 extends Activity {
public static final String LOG_TAG = "------------------GetALocation2";
Double latitude;
TextView tv;
MyLocation myLocation = new MyLocation();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("Yo there!");
Log.e(LOG_TAG, "Toast will be shown");
Toast.makeText(getBaseContext(), "This is the start!", Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG, "Toast was shown");
locationClick();
}
private void locationClick() {
Log.e(LOG_TAG, "Triggered location click");
myLocation.getLocation(this, locationResult);
}
public void yoThereNull(){
Toast.makeText(getBaseContext(), "Location is unknown.", Toast.LENGTH_SHORT).show();
}
public void yoThereNotNull(){
Toast.makeText( getBaseContext(), "I got the location! Yeah! >>> " + GetALocation2.this.latitude, Toast.LENGTH_SHORT).show();
}
public LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(final Location location){
//Got the location!
Log.d(LOG_TAG, "Entered gotLocation()");
try{
if( location == null ){
Log.d( LOG_TAG, "Null Location is returned" );
yoThereNull();
}else{
Log.d( LOG_TAG, "A location is found/returned" );
GetALocation2.this.latitude = location.getLatitude();
yoThereNotNull();
}
}catch (NullPointerException e) {
Log.e(LOG_TAG, e.toString());
}catch(Exception e){
Log.e(LOG_TAG, e.toString());
}
};
};
}
当 location 返回 null 并调用 yoThereNull() 方法时,logcat 说:无法在未调用过 Looper.prepare 的线程内创建处理程序,
但当 location 返回一个值时,一切正常。吐司出现。
有人知道我的情况如何处理吗?我对 java 和 android 有点陌生,非常感谢您的帮助! :)
I know this kind of question exist but I'm confused in this case. I'm using the following code:
package com.example.GetALocation2;
import com.example.GetALocation2.MyLocation.LocationResult;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
public class GetALocation2 extends Activity {
public static final String LOG_TAG = "------------------GetALocation2";
Double latitude;
TextView tv;
MyLocation myLocation = new MyLocation();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.thetext);
tv.setText("Yo there!");
Log.e(LOG_TAG, "Toast will be shown");
Toast.makeText(getBaseContext(), "This is the start!", Toast.LENGTH_SHORT).show();
Log.e(LOG_TAG, "Toast was shown");
locationClick();
}
private void locationClick() {
Log.e(LOG_TAG, "Triggered location click");
myLocation.getLocation(this, locationResult);
}
public void yoThereNull(){
Toast.makeText(getBaseContext(), "Location is unknown.", Toast.LENGTH_SHORT).show();
}
public void yoThereNotNull(){
Toast.makeText( getBaseContext(), "I got the location! Yeah! >>> " + GetALocation2.this.latitude, Toast.LENGTH_SHORT).show();
}
public LocationResult locationResult = new LocationResult(){
@Override
public void gotLocation(final Location location){
//Got the location!
Log.d(LOG_TAG, "Entered gotLocation()");
try{
if( location == null ){
Log.d( LOG_TAG, "Null Location is returned" );
yoThereNull();
}else{
Log.d( LOG_TAG, "A location is found/returned" );
GetALocation2.this.latitude = location.getLatitude();
yoThereNotNull();
}
}catch (NullPointerException e) {
Log.e(LOG_TAG, e.toString());
}catch(Exception e){
Log.e(LOG_TAG, e.toString());
}
};
};
}
when location returns null and call yoThereNull() method, the logcat says: cant create handler inside thread that has not called looper.prepare
but when location returns a value, all is okay. the toast appear.
Anyone knows how to handle this in my case? I'm kinda new to java and android, many thanks for any help! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以替换
为
Can you replace
with
您在这一行遇到问题:
尝试使用该活动。
You have a problem in this line:
Try using the activity.
当您在另一个线程中手动创建一个线程等等时,就会出现此错误。这是 Android 无法处理的一个情况。这就是为什么他们提供了名为
AsyncTask
的东西,您可以将其用于在后台做事情..当这个异常出现时,你不能总是说你的代码中有错误..有时你的代码是干净的,但Android操作系统仍然会抛出这个异常..所以请确保使用AsyncTask而不是通过创建线程 你自己..This error comes when you create a thread manually inside another thread and so on.. This is one condition that Android can't handle.. Thats why they have given something called
AsyncTask
, which you can use for doin things in Background.. when this ecxeption arrises u cannot always say that you have error in your code.. sometimes your code is clean but still Android OS will throw this Exception.. So make sure to use AsyncTask instead of creating a Thread by yourself..