Reputation: 161
I am testing some stuff with handlers , and i came accross the following issue, the design og the classes itself i guess makes not much sense (passing the context of the activity to the constructor of the other class , which in turns extends Thread ), but i think it could be useful from the "academic" point of view.
so i have two class - the first one is activity , and the second one extends Thread , with the idea that the second class will pass some data to the activity , and i made a constructor which will take both the handlers from the activity and the context of that activity
PS. I found out that the Issue actually appears only when i try to pass the Location object parameters , edited the code:
the Activity part :
public class MainActivity extends Activity {
public static final String content="Asega kvo stava";
public static final Integer kom=2;
Context m;
private Handler nok;
Location loc;
String TAG="TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nok=new Handler() {
public void handleMessage(Message msg)
{
Log.d(TAG, "msg: " + msg);
Bundle m=msg.getData();
}
};
new locac(nok,this).start();
and here is the other class
public class locac extends Thread implements LocationListener{
public static Context Ctx;
private final Handler nok;
LocationManager norm;
public static String moi="manag.GPS_PROVIDER";
public locac (Handler h, Context k){
nok=h;
Ctx=k;
}
.....
public void run ()
{
norm=(LocationManager)Ctx.getSystemService(Ctx.LOCATION_SERVICE);
norm.requestLocationUpdates(norm.GPS_PROVIDER, 0, 0, this);
Location oko=norm.getLastKnownLocation(norm.GPS_PROVIDER);
int lato=(int)oko.getLatitude();
Message message=new Message();
Bundle bok=new Bundle();
int beta=145;
bok.putInt("tok",lato);
bok.putInt("toke",beta);
message.setData(bok);
nok.sendMessage(message) ;
}
now if i try to compile that one , it gives me the following message
java.lang.RuntimeException: Can't create handler inside thread that has not called
Looper.prepare()
at android.os.Handler.<init>(Handler.java:197)
at android.os.Handler.<init>(Handler.java:111)
if i remove the context part of the Constructor of the Locac, and leave it just
public locac (Handler h){
nok=h;
}
and then just initialize it with - new locac(nok).start(); in the Activity class , then it works fine i can't figure why calling the context within the thread class gives the Looper message , a more detailed explanation will be greatly appreciated (for learning purposes also)
Upvotes: 0
Views: 1051
Reputation: 26
just in case anyone is interested , it turned out that the source of the problem was the requestLocationUpdates , which invokes the LocationListener object , which in turn calls the OnChangedLocation to get the updates
if we look at the LocationManager.java
public void onLocationChanged(Location location) {
Message msg = Message.obtain();
msg.what = TYPE_LOCATION_CHANGED;
msg.obj = location;
mListenerHandler.sendMessage(msg);
}
which means the onLocationChanged calls the mListenerHandler and therefore the looper message..
Upvotes: 1
Reputation: 390
If you replace in your MainActivity new locac(nok,this).start();
with new locac(nok,MainActivity.this);
you will not get the error, because when you use this you are referring to the Thread Object and not the MainActivity object.
And you cannot pass a Thread Object into a Thread
Upvotes: 0