I have an issue with this botton, when is clicked the app send a message "ImageButton has stopped". I think it's something dumb, but I just started coding. Details of my code are below.
LogCat:
12-13 21:01:56.659 1075-1075/com.example.christian.imagebutton E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.christian.imagebutton, PID: 1075
java.lang.IllegalStateException: Could not find method llamar(MainActivity)(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageButton with id 'boton1'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Main Activity
package com.example.christian.imagebutton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1=(TextView)findViewById(R.id.tv1);
}
public void llamar(View view){
tv1.setText("Llamando");
}
}
And the layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.christian.imagebutton.MainActivity">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/telefono"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/boton1"
android:onClick="llamar(MainActivity)" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/boton1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp"
android:id="#+id/tv1" />
</RelativeLayout>
android:onClick="llamar"
Shouldn't it be like this?
Android just implements the OnClickListener for you when you define the android:onClick="someMethod" attribute.
Those two code snippets are totally the same but just implemented in two different ways.
Code Implementation
Button btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
llamar(v);
}
});
// some more code
public void llamar(View v) {
// does something very interesting
}
Above is a code implementation of an OnClickListener. And now the XML implementation.
XML Implementation
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="llamar" />
<!-- even more layout elements -->
Now in the background Android does nothing else than the Java code calling your method on a click event.
Note that with the XML above, Android will look for the onClick method llamar() only in the current Activity. This is important to remember if you are using fragments, since even if you add the XML above using a fragment, Android will not look for the onClick method in the .java file of the fragment used to add the XML.
Related
This question already has answers here:
Android app crashing (fragment and xml onclick)
(5 answers)
How to handle button clicks using the XML onClick within Fragments
(19 answers)
Android Fragment onClick button Method
(7 answers)
Closed 4 years ago.
I have a button in activity_welcome.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WelcomeActivity">
<Button
android:id="#+id/signIn"
android:layout_width="270dp"
android:layout_height="39dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="註冊"
android:textColor="#000000"
android:textSize="20sp"
android:onClick="goToSignUpOne"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602" />
</android.support.constraint.ConstraintLayout>
When I call my goToSignUpOne method in the WelcomeActivity class, it causes a crash and it seems like which doesn't match the method. This is the code of the goToSignUpOne function:
public void goToSignUpOne(View view) {
setContentView(R.layout.activity_sign_up_one);
}
After using goToSignUpOne to navigate to my MainActivity, it works!
So my button is in the activity_welcome.xml but corresponds to MainActivity? Is that right? I think it should rather correspond to the activity which the XML is for.
error log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.faketsao.dating, PID: 21004
java.lang.IllegalStateException: Could not find method goToSignUpOne(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'signIn'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:423)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:380)
at android.view.View.performClick(View.java:6205)
at android.widget.TextView.performClick(TextView.java:11103)
at android.view.View$PerformClick.run(View.java:23653)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1534)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1424)
WelcomeActivity
package com.example.faketsao.dating;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class WelcomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
}
public void goToSignUpOne(View view) {
setContentView(R.layout.activity_sign_up_one);
}
}
What you are doing is a pretty non-typical approach. If I were you, I would define the onClick in the code of WelcomeActivity, and not in the XML, since using the XML is prone to produce errors like yours. Therefore, the code of your WelcomeActivity should be something like this:
public class WelcomeActivity extends AppCompatActivity {
private View.OnClickListener onButtonClick = new View.OnClickListener(){
#Override
public void onClick(View v){
goToSignUpOne(v);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
Button button = (Button) findViewById(R.id.signUp);
button.setOnClickListener(onButtonClick);
}
public void goToSignUpOne(View view) {
setContentView(R.layout.activity_sign_up_one);
}
}
In the XML, you remove the line android:onClick="goToSignUpOne" under your Button, and then that should work.
In case you didn't only want to change the view, but start a new activity instead, just make the goToSignUpOne(View) function like this:
public void goToSignUpOne(View view) {
startActivity(new Intent(this, SignUpOneActivity.class));
}
Hope this is what you were looking for! :D
I am trying to implement onclick when i click first button to go to new window but when i click it breakes program. I have the error :
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x97154160, error=EGL_BAD_MATCH.
This is main :
package FirstApp.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{
TextView text;
Button button;
Button button2;
boolean onClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
}
#Override
public void onClick(View view) {
if(view.getId() == R.id.button){
text.setText("Prvi button je kliknut");
}
else if(view.getId() == R.id.button2){
/* Intent i = new Intent(this, DetailActivity.class);
i.putExtra("TEXT", input.getText().toString());
startActivity(i);/*/
text.setText("Drugi button je kliknut");
}
}
}
And this is second class :
package dvino.myapplication;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
/**
* Created by zeroOne on 26.5.2017..
*/
public class Dvino extends Activity{
#Override
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.dvino);
}
}
EDIT-this is error message that i recive when the program crashes:
<'E/AndroidRuntime: FATAL EXCEPTION: main
Process: dvino.myapplication, PID: 3044
java.lang.IllegalStateException: Could not find method
OnClick(View) in a parent or ancestor Context for android:onClick attribute
defined on view class android.support.v7.widget.AppCompatButton with id
'button'
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at
android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)'
Main xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="dvino.myapplication.MainActivity">
<Button
android:text="D'vino"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_marginLeft="18dp"
android:layout_alignBottom="#+id/button2"
android:layout_alignParentEnd="true"
android:layout_marginBottom="49dp"
android:onClick="OnClick"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/dvino"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:text="D'vino Kitchen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:layout_marginLeft="18dp"
android:layout_marginBottom="12dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:onClick="onClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_message"
android:id="#+id/text"
android:textAppearance="?android:textAppearanceLarge"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp" />
</RelativeLayout>
You program is crashing because of a null pointer exception when you try to call textview.setText() - textview is null because you haven't initialized it yet.
the same way you did button = (Button)findView... you have to do that for the textview also
Your code have TextView problems and
this parts:
public class MainActivity extends AppCompatActivity implements
View.OnClickListener{}
change on:
public class MainActivity extends Activity implements
View.OnClickListener{
edit---
Okey.Try it.
public class MainActivity extends Activity {
private Button button;
private TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button) findViewById(R.id.testB);
text=(TextView) findViewById(R.id.testT);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
button.setText("ClickedButton");
text.setText("ChangedText");
}
});
}
}
Change onclick="OnClick"
To onclick="onClick" for your button widget with ID="button"
Remember use capital letters carefully ;)
In your onCreate method after the initialization of the button button=(Button)findViewBy... put button.onClicklistener(this);
java.lang.IllegalStateException: Could not find method OnClick(View)
in a parent or ancestor Context for android:onClick attribute defined
on view class android.support.v7.widget.AppCompatButton with id
'button'
1. In your layout XML, change Button attribute android:onClick value to onClick.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
..........
...............
tools:context="dvino.myapplication.MainActivity">
<Button
android:id="#+id/button"
..........
.................
android:onClick="onClick" />
<Button
android:id="#+id/button2"
..........
................
android:onClick="onClick" />
...........
.................
</RelativeLayout>
2. In your MainActivity, you are trying to set Text on an null TextView object as you did not get the reference of it from your attached layout. It may cause an NullPointerException. To avoid this exception update your activity onCreate() method as below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.text);
}
Hope this will help~
I'm brand new to Android Studio and for whatever reason am experiencing really weird errors if I could please get some help.
I have very basic code that is supposed to, on button click change the text of my button from "button" to "clicked!". However everytime I press the button, the app crashes and I get "Appname has stopped" on the emulator.
What is incredibly weird is that in my activity_main.xml Design view, the onClick dropdown shows two functions of the same name (https://puu.sh/t2h5I/42ad4379d6.png)
H owever the code only works when the bottom one is selected. AND each time I run the app, it deselects the bottom one and reselects the top, only to stop working.
Here is my MainActivity:
package com.example.john.ameladay;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void buttonPress(View v){
melButtonCode = (Button) v;
((Button) v).setText("Has been clicked!");
}
}
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.john.ameladay.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/mainText"
android:id="#+id/textView" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/mela"
android:id="#+id/melPhoto" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginBottom="43dp"
android:id="#+id/button"
android:onClick="buttonPress (MainActivity)" />
Any help would be greatly appreciated!!
Simple. You should write:
android:onClick="buttonPress"
Why happened
If you wrote buttonPress (MainActivity), Android tries to find buttonPress (MainActivity) method (not MainActivity.buttonPress()), but MainActivity doesn't have buttonPress (MainActivity) method. So the error happened.
Simply replace this Tag in button
Remove this
android:onClick="buttonPress (MainActivity)"
And Paste This
android:onClick="buttonPress"
A better way to do it is, get a reference to the button in your Java code using findViewById() method and set an OnClickListener to the button.
For your current problem, use
android:onClick="buttonPress"
instead of
android:onClick="buttonPress (MainActivity)"
According to me this is the better way to set click on button
public class MainActivity extends AppCompatActivity {
public Button melButtonCode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
melButtonCode = (Button).findViewById(R.id.button);//find button by Id
melButtonCode.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
melButtonCode.setText("Has been clicked!");
}
});
}
}
For your problem
replace it
android:onClick="buttonPress (MainActivity)"
With
android:onClick="buttonPress"
Make the method public - protected works when you are in instantrun mode, but not when not. No idea why!
I want to create a very simple app. The app should consists of 2 views. In both views are one button. By clicking the button the view should change.
Here is my folder structure
I have two activitys and two activity_layouts. As you can see OverviewActivity isn't inside the activity folder. When I place it into the folder I get this structur:
Why is the activity folder away? Can you give me a little explanation?
Ok, but in this question I use the first folder structur.
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="company.useradministration.activity.LoginActivity">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="btnOverview"
android:text="go to overview"/>
</RelativeLayout>
activity_overview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".activity.OverviewActivity">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="btnLogin"
android:text="go to login"/>
</RelativeLayout>
LoginActivity
package company.useradministration.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import company.useradministration.R;
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
}
OverviewActivity
package company.useradministration.activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import company.useradministration.R;
public class OverviewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
}
Okay. When I start the app, I see a button with the text "go to view". After pressing this button, the view changed. Now I see a button with the text "go to overview". When I press this button, the app crashes with the error:
java.lang.IllegalStateException: Could not find method btnOverview(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18439)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5085)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
at dalvik.system.NativeStart.main(Native Method)
UPDATE:
Here is the explained answer:
You have a activity_overview.xml with it's class OverviewActivity.
In your activity_overview.xml you set the android:onClick="btnLogin" to the button and you have:
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
in your OverviewActivity so when user clicks this button it changes the contentview successfully.
The question here is:
Why does the app crashes when you click on the button in activity_login.xml to change the contentview back to activity_overview.xml?
Simple. Because you have set the onclick method in your LoginActivity but you aren't switching to that activity but just changing the layout. So when the button to go back to overview content is clicked the app searches for the function in you OverviewActivity because the activity didn't swithed to LoginActivity. It has just changed the content and there is no function called btnOverview in your OverviewActivity. So here you have 2 solutions:
1- Create two RelativeLayouts in just one xml file and than setVisibility of each layouts on button click.
So for that you have to:
Create only one activity instead of two different and in it's xml file use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/overview"
android:visibility="gone">
<Button
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="go to login"
android:id="#+id/go_to_login"
android:layout_height="wrap_content" />
//Do your stuff here for overview content
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/login">
<Button
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="go to overview"
android:id="#+id/go_to_overview"
android:layout_height="wrap_content" />
//Do your stuff here for login content
</RelativeLayout>
</RelativeLayout>
This code has 2 RelativeLayouts. The first one with id:overview is of the overview content and it's visibility is gone and the second RelativeLayout with id:login has the visibility visible by default (Change the two layouts visibility as desired).
Than in your MainActivty.java (call it as you wish) you can change the visibility of these views like this:
public class MainActivity extends AppCompatActivity {
Button goToOverview, goToLogin;
RelativeLayout overview, login;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remember to change the content to match your xml file name
setContentView(R.layout.activity_main);
//Buttons
goToOverview = (Button) findViewById(R.id.go_to_overview);
goToLogin = (Button) findViewById(R.id.go_to_login);
//Layouts
overview = (RelativeLayout) findViewById(R.id.overview);
login = (RelativeLayout) findViewById(R.id.login);
goToOverview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login.setVisibility(View.GONE);
overview.setVisibility(View.VISIBLE);
}
});
goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
overview.setVisibility(View.GONE);
login.setVisibility(View.VISIBLE);
}
});
}
In this way you can switch to two different layout without any problem.
Edit: You can achieve this also with this solution but the difference between the first solution and this solution is just that you will have 2 xml files instead of one.
2nd- You will have one java class file (in this example I will take OverviewActivityas example) and 2 different layout files (Which you already have: activity_login.xml and activity_overview.xml).
So in your activity_overview.xml change this line: tools:context=".activity.OverviewActivity" to tools:context=".OverviewActivity" as you said that you are using the first folder structure where the OverviewActivity file is out of the activity folder.
Than in your OverviewActivity make the following changes:
public class OverviewActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
}
public void btnLogin(View view){
setContentView(R.layout.activity_login);
}
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
}
That's it. Please comment if you are having any problem with the code. I will be glad to help your further more.
Hope this can solve your issue.
Regards
I think this is your problem:
public void btnOverview(View view){
setContentView(R.layout.activity_overview);
}
This changes the layout for the current activity LoginActivity to activity_overview which contains the button thats looking for the btnLogin method
You are successfully changing the layout, but your staying in the same activity, so the new layout is looking for a method that exists in the other activity
Instead change the above code to this:
public void btnOverview(View view){
Intent intent = new Intent(this, OverviewActivity.class);
startActivity(intent);
}
And in OverviewActivity
public void btnLogin(View view){
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
I think you can use this as a help. I'm newbie myself to android java programming, but I believe you have to create intent to move to another activity. Additionally be sure to add your Activity to AndroidManifest.xml. That's all I think. If I'm wrong I'll be happy if someone more experienced will correct me :)
I am having an issue where my Button (id i_am_a_problem) which is declared in the fragment's layout XML, is generating an error when the button is clicked. Android tries to call the onClick method public void calculate(View v) but is unable to find it (despite it being declared in MainActivity). Details of my code are below, and the error is after that. Please help me determine why android is unable to find the onClick method. Thanks.
There is really not anything else going on in this code (fresh new project)
MainActivity.java
package supercali.FRAG.alistic;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) { /*snip - irrelevance*/ }
#Override
public boolean onOptionsItemSelected(MenuItem item) { /*snip - irrelevance*/ }
public void calculcate(View v){
TextView tv = (TextView)findViewById(R.id.results);
tv.setText(calculate_value());
}
private String calculate_value(){
return "Abra Kadabra";
}
}
MainActivity's layout just contains a fragment
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragment"
android:name="supercali.FRAG.alistic.MainActivityFragment"
tools:layout="#layout/fragment_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And the layout for that Fragment is:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivityFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:onClick="calculate"
android:id="#+id/i_am_a_problem"/>
<TextView android:text="#string/results_pending" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/results"/>
</LinearLayout>
The problem Button is just there ^^ with id i_am_a_problem
LogCat:
06-18 09:49:41.280 31642-31642/supercali.FRAG.alistic E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: supercali.FRAG.alistic, PID: 31642
java.lang.IllegalStateException: Could not find method calculate(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton
at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4441)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4405)
at android.view.View.performClick(View.java:5147)
at android.view.View$PerformClick.run(View.java:21069)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5401)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:615)
You misspelled the method.
the XML is looking for calculate but your method is calculcate
Your onCLick method is called "calculcate" in the Activity. In the XML it's "calculate".
I faced the same error but my onClick method was overridden from my base activity class and when I override it with editor help its signature became protected and hence was the problem.
Problem was resolved when I made it public.
Write the Method name correctly:
public void calculate(View v){
TextView tv = (TextView)findViewById(R.id.results);
tv.setText(calculate_value());
}
Just both spelling are different....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calculate"
android:onClick="calculcate"
android:id="#+id/i_am_a_problem"/>
I just put android:onClick="calculcate"
While you used word "calculcate"
public void calculcate(View v){
.....
}
And you used android:onClick="calculate"
You misspelled calculate. And if that doesn't work, try View view instead of View v.
Actually, In AndroidManifest file to verify name correct or not. AndroidManifest shown read color line if xml file name and class file name not relate.
Base on the problem, onClickListener can't find your class files because of wrong name. Error will only show Onclick error. This problem could confuse that you may set wrong onClickListener but actual problem is you set wrong name in xml file and activity class.
Sometime, you will not see error in xml and activity files because of same android name. In this case you can verify easily what was wrong in AndroidManifest file.