Reputation: 5040
I have a class which extends Activity.
public class MyActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id,someactivitylayout);
new Game(getApplicationContext());
}
My Game class looks like
public class Game{
Game(final Context context){
cell.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/* here i need to call runOnUIThread*/
}
}
}
My code does not have any syntax errors, so it compiles fine.
In the place where i have to call runOnUIThread, i have tried
Thread t = new Thread() {
public void run() {
while (progressBar.getProgress() < 100) {
((Activity) context).runOnUiThread(new unnable() {
public void run() {
}
});
}
};
t.start();
But when i try to cast context to Activity it gives an exception
java.lang.ClassCastException: android.app.Application cannot be cast to android.app.Activity
Why is it not possible to cast context to Activity??
I have tried many ways but did not find anyway to get Activity in my Game class.
Is there any way to do that??
Upvotes: 0
Views: 12279
Reputation: 9150
try with
public class MyActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id,someactivitylayout);
new Game(this);
}
}
Upvotes: 1
Reputation: 1127
You are sending ApplicationContext to the Game class. Just replace the getApplicationContext()
with this
to pass the Activity Context. It will work.
It should be new Game(this)
Upvotes: 3