I have two radiogroup on FormVisitMapping.java, i put one of them on dialogbox which is trigger by button onClick(). but i get an error nullpointerexception only on my radiogroup where in dialog. even i have a similiar code on both of them. i have no idea why this happenned. i've read a question with similiar issue, but nothing solve my problem. here is my code :
btninvoice= (Button) findViewById(R.id.btninvoice);
btninvoice.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
final Dialog dialog3 = new Dialog(FormVisitMapping.this);
dialog3.setContentView(R.layout.penagihan);
dialog3.setTitle("Isi Data Penagihan:");
dialog3.show();
//other stuff
RadioGroup tes=(RadioGroup) findViewById(R.id.penagihan);
switch (tes.getCheckedRadioButtonId()) {
case R.id.rbtandaterima:
systemofpayment = "Tanda Terima";
break;
case R.id.rbtagihlangsung:
systemofpayment="Tagih Langsung";
break;
default:
break;
}
}
});
i've got NullPointerException on this line :
switch (tes.getCheckedRadioButtonId()) {
every help will be apriciated.thank you
Your using findViewById() on a wrong view. You haven't included your XML but I'm guessing that you want to find it in dialog's layout.
Change
RadioGroup tes=(RadioGroup) findViewById(R.id.penagihan);
to
RadioGroup tes=(RadioGroup) dialog3.findViewById(R.id.penagihan);
Related
Currently I am working on an app that has somewhat of a basic calculator layout and functions. I have 8 edittext and a grid of 9 buttons. With the onClicklistener method is there a better way of handling this instead of nesting a long if or switch statement for each Edittext?
private View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId() /*to get clicked view id**/) {
case R.id.btn_1:
if edt_1.hasFocus() {
edt_1 = btn_1.getText()
}
if edt_2.hasFocus() {
edt_2 = btn_1.getText()
}
break;
case R.id.btn_2:
if edt_1.hasFocus() {
edt_1 = btn_1.getText()
}
if edt_2.hasFocus() {
edt_2 = btn_1.getText()
}
break;
default:
break;
}
}
I was thinking maybe it would be better to use a hasFocus() method and listen for clicks from there or a while loop, then maybe pass a variable?
Thanks in advance..
Hear is a way where you can small your code. just add android:onClick="onClick" in every button of your grid layout and do following in java file,
public void onClick(View v) {
Button b=(Button)v;
String s+=b.getText();
}
This two line get text from your button and set to the String s.
if you want to create calculator that function real-time calculation like this calculator
APK:https://play.google.com/store/apps/details?id=com.androchunk.calculator
then you can find free source code of calculator project as well as tutorial video that help you to create calculator and understand working of calculator.
Tutorial videos:https://www.youtube.com/playlist?list=PLdMmtAIsH0KYiKrdpbzat6t96Nb1_k3_1
Just wondering can anyone solve this problem about adding listeners to imagebuttons at runtime. I'm presuming it has got something to do with the "this" parameter passed to setOnClickListener as we are already in a onClickListener.
My fragment implements onclicklistener. The onClick methods work for imagebuttons known at compile time, just not for the ones that are defined after inflating a prompt view. What seems to happen is the prompt layout seems to be recreated and added onto the back stack.
Basically the onclicklistener for mWhatsappshare,mEmail share does not react as I might expect. I put a stackoverflow error in the onclick method previously and when I clicked these imagebuttons my application did not crash. This means that the listener is in fact not registered (or at least not correctly) at
mwhatsapp.setonclicklistener(this)
By the way, I do not want to set separate listener like mWhatsapp.setonclicklistner(new View.onClickListener()){ for each imagebutton as it is too cumbersome and I want each listener to be handled in onclick().
Thank you
mOtherShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeColorFilters();
mOtherShare.setImageResource(R.drawable.other_pill_pressed);
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.share_prompt,null);
mWhatsappShare = (ImageButton) dialoglayout.findViewById(R.id.ib_whatsapp_share);
mEmailShare = (ImageButton) dialoglayout.findViewById(R.id.ib_email_share);
mFlickrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_flickr_share);
mTumblrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_tumblr_share);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
dlg = builder.show();
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ib_whatsapp_share:
sendIntent("com.whatsapp");
break;
case R.id.ib_email_share:
sendIntent("android.email");
break;
case R.id.ib_flickr_share:
sendIntent("com.yahoo.mobile.client.android.flickr");
break;
case R.id.ib_tumblr_share:
sendIntent("com.tumblr");
break;
}
}
``
On this block of code:
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
this is referring to the new OnClickListener you are creating - mOtherShare.setOnClickListener(new View.OnClickListener() and NOT your fragment's OnClickListener
To use the outer onClick, change this to YourFragment.this
I have two linear layouts present inside relativelayout. Each linear layout contains three text view. I want to write onclick event for all text views present in both linear layouts in general. Please advice.
in all TextView add the following attribute
android:onClick="onClick"
dont forget to set id to all TextView too
Then from your code
public void onClick(View v){
switch (v.getId()) {
case R.id.tv1:
// do somethong
break;
default:
break
}
Have a common click listener for all the text views. From the common click listener handle the click events of all the text views by the ID of the text view.
Sample FYR.
findViewById(R.id.textview1_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview2_id).setOnClickListener(commonClickListener);
findViewById(R.id.textview3_id).setOnClickListener(commonClickListener);
private OnClickListener commonClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int selectedItemId = v.getId();
switch (selectedItemId) {
case R.id.textview1_id:
// implement your code here.
break;
case R.id.textview2_id:
// implement your code here.
break;
case textview3_id:
// implement your code here.
break;
}
}`
I have been looking at some posts and still I cannot get mine code to work (I am a beginner) .. I am just tring to use the toast with my two buttons with a case switch .. When compiling it just crashes .. one something has an idea ?
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
TextView et = (TextView) findViewById(R.id.txtHeader);
Button btnAdd = (Button) findViewById(R.id.btnAdd);
Button btnDis = (Button) findViewById(R.id.btnDisplay);
btnAdd.setOnClickListener((OnClickListener) this);
btnDis.setOnClickListener((OnClickListener) this);
}
public void OnClick(View v) {
switch (v.getId()) {
case R.id.btnAdd:
// Toast msg = Toast.makeText(getBaseContext(), "Torben", Toast.LENGTH_LONG);
// msg.show();
break;
case R.id.btnDisplay:
// Toast msg1 = Toast.makeText(getBaseContext(), "Henriksen", Toast.LENGTH_LONG);
// msg1.show();
break;
default:
break;
}
}
I see two main problems:
((OnClickListener) this
Make sure your class implements OnClickListener because you never need to cast if you are actually implementing the interface.'
The declaration of the class should be something like:
public class MyActivity extends Activity implements OnClickListener
Then change OnClick to lowercase o.
#Override
public void onClick(View v) {
some log output would be helpful!
a wild guess is that your activiy does not implement OnClickListener, why else would you cast this to OnClickListener?
just check in your layout manifest that whether the buttons id are correct and given the same id which your are using and if it is then please update the question with the LogCat output.
And also check that the activity is defined in manifest because there is no mistake in your code to implemts the onclick listener for multiple buttons.
Enjoy!!
Example adding a button listener:
Button b = ((Button)findViewById(R.id.button_name));
b.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//do something
}
});
and make sure the button is defined in your xml file with the id #+id/button_name or #id/button_name (just make sure they match)
I am searching the solution, how to get the value of the button which is pressed.
When I try something like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button numb1 = ((Button)this.findViewById(R.id.numb1));
numb1.setOnClickListener(this);
}
public void onClickHandler(View v){
String pressed = null;
switch (v.getId()) {
case R.id.numb1:
pressed=numb1.getText().toString();
break;
//OR
case R.id.numb1:
pressed=R.id.numb1.getText().toString();
break;
}
new AlertDialog.Builder(this).setTitle("Info").setMessage(pressed).setNeutralButton("Okey", null).show();
}
Both cases in switch are unfortunately bad.
And I still can't get the value of the pressed button... Can you help me please with this problem yet?
Thank you.
Well guys, I am pretty new in this area, but I solved this problem like that:
public void onClick(View view) {
int intID = view.getId();
Button button = (Button) findViewById(intID);
String message = button.getText().toString();
}
pressed=((Button)v).getText(); should do the job.
Also, let your activity implement View.OnClickListener and instead of onClickHandler() override the method public void onClickHandler(View v) with your implementation.
try this.
Button sender = (Button)v;
sender.text;
Looking at your code, is it possible you have two variables with the same name but different scopes causing you confusion?
In onCreate, you declare Button numb1, but onClickHandler appears to expect numb1 to have also been declared outside of onCreate. So, should onCreate just assign a value to numb1, instead of also declaring it?
At any rate, if you were to post a more complete code example, it may help others to identify the problem, instead of just guessing about how things are.