I've been working on developing an app for a while, and everything was going well. However, when I added an onCLickListener for a button in one of my .xml files(not main.xml), it started crashing as soon as I tried to open it in the emulator. All the code for the onClickListener is the same as for the other two buttons I have. here's the code for my problematic listener:
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(bt3listener);
private OnClickListener bt3listener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.main);
}
};
The only way that any of this code differs from my other onClickListeners is that it references a button in a different file than main.xml. It crashes before displays any of the ui except a black screen with my project name on the top, and gives me a prompt that says "un
for a button in one of my .xml files(not main.xml),
It have to be int the layout you set with setContentView(), in order to retrieve it with findViewById() using this or use an inflater in order to get the layout in which the Button is declared, and use the returned view with findViewById().
viewReturnedByInflater.findViewById()
You need to initialise the OnClickListener before you assign it to the button.
Try this
private OnClickListener bt3listener = new OnClickListener(){
public void onClick(View v){
setContentView(R.layout.main);
}
};
Button button3 = (Button)findViewById(R.id.button3);
button3.setOnClickListener(bt3listener);
It gives you the error because when Button button3 = (Button)findViewById(R.id.button3);
it tries to find the button, but since the button is in another xml layout, it crashes.
You should infalte the other layout or add the button on the main layout
Related
I'm new to android development. I've a doubt.
I know that you can add a button and initialize it like
Button b1=(Button) findViewById(R.id.button1);
and I can also give a unction name in the XML file.
android:onClick="click_event"
My doubt is, which is the best and efficient way?
like it says that its better to use #string resource instead of a hard-coded one.
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1
So in your xml you would have somewhere
<Button
android:id="#+id/button1"
<!-- other properties -->
/>
You can add a Button programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="#+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
#Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener in your Activity declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
#Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.
I'm new to android development. I've a doubt.
I know that you can add a button and initialize it like
Button b1=(Button) findViewById(R.id.button1);
and I can also give a unction name in the XML file.
android:onClick="click_event"
My doubt is, which is the best and efficient way?
like it says that its better to use #string resource instead of a hard-coded one.
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1
So in your xml you would have somewhere
<Button
android:id="#+id/button1"
<!-- other properties -->
/>
You can add a Button programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="#+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
#Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener in your Activity declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
#Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.
I'm new to android development. I've a doubt.
I know that you can add a button and initialize it like
Button b1=(Button) findViewById(R.id.button1);
and I can also give a unction name in the XML file.
android:onClick="click_event"
My doubt is, which is the best and efficient way?
like it says that its better to use #string resource instead of a hard-coded one.
I think you are confused. The examples you give are two different things.
Adding a Button
This line
Button b1=(Button) findViewById(R.id.button1);
doesn't add a Button. It declares and initializes an instance of Button which refers to a Button in your currently inflated xml which has an id of button1
So in your xml you would have somewhere
<Button
android:id="#+id/button1"
<!-- other properties -->
/>
You can add a Button programmatically with
Button bt1 = new Button(this);
// give it properties
But it is generally easier to do in xml because here you have to programmatically give it parameters, properties, and add it to an inflated layout
OnClick
As far as the onClick() it depends on what you feel is the easiest and best in your situation. I like to declare it in the xml like that often but you can do it several ways. Using this method you just have to be sure that you have a function like this that is public and takes only one parameter and that parameter must be a View
public void clickEvent(View v)
{
// code here
}
I also changed the name so your xml would be like
<Button
android:id="#+id/button1"
<!-- other properties -->
android:onClick="clickEvent"/>
You also can set onClick() in your Java with something like
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// code here
}
});
or
Button b1=(Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
#Override
public void onClick(View v)
{
// code here
}
Note that the last way you will need to add implements OnClickListener in your Activity declaration
public class MyActivity extends Activity implements OnClickListener
{
You can also create your own click Listener by changing it to something like
b1.setOnClickListener(myBtnClick);
then create an instance of it with something like
public OnClickListener myBtnClick = new OnClickListener()
{
#Override
public void onClick(View v)
{
// click code here
}
};
You can use this for multiple Buttons and switch on the id or check the View param to know which Button was clicked or create separate Listeners for different Buttons.
Let's pretend this was my Java Class...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button ScreentwoGameButton = (Button) findViewById(R.id.screentwo);
ScreentwoGameButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent ScreentwoGameIntent = new Intent(Main.this, Screentwo.class);
startActivity(StartGameIntent);
}
});
How do i use this code below but the right way like.
So let's put an example if I click screentwo button the screentwo.xml will show and it will allow me to click inside if any buttons are available. Instead just stare what's in the layout.
I don't want to use the Activity to activity cause the whole point is i'm trying to avoid the flashing looking feel going to another java class.
If you look at the moron test game on Android it says example: press the blue button then red and then green, so if u press the blue button the screen will remain and not flash at all but the image of the blue button will disappear and I'm allowed to click the red and then green.
Hope that helped.
Thanks
Wahid
Button ScreentwoButton = (Button) findViewById(R.id.screentwo);
ScreentwoButton.setOnClickListener(new OnClickListener() {
private Uri Uri;
#Override
public void onClick(View v) {
setContentView(R.layout.Screentwo);
Uri uri=Uri;
Intent i=new Intent(Intent.ACTION_VIEW, uri);
mSoundManager.playSound(1);
}
});
try to use:
setContentView(R.layout.next layout); in your button click.
You could use the viewflipper class and add the different layouts as childs to the viewflipper
and set the active child. Using setcontentView will be trouble some when you use findViewById for a old layout. As findViewById will look in the layout that is specified by setContentView
I am new to Android development. Please excuse me If my question is very simple.
I have tried to create a button on my Android Layout view using XML. Now within the Activity class I am trying to get the button and add a on click listner to it. This is working fine without any issues.
On similar lines on the button click i explained previously I have a dialog being popped up. In this dialog I have a ImageButton. On click of this Image button I am trying to set a on click listner using the below code.
The Activity on create is as below
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button button = (Button) findViewById(R.id.btnAdd);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
final Button btnAdd = (Button) findViewById(R.id.btnAdd);
if(v==btnAdd) {
dialog = new Dialog(this);
dialog.setContentView(R.layout.add_dialog);
dialog.setTitle("Test Title.");
dialog.setCancelable(true);
dialog.show();
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
try {
Log.i("Log","1");
button.setOnClickListener(this);
Log.i("Log","2");
}
catch(Exception e)
{
Log.i("Log","3");
dialog.dismiss();
//Dialog d = new Dialog(this);
//d.setTitle("test.");
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.getLocalizedMessage(),Toast.LENGTH_LONG).show();
Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
Log.i("Log","4");
//d.show();
Log.i("Log","5");
}
}
}
In the above I get the Log in this sequence. 1,3,4,5. I dont get the 2. In the toast i get message of blank, blank followed by java.lang.Nullexception.
But this when executed gives me a force close pop up. Please advice how to do this. Or is there any workaround for the same? I need to have a dialog box to come on a button click, and then within the dialog I need to have more than one option of buttons. For each of buttons in the dialog I need to perform different activities. Any kind of help or advice is appreciable. Thank you in advance for your time and help.
Most probably you are trying to retrieve the button from the Activity class. It returns null because this method will only retrieve resources attached to the Activity (by using the method setContentView).
You have two options:
You can inflate the dialog layout using a LayoutInflater
If you are extending the Dialog class, add the listener inside that class instead.
Edit after the update:
As I said above, the problem is:
final ImageButton button = (ImageButton) findViewById(R.id.imageButton1);
because imageButton1 is not part of the layout in the activity. Just replace it by:
final ImageButton button = (ImageButton) dialog.findViewById(R.id.imageButton1);