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!
Related
I'm trying to create a listener from the simplest kind but for some reason I can't.
Code:
package com.example.check;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "this is a test",
Toast.LENGTH_LONG).show();
}
});
}
}
And the .xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/buttton"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
It's a very simple listener but for some reason it doesn't work. The IDE doesn't detect any error, the problem is in this part of the code: new View.OnClickListener()
The IDE marks it in gray as it never been used in the code. I searched for days in every possible forum but no one has this problem.
Here's a Picture: new View.OnClickListener() is grey
If I put my mouse on: new View.OnClickListener() it suggests I replace it with lambda. If I do replace it, there's no more gray section but when I run the app the code inside the onClick(View view) function is not been executed.
When I run the app with new View.OnClickListener() or with lambda instead, I don't get any error messages.
I think the problem is somehow related to the Gradle but that's just an idea. Please someone help, I am really desperate because the code is correct and it's not working!
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 new to android programming and there are a few things I don't quite know how to do yet. I am doing a course on udemy and was trying to put together everything I have learned up to a certain point.
What I am trying to do is have the user click on a button (i have 12) and have it bring up a textField where they can enter two numbers. I just want to be able to get the user's two numbers and i'm pretty sure I can figure out the rest ( I hope). I just don't understand how to go about doing this.
Any help would be much appreciated. Basically all I want to do is to be able to have the user click on one of the 12 buttons and be asked to enter two values, then take those values and perform a calculation on it.
Your xml could be like this:
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/b_ok"
android:text="Click Me"/>
<EditText android:layout_width="fill_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/et_showme"/>
</LinearLayout>
and your activity could be like this:
package com.william.kinaan.welcomeback;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends Activity implements OnClickListener {
private Button b_ok;
private EditText et_showme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
initialize();
}
private void initialize() {
this.b_ok = (Button) findViewById(R.id.b_ok);
this.b_ok.setOnClickListener(this);
this.et_showme = (EditText) findViewById(R.id.et_showme);
}
#Override
public void onClick(View v) {
this.et_showme.setVisibility(View.VISIBLE);
}
}
Create an Activity that has 2 EditTexts
When user click a button the Activity is started and user can enter the Numbers
Try this.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_id"
android:visibility="invisible"
android:text="sample text"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:id="#+id/click"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
And in your activity, you can do like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button button = (Button) findViewById(R.id.click);
final EditText text = (EditText) findViewById(R.id.tv_id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
text.setVisibility(View.VISIBLE);
}
});
}
eveything looks to be fine, but I still get error when I try to change the view by pressing the button.
Here is code:
package com.example.testy;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
ViewFlipper flipper;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void clcik(View v) {
flipper.showNext();
}
}
And here is my XML:
<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:gravity="top" >
<ViewFlipper
android:id="#+id/viewFlipper1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="click"
android:text="Button!!" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView!!" />
</ViewFlipper>
</RelativeLayout>
Anyone knows what can be wrong in this code?
Thank you for answers!
Perhaps you should fix the name of your method to click ?
public void **clcik**(View v) {
flipper.showNext();
}
Due the wrong spell (clcik) in our activity code, you may be getting a Exception because Android can't find the click method.
And thanks to #yugidroid's answer I spot one more error on your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper1);
setContentView(R.layout.activity_main);
}
You call findViewById before even you have any Views (a call to setContentView) . You will get a NullPointException because of that.
I would recommend you to make a call to super.onCreate the very first line of your onCreate() method. That is what Google does.
Regarding setting the click listener on layout or creating a listener and setting on the code. Well, there is not much difference, although the latter is certainly faster as the first uses reflection, what has a higher cost than just calling a method.
First of all, make sure you call setContentView(R.layout.activity_main); after the super, its a good practice.
Your problem is that you set android:onClick="click" but you are refering the wrong method in Java (clcik doesn't exists).
I advice you to declare and set listeners in the activity, not in the xml.
I'm trying to get a simple onClick to fire from an ImageButton - it seems like a simple enough task, but I'm obviously missing something here.
Here is my java file:
package com.jlbeard.android.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class testapp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//handle the button press
ImageButton mainButton = (ImageButton) findViewById(R.id.mainButton);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//show message
Toast.makeText(testapp.this, "Button Pressed", Toast.LENGTH_LONG);
}
});
}
}
Here is my layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/whereToEat"
android:src="#drawable/where_to_eat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8px"
/>
<ImageButton
android:id="#+id/mainButton"
android:src="#drawable/main_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#null"
android:clickable="true"
android:onClick="mainButtonClick"
/>
</RelativeLayout>
It seems to me that I'm missing something simple... but can't seem to figure it out. Thanks!
You didn't run show() method on Toast object. Very common mistake :-)
You also might have a problem due to the manifest setting onClick
android:onClick="mainButtonClick"
If mainButtonClick exists on post 1.5 devices it may be called instead, overriding the one you're setting in code
In my case, the imageButton was displayed behind a list. Because the list was empty, the ImageButton was seen but onClick was never fired.
Adding android:elevation="5dp" in the screen xml solve my problem
Note that if I use Button instead of ImageButton, elevation is not required.