我知道這種問題存在,但我很困惑在這種情況下。我使用下面的代碼: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());
}
};
};
}
時的位置返回null,並調用yoThereNull()方法時,logcat的說:不能創建內螺紋處理程序尚未調用looper.prepare
但位置返回時一個值,一切都沒關係。出現吐司。
任何人都知道如何處理這個在我的情況?我對java和android有點新鮮,非常感謝任何幫助! :)
只需對您的代碼稍作修改,我替換yoThereNotNull();與yoThereNull();它的功能就像一個魅力,非常感謝@PravinCG! :) – Emkey
很高興幫助。我誤解了你遇到的問題。 :) – PravinCG
感謝它爲我工作:) –