【问题标题】:Android: cant create handler inside thread that has not called looper.prepareAndroid:无法在未调用 looper.prepare 的线程内创建处理程序
【发布时间】:2011-10-17 07:52:30
【问题描述】:

我知道存在这种问题,但在这种情况下我很困惑。我正在使用以下代码:

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说:cant create handler inside thread that has not called looper.prepare

但是当位置返回一个值时,一切正常。吐司出现。

有人知道我的情况如何处理吗?我对java和android有点陌生,非常感谢您的帮助! :)

【问题讨论】:

    标签: android handler looper


    【解决方案1】:

    可以换吗

    yoThereNotNull();
    

    GetALocation2.this.runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        yoThereNotNull();
                    }
                });
    

    【讨论】:

    • 只是对你的代码稍作修改,我替换了 yoThereNotNull();与 yoThereNull();它就像一个魅力,非常感谢@PravinCG! :)
    • 很高兴它有帮助。我误读了您遇到问题的方法。 :)
    【解决方案2】:

    当您在另一个线程中手动创建线程等等时会出现此错误。这是 Android 无法处理的一种情况。这就是为什么他们提供了一个名为 AsyncTask 的东西,您可以使用它来做一些事情在后台..当这个异常出现时,你不能总是说你的代码有错误。有时你的代码是干净的,但 Android 操作系统仍然会抛出这个异常。所以请确保使用 AsyncTask 而不是自己创建线程。 .

    【讨论】:

    • 是的,你说得对,但我没有在他的代码中看到任何线程?
    【解决方案3】:

    你在这行有问题:

    Toast.makeText( getBaseContext(), "I got the location! Yeah! >>> " + GetALocation2.this.latitude, Toast.LENGTH_SHORT).show();
    

    尝试使用该活动。

    Toast.makeText( this, "I got the location! Yeah! >>> " + GetALocation2.this.latitude, Toast.LENGTH_SHORT).show();
    

    【讨论】:

    • 你能试试用'GetALocation2.this'代替这个吗?以防万一它试图使用“LocationResult”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多