Pass Data Moving from activity to Class extends View - android

i'm trying pass data from MainActivity to my class VistaJuego which draws a doll moving on the screen but when it collides with obstacles , the game stops and with an intent should go to another activity. I need the context of my MainActivity to do my intent but Eclipse always show NullPointerException.
my MainActivity
public class MainActivity extends Activity {
RelativeLayout relative;
static Activity ac;
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vistafaba=(VistaJuego)findViewById(R.id.faba);
relative=(RelativeLayout)findViewById(R.id.relative);
ac=this.getActivity();
}
public void goToActivityLosed(){
Intent i= new Intent(this,ActivityLosed.class);
startActivity(i);
}
And my VistaJuego
public class VistaJuego extends View {
ThreadJuego juego = new ThreadJuego();
ThreadFaba hiloFaba= new ThreadFaba();
MainActivity main;
public VistaJuego(Context context, AttributeSet attrs) {
super(context, attrs);
//dialogo= new DialogoFinJuego(MainActivity.this);
main= new MainActivity();
}
public void whenBeanCrash(Grafico elementofaba,Grafico elementoBotella){
if((elementofaba.getPosX()+elementofaba.getAncho()>=elementoBotella.getPosX()+15)&&
(elementofaba.getPosX()+elementofaba.getAncho() <= elementoBotella.getPosX()+elementoBotella.getAncho()+15)&&
(elementofaba.getPosY()+elementofaba.getAlto()>=elementoBotella.getPosY())
&& (elementofaba.getPosY()+elementofaba.getAlto() <= elementoBotella.getPosY()+elementoBotella.getAlto())){
juego.detener();
hiloFaba.detener();
main.goToActivityLosed();
}

If you want to start an activity use context not Activity
Example:
public void whenBeanCrash(Grafico elementofaba,Grafico elementoBotella){
// ...
// and not main.goToActivityLosed()
Context context = getContext();
context.startActivity(new Intent(context, YourActivity.class));
}
Or you can create a function to set an activity object:
public void setActivity(MainActivity activity){
this.activity = activity;
// and now in whenBeanCrash() you can use this.activity to call function from this class
}

Related

Restarting activity inside onClick method

I have an app which reads .txt file and displays contents in table layout.
here is my MainActivity.java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this,0));
}
}
Inside TableLayout class when adding first row (headers), i also add on click listeners.
Here is the code that gets executed when click happens:
public class MyOnClickListener extends MainActivity implements OnClickListener {
int rowNumber;
public MyOnClickListener(int rowNumber) {
this.rowNumber = rowNumber;
}
#Override
public void onClick(View v) {
setContentView(new TableLayout(context,rowNumber));
}
};
context is saved from when activity is first started, but i get nullpointexception error with this as an argument.
What i would like to do when header is clicked is to recreate table with header number argument.
So my question is what should i do to restart table creation within onClick method?
Edit: this is how context is saved
Context context;
public TableLayout(Context context, int rowNr) {
super(context);
this.context = context;
I would probably do something like this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TableLayout(this, getRowNumber()));
}
protected int getRowNumber(){
return 0;
}
}
and then you do something like
public class MyOnClickListener extends MainActivity implements OnClickListener {
// here you initialize rowNumber
static int rowNumber = 0;
#Override
protected void getRowNumber(){
return rowNumber;
}
#Override
public void onClick(View v) {
// here you set your rowNumber
rowNumber = some_value;
recreate();
}
};
P.S: I haven't compiled this but you can get the idea

Move from an activity to another one in View Class in Android

I have an activity that has two classes like these:
public class StartActivity extends Activity {
.
.
.
}
public class StartView extends View {
.
.
.
}
I want to go from this Activity to another one with click on one image.Is there any method (such startActivity method in Activity Class) in View Class that I can use it in the Second Class?
Do you mean something like this:
public class startView extends View {
Activity mActivity;
Context mContext;
public startView(Activity yourActivity, Context context, AttributeSet attrs) {
// TODO Auto-generated constructor stub
super(context, attrs);
this.mActivity = yourActivity;
this.mContext = context;
init();
}
public void init()
{
ImageView iv = (ImageView) findViewById(R.id.IMAGEVIEWID);
iv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(mActivity, SecondActivity.class);
mContext.startActivity(i);
}
});
}
}

How to pass an integer from an activity to its view

I have tried to pass on an int without success from this activity:
public class SecondActivity extends Activity {
private int sign=3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(newView);
}
}
to this view:
public class TestView extends View {
private int sign;
public TestView(Context context, int signValue) {
super(context);
this.sign=signValue;
}
}
But the value isn't passed on, the variable "sign" remains null. Am I missing something? What?
(In my code the variable sign gets its value from a bundle, but I tested it and know it's getting the right value.)
EDIT:
Still don't know what was wrong with the previous code, but I solved it by creating a setSign method.
public class SecondActivity extends Activity {
private int sign=3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TestView newView = new TestView(this, sign);
requestWindowFeature(Window.FEATURE_NO_TITLE);
newView.setSign(sign);
setContentView(newView);
}
}
to this view:
public class TestView extends View {
private int sign=0;
public TestView(Context context) {
super(context);
}
public void setSign(int signValue) {
sign=signValue;
}
}

android.view.windowmanager Bad TokenException:unable to add window for Dialog on android

In my app i am showing a custom dialog from a non-activity class. it works but rarely crashed with the following reason:
android.view.windowmanager Bad TokenException:unable to add window-
token android.os.binderproxy#2afac69d8 is not valid;is your activity running?
my coding structure:
MainActivity.java(Activity class)
public class MainActivity extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
........
UpdateClass obj = new UpdateClass(MainActivity.this);
obj.checkUpdate();
}
}
UpdateClass.java (Non Activity class)
public class UpdateClass{
UpdateClass(MainActivity mainActivity ){
this.context = mainActivity;
}
checkUpdate(){
dialog_Update = new Dialog(context);
...
updateButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
....
}
});
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog_Update.dismiss();
}
});
dialog_Update.show();
......
}
}
Just try to implement as below :
public class MainActivity extends TabActivity {
#Override
private Context context;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
........
context=MainActivity.this;
UpdateClass obj = new UpdateClass(context);
obj.checkUpdate();
}
}
Do the changes as below in your UpdateClass.
Instead of passing the MainActivity in constructor pass the Activity as below:
public class UpdateClass{
pdateClass(Activity mainActivity ){ <----Pass the activity here.
this.context = mainActivity;
}
......
}
}

How to call a method in another activity

I have a question about communication between activities on implementation program of Android.
Here is two activity classes.
public class HelloAndroidActivity extends TabActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, Tab1Activity.class);
spec = tabHost.newTabSpec("Tab1").setIndicator(
"Tab1", res.getDrawable(R.drawable.ic_tab_icon))
.setContent(intent);
tabHost.addTab(spec);
}
}
_
public class Tab1Activity extends ListActivity {
private ArrayList<String> list = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = new ArrayList<String>();
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list));
addColumn("one");
addColumn("two");
addColumn("three");
}
public void addColumn(String s){
list.add(new String(s));
}
}
HelloAndroidActivity is main activity.
Tab1Activity is sub activity and display listview.
HelloAndroidActivity include Tab1Activity view.
What I want to do is calling addColumn method from HelloAndroidActivity,
because HelloAndroidActivity is added to new function like TwitterUserStreamAdapter.
If the android receive messages from internet,
application send message to Tab1Activity.
However, I don't know how to implement communication between activities.
You can pass data between activities using intent, you could put it in the extras with the intent:
HelloAndroidActivity
intent.putExtra("callX", true);
Tab1Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
boolean callX = extras.getBoolean("callX");
if(callX) {
X();
}
}
EDIT
If you need to event/listener mechanism it could be roughly like this(haven't compiled this, but should give you an idea):
public inerface MyEventListener {
abstract void handleMyEvent();
}
public class Tab1Activity implements MyEventListener {
public void handleMyEvent() {
/*...*/
}
protected void onCreate(Bundle savedInstanceState) {
/*...*/
HelloAndroidActivity.addListener(this);
}
protected void onDestroy() {
/*...*/
HelloAndroidActivity.removeListener(this);
}
}
public class HelloAndroidActivity {
static ArrayList<MyEventListener> listeners = new ArrayList<MyEventListener>();
public static void addListener(MyEventListener listener) {
listeners.add(listener);
}
public static void removeListener(MyEventListener listener) {
listeners.remove(m);
}
public static void onEvent() {
for(MyEventListener m : listeners) {
m.handleMyEvent();
}
}
}
although you can do it by creating static method , but not right way because it will leave context . pass data to Tab1Activity you have in HelloAndroidActivity throgh intent .
inside Tab1Activity getIntent and work accordingly .
you can also use onTabChange() to reflect changes between tabs .

Categories

Resources