1

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

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 的线程内创建处理程序

但是当位置返回一个值时,一切都很好。吐司出现。

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

4

3 回答 3

6

可以换吗

yoThereNotNull();

GetALocation2.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yoThereNotNull();
                }
            });
于 2011-08-01T10:35:57.347 回答
0

你在这一行有问题:

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();
于 2011-08-01T10:01:46.650 回答
0

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

于 2011-08-01T10:08:36.140 回答