Android Button throws IllegalStateException - java

So I'm trying to build the base of a simple app where I can switch activities with a button click. I have this working fine for my main activity to my second activity; however, I can't get it to work going back to the main activity.
Currently I am getting this error when I click the button: java.lang.IllegalStateException: Could not find method NewBack(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'back'
Heres the code in the second activity:
package com.example.homework4;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class CreateQuestion extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_question);
}
public void NewBack(View v){
setContentView(R.layout.activity_main);
}
}
Here is the code for the button in my xml file:
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="153dp"
android:onClick="NewBack"
android:text="GO BACK"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit" />
I have also tried setting an onClickListener with the same results. I'm sure this is an easy fix, I just can't seem to figure it out!

If You want to navigate between activities you need to replace
public void NewBack(View v){
setContentView(R.layout.activity_main);
}
with
public void newBack(View v){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
P.S. don't use Upercase in function name. So if the function is called newBack
change it also in XML file like:
<Button
android:id="#+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginEnd="153dp"
android:onClick="newBack"
android:text="GO BACK"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/submit" />

Related

Why doesnt the onClickListener do anything?

Following a tutorial, I did set up onClick-Listeners before.
Now I am trying to make a simple app for listening to one stream, but my onClickListener doesn't do anything.
I created a new very simple app in android studio 3.3.2 to find out what is happening. I guess it is something very basic.
package com.example.musicplay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_main);
final Button play_button = findViewById(R.id.play_button);
Log.i("Button found","this one: " + play_button.getText());
play_button.setOnClickListener(new View.OnClickListener() {
// The code in this method will be executed when the play button is clicked on.
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "A click happened!", Toast.LENGTH_SHORT).show();
}
});
setContentView(R.layout.activity_main);
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/play_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/play_button"/>
</LinearLayout>
The toast message should appear when the button is clicked, but nothing happens.
You are calling setContentView twice. This will replace the view hierarchy set up by the first call. Remove the second call to setContentView.

Java-app closes, when i push Button to start a Toast. But why?

I made VERY simple java toast that appears when you press a button. But when I run it on my phone, it stops the app and quits without an error. What did I do wrong?
MainActivity:
package com.example.ras.tests;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonWasClicked(View Button) {
Toast.makeText(this , "Button wurde geklickt!" , Toast.LENGTH_SHORT).show();
}
}
Button:
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginTop="16dp"
app:layout_constraintTop_toTopOf="#+id/activity_main"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="#+id/activity_main"
android:layout_marginLeft="16dp"
android:onClick="buttonWasClicked (MainActivity)"
android:visibility="visible" />
</android.support.constraint.ConstraintLayout>
Simple: android:onClick="buttonWasClicked" in your xml should do the trick.
Or in your OnCreate method you can assign listener to your button like this:
final Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
buttonWasClicked(button);
}
});
In this case you delete android:OnClick from your xml.
And you can do it event better: remove parameter View button from method buttonWasClicked this allows you not to declare your variable final.
Try removing the "(MainActivity)" in the android:onClick in your xml file.

How do i create a button to another page for my app

i was just wondering how i would make a button on my page so that it would go to another page for my app. I am a beginner so if you could explain how it all works and where it goes it would help a lot.
PS i am using android studio if that makes a difference and this is the code i have so far in my fragment_main.xml. I have not entered any code into the .java
<TextView android:text="#string/hello_world"
android:id="#id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:layout_below="#+id/text"/>
You can declare views and objects dynamically in Java, and then pass the button from fragment to fragment (or Activity to Activity, depending on your app).
To declare a Relative Layout with a Button, for example:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.RelativeLayout;
public class JavaLayoutActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton = new Button(this);
RelativeLayout myLayout = new RelativeLayout(this);
myLayout.addView(myButton);
setContentView(myLayout);
}
This doesn't set the properties or anything, I am just using this as proof of concept.
XML makes things easy with regards to user interface design because you don't have to manage it in code, but this is one case where it is an exception. If you want dynamic interface objects, you need to use Java.
Instead of creating view dynamically you should obtain your view in activity
ImageButton button = (ImageButton) findViewById(R.id.firstButton)
and assign an onClick listener to id
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// start new Activity here
}
});
You can also do it in xml:
<ImageButton
android:id="#+id/firstbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/homebutton"
android:onClick="sendMessage"
android:layout_below="#+id/text"/>
with such configuration, you should add method in activity:
public void sendMessage(View view) {
// start another activity here
}
There are two methods available in android using which you can go from one Activity to another.
1. Use button.setOnClickListener()
Create a button in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Now set event listener for the button in your .class file
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//set the event you want to perform when button is clicked
//you can go to another activity in your app by creating Intent
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}
});
2. Use <android:onClick="goNext">
Put the onClick as the attribute of the button you have created in xml file.
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="goNext" />
Now in your .class file define an event for that button as,
goNext() {
Intent intent = new Intent(getApplicationContext, Activity2.class);
startActivity(intent);
}

My app is unable to start activity?

When i try to run the app on my phone,it force closes when i try to do activity 3.
Logcat says:
01-13 17:53:25.368: E/AndroidRuntime(3235): Caused by: java.lang.NullPointerException
01-13 17:53:25.368: E/AndroidRuntime(3235): at android.app.activity3.onCreate(activity3.java:18)
where line 18 is
Button wg = (Button) findViewById(R.id.Back);
heres my full code for activity3.java:
package android.app;
import android.app.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class activity3 extends Activity{
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button wg = (Button) findViewById(R.id.Back);
wg.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
Thanks in advance
You need to modify your xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!-- your textview -->
<TextView
android:id="#+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/text1"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<!-- your back button -->
<Button
android:id="#+id/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Back"
/>
</RelativeLayout>
What I did:
Removed text and orientation from your root RelativeLayout, these do nothing.
Modified your height and width, feel free to change these as you please.
Added a Button with id of R.id.back and text of #string/Back.
You will now be able to reference your button using findViewById(R.id.back) and set your click listener.
I don't see a button in the xml. And once you have that, it needs to have:
android:id="#+id/Back"
This is happening because you don't have a button declared in your view. Only a Textview. You must create the button in the view. You're referencing nothing hence the null.
You can find a nice example of how to make a button here.
You don't have button with id "Back" in xml posted, that is why you are getting null there. Add button entry in your xml.

Can I use setContentView outside the oncreate method?

I have seen many people telling that you can set setContentView outside the oncreate method, but I didn't find anywhere an example. Now when I try to use setContentView, my app just crashes. Here is my source code:
AlarmActivity.java:
package com.alarm.example;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button buton = new Button(this);
buton.setId(101);
buton.setText("New alarm");
buton.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(buton);
setContentView(layout1);
Button nou =(Button)findViewById(101);
nou.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
New_ala nou1=new New_ala();
nou1.nou_alarma();
}
});
}
}
New_ala.java:
package com.alarm.example;
public class New_ala extends AlarmActivity{
public void nou_alarma() {
setContentView(R.layout.timepicker);
}
}
TimePicker.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TimePicker
android:id="#+id/timePicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/picker_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Save">
</Button>
<Button
android:id="#+id/picker_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cancel">
</Button>
</LinearLayout>
</LinearLayout>
On more detail, I can use setContentView(R.layout.timepicker) inside the oncreate method without any problems so the problem is that setContentView isn't working properly inside the New_ala.java class. Can anyone help me?
The code after setting the intent:
AlarmActivity:
package com.alarm.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class AlarmActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(this, NewAlarmActivity.class));
}
}
NewAlarmActivity:
package com.alarm.example;
import android.os.Bundle;
public class NewAlarmActivity extends AlarmActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timepicker);
}
}
You can call setContentView any time you are running on the event (UI) thread. Be aware that when you do, any fields you initialized by calling findViewById will need to be reset.
The best way to do that is to have multiple activities: instead of nou1.nou_alarma();, create an intent and start a new activity with the new layout.
startActivity(new Intent(this, NewAlarmActivity.class));
And in the onCreate method of NewAlarmActivity, set the content view to R.layout.timepicker
The setContentView() method can be called only once per activity. If you want to completely change the layout at some point you should either go for ViewFlipper or have 2 layouts in the activity and show only one of them at given time by calling view.setVisibility(View.GONE); and view.setVisibility(View.VISIBLE); respectively.

Categories