Android:无法在未调用looper.prepare的线程内创建处理程序

发布于 2024-11-27 18:54:43 字数 2669 浏览 3 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对你的占有欲 2024-12-04 18:54:43

你可以替换

yoThereNotNull();

GetALocation2.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yoThereNotNull();
                }
            });

Can you replace

yoThereNotNull();

with

GetALocation2.this.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    yoThereNotNull();
                }
            });
紫罗兰の梦幻 2024-12-04 18:54:43

您在这一行遇到问题:

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();

You have a problem in this line:

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

Try using the activity.

Toast.makeText( this, "I got the location! Yeah! >>> " + GetALocation2.this.latitude, Toast.LENGTH_SHORT).show();
绝影如岚 2024-12-04 18:54:43

当您在另一个线程中手动创建一个线程等等时,就会出现此错误。这是 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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文