I've posted another question on this, but im asking another as the problem has been narrowed down some what. My problem is that I'm getting null pointer exception from inside the inner onClick at the line where the first String strTime is created. It has been suggested the reason for this is the poptest.xml not inflating properly. Can anyone see why this is happening?
I have this method:
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.newappt:
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.poptest);
dialog.setTitle("Create New Appointment");
dialog.setCancelable(true);
Button buttoncancel = (Button) dialog.findViewById(R.id.Button01);
buttoncancel.setOnClickListener(new OnClickListener() {
// on click for cancel button
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
Button buttonsave = (Button) dialog.findViewById(R.id.Button02);
buttonsave.setOnClickListener(new OnClickListener() {
// on click for save button
#Override
public void onClick(View v) {
String strTime = ((EditText) findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) findViewById(R.id.evntdet)).getText().toString();
cursor = getAppts();
addAppt(strTime, strTitle, strDet);
showAppts(cursor);
dialog.dismiss();
}
});
dialog.show();
break;
case R.id.delappt:
rmvall();
break;
}
}
Try this if your EditText are in the Dialog:
String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();
String strTime = ((EditText) dialog.findViewById(R.id.evnttime)).getText().toString();
String strTitle = ((EditText) dialog.findViewById(R.id.evnttitle)).getText().toString();
String strDet = ((EditText) dialog.findViewById(R.id.evntdet)).getText().toString();
my first guess would be that the findViewById inside the onClick listener is searching for R.id.evnttime as a child of buttonsave.
the findViewById method belongs to the View class and is therefore availlale to every sub class like Buttons, Layouts...
It is searching up to the last children of the view you use it on.
Possible solutions here:
find your views outside of the onClick method or use the findViewById function of the main view (dialog).
Related
I have this button to show a dialog which in turn has an EditText for the users to write something and then tap the done button to save it otherwise another button to close the dialog. The problem here is that I am not able to fetch that String that the user wrote. It returns me a null sting instead and causes the app to crash. Here's the code :
I call the dialog up on clicking on a button:
//Write Review Dialog Box
writeReviewBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("HAPPENED", "This Onclick is working");
try{
// Create custom dialog object
final Dialog dialog = new Dialog(GetReviewActivity.this);
// Include dialog's xml file
dialog.setContentView(R.layout.write_review_activity);
// Set dialog title
dialog.setTitle("Your review is valuable");
final EditText etWR = (EditText)findViewById(R.id.etWR);
Button writeButton = (Button) dialog.findViewById(R.id.buttonWR);
Button declineButton = (Button) dialog.findViewById(R.id.buttonNoThanks);
dialog.show();
//Done Button
writeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("HAPPENED: ","writeButton Block Is WORKING!");
Activity activity = GetReviewActivity.this;
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
String Review = etWR.getText().toString();
StoreAReview(Review);
}
});
}
});
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
});
final EditText etWR = (EditText)findViewById(R.id.etWR); is resolving to Activity#findViewById. But your dialog's layout is not attached to your activity window, it's attached to the dialog. so you have to inflate EditText from your dialog's view:
Try
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
instead of
final EditText etWR = (EditText)findViewById(R.id.etWR);
Been through this issue before. I tried initiating EditText with Dialog reference but this didn't work
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
Solution:
This is how I solved it:
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View dialogWR = getActivity().getLayoutInflater().inflate(R.layout.write_review_activity, null);
etWR = (EditText) dialogWR.findViewById(R.id.etWR);
}
You should initialize EditText
EditText etWR = (EditText)dailog.findViewById(R.id.etWR);
becoz your EditText is coming from dailog. So
U just miss the dialog.findViewById(R.id.etWR)
change this line
final EditText etWR = (EditText)findViewById(R.id.etWR);
to this one
final EditText etWR = (EditText)dialog.findViewById(R.id.etWR);
then, add click event code
First of all, I have searched in forum for "How to get text from edittext in other layout", and tried many other ways, then I decided to use LayoutInflater but it did not help.
The implementation of LayoutInflater (I follow this guide)
LayoutInflater factory = getLayoutInflater();
View regisText = factory.inflate(R.layout.regis, null);
EditText user = (EditText) regisText.findViewById(R.id.edt1);
String usr = user.getText().toString();
My parent layout is named activity_input.xml having 3 buttons (I do not set text button). I put only button Id for easy view.
<Button
android:id="#+id/btn1">
</Button>
<Button
android:id="#+id/btn2">
</Button>
<Button
android:id="#+id/btn3">
</Button>
After I click a button, one dialog appears to select value (1, 2 or 3) to set the button display text (1, 2 or 3) in activity_input.xml.
final Dialog dialog1 = new Dialog(user_input.this);
dialog1.setTitle("Select a Number");
dialog1.setContentView(R.layout.game_dialog);
dialog1.show();
button1 = (Button) dialog1.findViewById(R.id.btn1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
p1_btn.setText("1");
dialog1.dismiss();
}
});
The onClick works perfect.
But my problem is that I want to get the text of the button in parent layout again, I try to implement Layoutinflater but it does not work, here is the code.
LayoutInflater f1 = getLayoutInflater();
View v1 = f1.inflate(R.layout.activity_input, null);
Button btn1 = (Button) v1.findViewById(R.id.b1);
String t1 = btn1.getText().toString();
I check to make sure that the String t1 is retrieve successfully by print the string but it shows nothing
Hope you guide help me as soon as possible!
Thanks you very much.
You can make a static variable and store the result there, then access it from your dialog. So in your ActivityInput.java
public class ActivityInput extends Activity {
public static String input = "";
//code
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Button p1_btn = (Button) findViewById(x);
input = "1";
p1_btn.setText(input);
dialog1.dismiss();
}
});
}
Now in your other java class ActivityOutput.java:
public class ActivityOutput extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
//code
String result = ActivityInput.input;
//use the result string somewhere to display it in your app
}
}
I am making an android application. I have a TextField and a button. Based on the value of the textfield, as soon as the user clicks the button I want to make something. I have the code:
EditText et = (EditText)findViewById(R.id.editText1);
String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value = "a" ) {
//do something }
}
});
This however doesn't compile, saying "Cannot refer to a non-final variable value inside an inner class defined in a different method". Is there any way to fix this? Thanks a lot
Use
final String value = et.getText().toString();
and then use,
if(value.equals("a") {
}
If you want to compare the value of a string you should use the "equals" method instead of "=".
The code will look like this:
EditText et = (EditText)findViewById(R.id.editText1);
final String value = et.getText().toString();
ImageButton ib = (ImageButton)findViewById(R.id.imageButton1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (value.equals("a") ) {
//do something }
}
});
final String value = et.getText().toString();
I'm making a simple app which has two buttons and one edittext. When the buttons are clicked the edittext wil display values of my database. I tried this code but it did not work
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == bnt1) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
} else if (v == bnt2) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}
Please show me the problems.
It looks like you probably don't have a Button attached to your onClick() method which would result in a null pointer exception when you try to reference v inside the method. There are different ways of implementing this (programmatically or through xml). With what you show (not overriding the onClick() method) it looks like you would want to use the xml way which means you need to declare the function for your Buttons
<Button
android:id="#+id/btn1"
android:onClick="onClick"
then do the same for btn2. Then v will reference whichever Button was clicked. You can also do this programmatically but setting an onClickListener
Button btn1 = (Button) findViewById(R.id.btn1);
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
// some code
}
});
But then you have to do the same thing for btn2 so if you want to reuse the same function for both Buttons then the first method would be preferred.
Here is a link to a previous answer of mine convering the same thing
Use the below code:
btn1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharename = PreferenceManager.getDefaultSharedPreferences(this);
String na = sharename.getString("thename", "null");
edit.setText(na);
}
}
btn2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences sharedescribed = PreferenceManager.getDefaultSharedPreferences(this);
String dess = sharedescribed.getString("thedescribed", "null");
edit.setText(dess);
}
}
Try this code :
SharedPreferences sharename = this.getSharedPreferences("sharename", MODE_WORLD_READABLE);
String prefName = sharename.getString("thename", 0);
edit.setText(prefName);
I guess the Exception is NullPointerException. Please check your share preference not null before call getString() method.
I have two editTextField, the same listener is attached to both of them. How would I figure out in the listener which one is clicked?
inputStart = (EditText)findViewById(R.id.editText3);
inputStart.setOnClickListener(TimePickerButtonOnClickListener);
inputEnd = (EditText)findViewById(R.id.editText4);
inputEnd.setOnClickListener(TimePickerButtonOnClickListener);
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener() {
public void onClick(View v) { }
};
I have checked, one possible solution is in this question,
but I am looking for any property like EditText gotEditField = (EditText)v.targetEditField;
Does any such property exist?
Use switch case, in onClick() with parameter View v using v.getId()
private Button.OnClickListener TimePickerButtonOnClickListener = new Button.OnClickListener()
{
public void onClick(View v) {
switch(v.getId){
case R.id.editText3:
break;
.
.
.
}
};
Also you can get the object of EditText using Type Casting the View v parameter..
Like EditText editText = (EditText)v;
View v from the parameters in your onCLick method is the view that was clicked.
EditText gotEditField = (EditText) v;
public void onClick(View v)
{
int id= v.getId();
switch (id)
{
case R.id.editText3:
break;
case R.id.editText4:
break;
}
}