Reputation: 1
Probably a basic mistake but I'm working on adding additional features to a random open source project I found on the web as part of learning to work with android and so would obviously like to add additional activities. My code is as follows, taking out the irrelevant bits:
import mun.pakkaus.aateekoo.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.util.Log;
public class CalorieCounter extends Activity
{
String GDA;
EditText tx, weightE, heightE, ageE;
TextView counter;
Spinner spin;
String gender;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button2 = (Button) findViewById(R.id.button2);
}
public OnClickListener button2 = new OnClickListener()
{
public void onClick(View arg0)
{
Intent nextScreen = new Intent(getApplicationContext(),
SecondScreenActivity.class);
nextScreen.putExtra("Gender", gender);
Log.e("n", gender);
startActivity(nextScreen);
}
};
}
and the second activity is:
package my.pack.coursework;
import mun.pakkaus.aateekoo.*;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SecondScreenActivity extends Activity
{
public void OnCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View arg0)
{
TextView counter;
Intent i = getIntent();
String gender = i.getStringExtra("gender");
Log.e("Second Screen", gender + ".");
counter = (TextView) findViewById(R.id.textView23);
int caloriesBurned = 0; int caloriesConsumed = 0;
EditText consumedE;
EditText burnedE;
String test1, test2;
test1 = getString(R.id.txtBurned);
test2 = getString(R.id.txtConsumed);
try
{
if (test1 != "" && test2 != "")
{
burnedE = (EditText) findViewById(R.id.txtBurned);
caloriesBurned = Integer.parseInt(burnedE.getText().toString().trim());
consumedE = (EditText) findViewById(R.id.txtConsumed);
caloriesConsumed = Integer.parseInt(consumedE.getText().toString().trim());
if(gender.contains("Male") && caloriesConsumed - caloriesBurned > 2000)
{
counter.setText("You are over your GDA of calories");
}
else
{
counter.setText("Don't add offensive text");
}
}
}
catch (Exception e)
{
System.out.println(e);
}
}
});
}
}
Essentially, why is my activity not switching?
Upvotes: 0
Views: 125
Reputation: 253
If you have problem with OnclickListener, you can use this method: Inside your xml activity.
<Button
.......
android:onClick="functionBt1"
........
/>
In your class, you should create a method as same name "functionBt1"
public void functionBt1(View v){
Intent i = new Intent(First.this,Second.class);
startActivity(i);
}
And remember put both activities in your AndroidManifest.xml
Upvotes: 0
Reputation: 278
You are not assigning the OnClickListener, you are just creating an OnClickListener with the same name as your Button Object.
Correct way of using it:
...
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(onClickListener);
}
public OnClickListener onClickListener = new ...
Upvotes: 1
Reputation: 589
You have created the Button locally so it is destroyed after OnCreate(). Declare globally in class and just initialize in OnCreate().
Button button2;
onCreate()
{
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
}
public void onClick(View v)
{
if(v == button2)
{
}
}
Upvotes: 1