Why "OnClickListener" is not working - android

I can't understand why this simple code is not working.
This code suppose to set the color of the button's text to blue, that's all.
I only added to a new created project the lines :
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
The full code :
package com.example.b;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}

Most likely the button is in the fragment and not the activity. If so, move your findViewById and your setonclick code into your PlaceHolderFragment.

Related

How can i switch activity using Button?

How can i switch activity using Button? this is my problem:
package net.example.finals;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements OnClickListener {
Button bq;
Button bw;
Button be;
Button br;
Button bt;
Button by;
Button bu;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_second,
container, false);
bq=(Button)rootView.findViewById(R.id.btn1);
bq.setOnClickListener(this);
bw=(Button)rootView.findViewById(R.id.btn2);
bw.setOnClickListener(this);
be=(Button)rootView.findViewById(R.id.btn3);
be.setOnClickListener(this);
br=(Button)rootView.findViewById(R.id.btn4);
br.setOnClickListener(this);
bt=(Button)rootView.findViewById(R.id.btn5);
bt.setOnClickListener(this);
by=(Button)rootView.findViewById(R.id.btn6);
by.setOnClickListener(this);
bu=(Button)rootView.findViewById(R.id.btn7);
bu.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//i dont know what to put here
case R.id.btn1:
if(bq.callOnClick()==true){
Intent myOwnIntent = new Intent(getActivity(), Second.class);
startActivity(myOwnIntent);
}
break;
}
}
}
Why did you use "if(bq.callOnClick()==true)" this?
Android use a lots of java code, first you need to learn java properly.
AND Google too
#Override
public void onClick(View v) {
int id = v.getId();
switch(id){
case R.id.btn1:
Intent myOwnIntent = new Intent(YourActivityName.this, Second.class);
startActivity(myOwnIntent);
break;
}
}
Try this one.
How to start new activity on button click
Start another activity by clicking a button
Android eclipse how to start a new activity on button click

Error on create a Button Listener

My intent is to get text from an EditText and view it in a Toast message. The code is this:
package com.example.primaapplicazione;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
public void fromEditToToast()
{
EditText e = (EditText)findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
If I call the method "fromEditToToast()" in onCreate() without the "setOnClickListener()", it compiles properly and the app opens. If I call the same method with that function on the emulator app doesn't open, showing this message "Unfortunately, PrimaApplicazione has stopped.
What should I have to do?
I have seen this is a very common error, The PlaceholderFragment uses the fragment_main.xml that must contain the elements editText1 and button1, probably you have that elements defined in your activity_main.xml
It may be that views are not being instantiated while the activity has started. Device works faster than emulator so it might work in a device while not in emulator.
Implement your onClickListener directly in fragment and show the toast from there, it will work (assuming EditText and Button lies in fragment).
Try following code in your fragment and call the following function in your fragment's onViewCreated function to set the button's listener : -
private void set buttonOnClickListener()
{
EditText e = (EditText)getView().findViewById(R.id.editText1);
Button b = (Button)getView().findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//your code goes here;
}
});
}

Android:ActionBarActivity findViewById return NULL

I can't to get the button from fragment, what's wrong?
Recieve a uncaught exception - nullpointerexception;
Button can't be found.
Early all will be find, but after update android sdk, all is changed ((
package com.example.test;
import java.io.IOException;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener {
Button play, pause;
MediaPlayer mplayer;
RadioGroup rgroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MyTAG", "1");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mplayer = new MediaPlayer();
rgroup = (RadioGroup) findViewById(R.id.radioGroup1);
play = (Button) findViewById(R.id.play);
pause = (Button) findViewById(R.id.pause);
play.setOnClickListener(this);
pause.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
p.s. sorry for my english
You are trying to search inside Activity's layout, and your widgets are inside a Fragment's layout.
Put your logic inside your PlaceHolderFragment class:
public static class PlaceholderFragment extends Fragment implements OnClickListener {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
mplayer = new MediaPlayer();
rgroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
play = (Button) rootView.findViewById(R.id.play);
pause = (Button) rootView.findViewById(R.id.pause);
play.setOnClickListener(this);
pause.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
}
}
P.S. Салам алейкум =)

Why dont my app stop working?

I tried to run this simple code on emulator but it gives error right at the beginning. Helps appreciated. It gives a fatal error in logcat. It says "application has stopped". I get these errors:
"03-22 17:43:53.315: E/AndroidRuntime(1283): Process: com.example.deneme, PID: 1283"
"03-22 17:43:53.315: E/AndroidRuntime(1283): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.deneme/com.example.deneme.MainActivity}: java.lang.NullPointerException"
package com.example.deneme;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
You are assigning values to button and txt before the view has been created in onCreate(). You shouldn't assign them until after the call to setContentView(R.layout.activity_main).
You need to put button and textview in onCreate.
Try the following
public class MainActivity extends Activity {
final Button button; // Don't call find view by ID here;
EditText txt; // Don't call find view by ID here;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById (R.id.editText1); // Instead call it after setContentView
button = (Button) findViewById(R.id.button1); // Instead call it after setContentView
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// if (savedInstanceState == null) {
// getFragmentManager().beginTransaction()
// .add(R.id.container, new PlaceholderFragment())
// .commit();
// }
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
These lines will give you errors:
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
You cannot initialize the Button or the EditText outside of any methods because the layout has not been set. You can, however, declare them outside of any methods and then initialize them inside of your onCreate method just after you have set the layout (the view). Like so:
// declare them at a class level
Button button;
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize them here
button = (Button) findViewById(R.id.button1);
txt = (EditText) findViewById (R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
...
Then button cannot be final, though.

cannot resolve method setVisibilty(int)

I am new to android development.I am trying to create an activity method on clicking a button.
Below is my MainActivity.java file code.
I am getting errors
1."cannot resolve method'setVisibility (int)'" and
2."cannot resolve symbol 'TextView'"
please guide.
MainActivity.java
package com.AndroidLove;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public void onLoveButtonClicked(View view) {
TextView textView =(TextView) findViewById(R.id.textView);
textView.setVisibility(View.VISIBLE);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle action bar item clicks here. The action bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
Import this package as well
import android.widget.TextView;

Categories

Resources