New to Java. I am trying to create an explicit Intent to launch send the user to a new tab and pass some data to it. The problem I think I am having is getting the Context of the parent object that I can pass to the Intent Constructor. "This" is not correct. The onCreate code is below. Any pointers to examples would be helpful
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.quickactivity);
final Button goButton = (Button) findViewById(R.id.go);
goButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, TimeActivity.class);
}
});
}
You can also use:
Intent i = new Intent(v.getContext(), TimeActivity.class);
More info here
Use
<MyClassName>.this
instead. Replacing by the actual class in which this method resides
Related
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnSighin = (Button) findViewById(R.id.btnSignIn);
btnSignUp = (Button) findViewById(R.id.btnSignUp);
btnSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent j = new Intent(MainActivity.this, SignUp.class);
startActivity(SignUp);
}
});
btnSighin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View view) {
Intent k = new Intent(MainActivity.this, SignIn.class);
startActivity(SignIn);
}
});
}
I am working on an app that allows customers to book appointments and in order to do so, they need to sign up or sign in. I have activities made for both of them and I can't run it without getting the "Expression Expected" and I'm new to Android Studio and have no idea what to do. Any help?
You are missing some information. Anyway, i see that you have mistakes in lines
startActivity(SignUp);
and
startActivity(SignIn);
The correct way should be startActivity(j) and startActivity(k) respectively
You have to pass the intent object to the start activity method not the signup or signin class
So it should be startActivity(i) and startActivity(j)
I have researched many problems like mine on the internet and none seem to be doing what I'm trying to do. What I am trying to do is get a textview that is currently blank in my high_risk.xml file, and make it have text based on if a button is clicked in another class. Here is what I have so far...
Question13Activity(if the yes button is clicked I want to be able to set the text in the HighRisk Activity)
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = getIntent();
String text = intent.getStringExtra("New Text");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
}
});
This is how I have the static variable t defined in my highrisk activity class...
HighRiskActivity(this is where I want the text to be set and displayed)
public static TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent myIntent = new Intent(HighRiskActivity.this, Question12Activity.class);
myIntent.putExtra("Text", "New Text");
startActivity(myIntent);
Every time I try and access the contents of t in another class and change it's text, it always returns null. Any way I can fix this from happening? Any help would be greatly appreciated :)
You can use Bounds, to get the the data to the new Activity and set the text from there:
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("<KEY-NAME>", "<TEXT>");
startActivity(myIntent);
And in the Question13Activity.class:
Intent intent = getIntent();
String text= intent.getStringExtra("<KEY-NAME>");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
UPDATE:
Use it so:
HighRiskActivity.class:
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
myIntent.putExtra("TestKey", "My new Text");
startActivity(myIntent);
}
});
Question13Activity.class:
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
Intent intent = getIntent();
String text= intent.getStringExtra("TestKey");
t = (TextView) findViewById(R.id.abusedOrNah);
t.setText(text);
In order to setText() from another class (for example, from your Custom class), you need to specify the activity on which you'd like to do that.
Unless a class does have a reference of your context, you cannot call 'findViewById()'
To do that, you use Context as a parameter for custom class constructor.
Just after you have set the context will you be able to call
TextView txtView = (TextView) ((Activity)context).findViewById(R.id.text);
txtView.setText("foo");
}
I have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.
btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.
The java code for my program is located below here
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);
btnObjClickToGoToFirstPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.activity_main);
}
}
);
} }
Please help me rectified the problem thanks.
Please implement the View.Onclick listener not Button.onclick listener
btnObjClickToGoToFirstPage.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
It is not a proper way switching pages in Android. Use two activities for switching pages with intents.
Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);
Put above code in your button's onClick().
If you want to show a new page you either start a new activity or start a new fragment.
Changing the contentView is not the correct way to approach this and should not be done.
You refer to the documentation on activities here.
Assuming you have another Activity called SecondActivity here is how you would start it:
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
And then you define the layout in the XML of the new activity, that is second_activity.xml
If they all have similar layouts using a fragment is also a good option.
Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().
This question may further clear your doubts:
What is setContentView(R.layout.main)?
I am new in android development and do not have an in depth knowledge of Java. I am stuck on a problem for a long time. I am trying to open a new activity on button click. But I am getting an error that error: not an enclosing class: Katra_home.
Here is the code for MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button)findViewById(R.id.bhawan1);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(Katra_home.this, Katra_home.class);
Katra_home.this.startActivity(myIntent);
}
});
And this is the code for Katra_home.java
public class Katra_home extends BaseActivity {
protected static final float MAX_TEXT_SCALE_DELTA = 0.3f;
private ViewPager mPager;
private NavigationAdapter mPagerAdapter;
private SlidingTabLayout mSlidingTabLayout;
private int mFlexibleSpaceHeight;
private int mTabHeight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.katra_home);
ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
ab.setHomeButtonEnabled(true);
}
Though I have seen many answers on stackoverflow but I could not understand them as I am new in android development. So I would like to ask what changes do I need to make in my code to make it work.
It should be
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
You have to use existing activity context to start new activity, new activity is not created yet, and you cannot use its context or call methods upon it.
not an enclosing class error is thrown because of your usage of this keyword. this is a reference to the current object — the object whose method or constructor is being called. With this you can only refer to any member of the current object from within an instance method or a constructor.
Katra_home.this is invalid construct
Intent myIntent = new Intent(MainActivity.this, Katra_home.class);
startActivity(myIntent);
This Should the perfect one :)
you are calling the context of not existing activity...so just replace your code in onClick(View v) as
Intent intent=new Intent(MainActivity.this,Katra_home.class);
startActivity(intent);
it will definitely works....
String user_email = email.getText().toString().trim();
firebaseAuth
.createUserWithEmailAndPassword(user_email,user_password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()) {
Toast.makeText(RegistraionActivity.this, "Registration sucessful", Toast.LENGTH_SHORT).show();
startActivities(new Intent(RegistraionActivity.this,MainActivity.class));
}else{
Toast.makeText(RegistraionActivity.this, "Registration failed", Toast.LENGTH_SHORT).show();
}
}
});
replace code in onClick() method with this:
Intent myIntent = new Intent(this, Katra_home.class);
startActivity(myIntent);
startActivity(new Intent(this, Katra_home.class));
try this one it will be work
I have to spinners, and when I start my app, the PHP only returns values of spinner's first choice.
First code is part of one class (IzboraGrada.java)
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
str_grad=spinner1.getSelectedItem().toString();
str_predmet=spinner2.getSelectedItem().toString();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
And the second code is part of MainActivity.class that was in intent.
private void initView() {
// show progress dialog
dialog = ProgressDialog.show(this, "", "Loading...");
String url = "http://192.168.1.102/test/spinner.php";
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
FetchDataTask task = new FetchDataTask(this);
task.execute(url);
}
I presume that's because str_grad and str_predmet are not defined in second class. But If I put str_grad and str_predmet in second class, they are can't be resolved as type.Any ideas what to do?
It looks like you are calling this method at the beginning, before an item is selected, so I think the problem is that you are setting the values for str_grad and str_predmet when they are first set so the selected item is the default item. Those are getter functions not listeners.
You need to move those lines inside the onClick() or use onItemSelected() on your Spinners to set those variable values
public void addListenerOnButton() {
spinner1=(Spinner) findViewById(R.id.spinner1);
spinner2=(Spinner) findViewById(R.id.spinner2);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
str_grad=spinner1.getSelectedItem().toString(); // move these lines here
str_predmet=spinner2.getSelectedItem().toString();
Intent i=new Intent (v.getContext(), MainActivity.class);
url = "http://192.168.1.102/test/spinner.php";
url=url+"?grad="+str_grad+"&predmet="+str_predmet;
i.putExtra("URL",url);
startActivity(i);
}
});
If I understand your problem correctly, that should solve your problem.