ButtonClick is breaking program - android

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~

Related

how can I show log on onclick of a button in android?

Trying to add just onClick function run in the android studio. I want to show text message in log by clicking on my button but the desired result doesn't come.
Please help.
activity_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:layout_width="match_parent"
android:layout_height="match_parent"
tools:onClick="clickFunction"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:text="Button" />
</RelativeLayout>
mainActivity.java file
package com.example.jewelcsebu.hellotest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public void clickFunction(View view){
Log.i("Info", "Button Pressed");
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I just made onClick function to run. but this given error below:
2018-12-06 19:40:12.700 1808-1808/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2018-12-06 19:40:19.252 1716-1745/? E/storaged: getDiskStats failed with result NOT_SUPPORTED and size 0
2018-12-06 19:40:37.061 1890-1911/system_process E/memtrack: Couldn't load memtrack module
You need to replace tools:onClick with android:onClick
And I recommend you to put android:onClick inside the Button element like this:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="139dp"
android:onClick="clickFunction"
android:text="Button" />
Or use the OnClickListener Callback like this:
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickFunction();
}
});
public void clickFunction(View view){
Log.i("Info", "Button Pressed");
}
I faced the same issue and I just import class (import android.widget.Button;)
Like this :
import android.widget.Button;
and I didn't change anything to log.i(tag: "info", msg: "Button Pressed!");
it works for me

Button onClick() only matches in mainActivity [duplicate]

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

Android can't find my OnClick method

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.

Base Activity Button Click's not working in child activities

I am new to android. In my app, having common header.
I have created common header.xml and include that header.xml in all the activites.
But the button clicks [listener] not working.
How to solve the issue. this is the code.
Header.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Home" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="Accounts" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:text="History" />
</LinearLayout>
Home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header" />
<EditText
android:id="#+id/txtOTPCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnVerify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="verify" />
</LinearLayout>
BaseHeaderActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class BaseHeaderActivity extends Activity
{
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.header);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
System.out.println("Home button Action");
}
});
}
}
Home Activity.java
import android.os.Bundle;
import android.widget.Button;
public class HomeActivity extends BaseHeaderActivity
{
Button btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
btnVerify = (Button)findViewById(R.id.btnVerify);
}
}
base activities button clicks not working..!
Kindly suggest the solution.
Thanks in advance,
Arun
You don't have to create two different activities for this. Create one activity and use all buttons (including button in header.xml) similarly
public class MainActivity extends Activity {
Button btn1,btnVerify;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Home button Action", Toast.LENGTH_SHORT).show();
}
});
btnVerify = (Button)findViewById(R.id.btnVerify);
btnVerify.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Programming stuf
//show the another view
Toast.makeText(MainActivity.this, "Button Verify Action", Toast.LENGTH_SHORT).show();
}
});
}
}
You can not use one layout xml file with single activity only.
Note - If you still want to create two different activities - one base and one child then you need to use setContentView() method in base activity with main layout file (Home.xml in your case) and extend it with child activity. Remember in this case dont use setContentView() method in Child Activity. This scenario is not preferred generally unless we want to create several child of same activity.

Null Pointer Exception when Getting EditText Value

I am trying to get EditText value through below code. The activity is used for adding two strings. In the layout, I already put onSave and onCancel method. But when I press button linked to onSave, it shows null pointer exception error.
Activity:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class AddTimeActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.time_item);
}
public void onCancel(View view){
this.setResult(RESULT_CANCELED);
finish();
}
public void onSave(View view){
Intent intent = getIntent();
EditText timeView = (EditText)view.findViewById(R.id.time_view);
String time = timeView.getText().toString();
intent.putExtra(TimeTrackerActivity.TIME_KEY, time);
EditText notesView = (EditText)view.findViewById(R.id.notes_view);
String notes = notesView.getText().toString();
intent.putExtra(TimeTrackerActivity.NOTES_KEY, notes);
this.setResult(RESULT_OK, intent);
finish();
}
}
Layout:
...
<EditText
android:id="#+id/timeView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</EditText>
<EditText
android:id="#+id/notes_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:gravity="top"
android:layout_weight="1"
android:layout_marginBottom="10dp"/>
...
<Button
android:onClick="onSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save" />
<Button
android:onClick="onCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
...
Error Message from Debugger
E/AndroidRuntime(1123): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1123): at com.timetracker.AddTimeActivity.onSave(AddTimeActivity.java:34)
I tried to check and the value of timeView through debugger and it is null.
Could anybody help?
You are looking for the id time_view (findViewById(R.id.time_view)), but you declare in the XML the ID timeView (android:id="#+id/timeView"). Make them the same and it should fix it.
Also, you should not call it on the view, but on the activity, e.g:
EditText timeView = (EditText)findViewById(R.id.time_view);
Your xml has
android:id="#+id/timeView"
But your code has
EditText timeView = (EditText)view.findViewById(R.id.time_view);
Change above line to.
EditText timeView = (EditText)view.findViewById(R.id.timeView);

Categories

Resources