Passing String values from custom dialog to another activity - android

A custom dialog is being used to take in user input, and then these values are being passed to another activity using getter methods.
But when I pass the values to a method that outputs the string values to a CSV file, shipName, analystName etc the values appear as empty in the file like this, " " although I have entered the values in the dialog.
I debugged the problem by watching the String values in the debug menu's expression window, shipName and analystName but the values never update in the expression window.
I gather from this that the method i which the input is being passed over is not correct.
Does anyone know why the values being output are empty?
This the dialog class being used:
package ie.gmi.computing;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.renderscript.Sampler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MyMessageDialog {
private Context context;
private EditText shipText, scientistNameText , scientistEmailText , volumeText , colourText ;
private String shipString, scientistNameString , scientistEmailString , volumeString , colourString ;
public AlertDialog displayMessage(Context context, String title, String message){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
LayoutInflater inflater = LayoutInflater.from(context);
final View v = inflater.inflate(R.layout.custom_view, null);
builder.setView(v);
shipText = (EditText)v.findViewById(R.id.shipNameEditText);
scientistNameText = (EditText)v.findViewById(R.id.scientistEditText);
scientistEmailText = (EditText)v.findViewById(R.id.emailEditText);
volumeText = (EditText)v.findViewById(R.id.volumeEditText);
colourText = (EditText)v.findViewById(R.id.colourEditText);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog= builder.create();
dialog.show();
Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
tb.setOnClickListener(new CustomListener(dialog));
return dialog;
}
//getter/setters to allow access to string values
//in SearchResult class
public EditText getShipText() {
return shipText;
}
public void setShipText(EditText shipText) {
this.shipText = shipText;
}
public EditText getScientistNameText() {
return scientistNameText;
}
public void setScientistNameText(EditText scientistNameText) {
this.scientistNameText = scientistNameText;
}
public EditText getScientistEmailText() {
return scientistEmailText;
}
public void setScientistEmailText(EditText scientistEmailText) {
this.scientistEmailText = scientistEmailText;
}
public String getShipString() {
return shipString;
}
public void setShipString(String shipString) {
this.shipString = shipString;
}
public String getScientistNameString() {
return scientistNameString;
}
public void setScientistNameString(String scientistNameString) {
this.scientistNameString = scientistNameString;
}
public String getScientistEmailString() {
return scientistEmailString;
}
public void setScientistEmailString(String scientistEmailString) {
this.scientistEmailString = scientistEmailString;
}
public String getVolumeString() {
return volumeString;
}
public void setVolumeString(String volumeString) {
this.volumeString = volumeString;
}
public String getColourString() {
return colourString;
}
public void setColourString(String colourString) {
this.colourString = colourString;
}
public EditText getVolumeText() {
return volumeText;
}
public void setVolumeText(EditText volumeText) {
this.volumeText = volumeText;
}
public EditText getColourText() {
return colourText;
}
public void setColourText(EditText colourText) {
this.colourText = colourText;
}
#SuppressLint("NewApi")
class CustomListener implements View.OnClickListener {
private final Dialog dialog;
public CustomListener(Dialog dialog) {
this.dialog = dialog;
}
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
if(shipText.getText().toString().isEmpty() && !shipText.getText().toString().equals(null)){
shipText.setError("The Field is required");
}else if(scientistNameText.getText().toString().isEmpty() && !scientistNameText.getText().toString().equals(null)){
scientistNameText.setError("The Field is required");
}else if(scientistEmailText.getText().toString().isEmpty() && !scientistEmailText.getText().toString().equals(null)){
scientistEmailText.setError("The Field is required");
}else if(volumeText.getText().toString().isEmpty() && !volumeText.getText().toString().equals(null)){
volumeText.setError("The Field is required");
}else if(colourText.getText().toString().isEmpty() && !colourText.getText().toString().equals(null)){
colourText.setError("The Field is required");
}else{
shipText.setError(null);
scientistNameText.setError(null);
scientistEmailText.setError(null);
volumeText.setError(null);
colourText.setError(null);
shipString = shipText.getText().toString();
scientistNameString = scientistNameText.getText().toString();
scientistEmailString = scientistEmailText.getText().toString();
volumeString = volumeText.getText().toString();
colourString = colourText.getText().toString();
Toast.makeText(dialog.getContext(), "The Values you get from : " +
"\n Ship name value: " + shipText.getText().toString() +
"\n Scientist name value: " + scientistNameText.getText().toString() +
"\n email value: " + scientistEmailText.getText().toString() +
"\n sample volume value: " + volumeText.getText().toString() +
"\n sample colour value: " + colourText.getText().toString() , Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
}
}
And this is how I'm retrieving the values in my SearchResult class, when i select the settings button:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
MyMessageDialog dialog =new MyMessageDialog();
dialog.displayMessage(SearchResult.this, "Sample Info", "Required");
// store / use the values here
shipName = dialog.getShipString();
analystName = dialog.getScientistNameString();
analystEmail = dialog.getScientistEmailString();
sampleVolume = dialog.getVolumeString();
sampleColour = dialog.getColourString();
longitudeValue = String.valueOf(lng);
latitudeValue = String.valueOf(lat);
sampleMaterial = message;
return true;
default:
return super.onOptionsItemSelected(item);
}
}

Your dialog showing is asynchronous; that is, code execution in onOptionsItemSelected() does not pause after the call to dialog.displayMessage(), so the getters are returning the initial values for those fields, which is null in all cases. You should create an interface that the Activity implements to receive a callback after those fields are set in the onClick() method of your CustomListener, and update the Activity's variables then.
In the dialog class, we create an interface. For example:
public class MyMessageDialog {
public interface DialogCallback {
public void onValuesSet();
}
...
}
Be sure to save a reference to the Context:
public AlertDialog displayMessage(Context context, String title, String message){
this.context = context;
...
}
And at the end of the onClick() method, after the fields' values are set:
((DialogCallback) context).onValuesSet();
The Activity needs to implement the interface we created, the dialog should be a class member, and the fields will be set in the interface's callback method:
public class SearchResult extends Activity
implements MyMessageDialog.DialogCallback {
...
MyMessageDialog dialog;
#Override
public void onValuesSet()
{
shipName = dialog.getShipString();
analystName = dialog.getScientistNameString();
...
}
...
}

Does anyone know why the values being output are empty?
Because all statements from getting data from dialog class is executing after dialog.displayMessage on menu option section.
How to get data from MyMessageDialog on Ok button click ?
1. Instead of getting EditText from MyMessageDialog change all getter/setter method return type to String..
2. Create a event Listener using interface for getting event of Alert close on Ok button click in Activity. you can create event listener as:
Android Custom Event Listener
3. call all setter method on Ok button click of Alert. after calling all setter method call event listener method to execute event in Activity after Alert finish.:
#Override
public void onClick(DialogInterface dialog, int which) {
setShipText(shipText.getText().toString());
setScientistNameText(shipText.getText().toString());
....
//..close alert and call event listener method
}

Your getters don't seem to actually call the getText() method on each EditText view. Modify them do that they do (e.g., scientistNameText.getText()).
PS And, yes, Mike M may indeed write about asyncronicity contributing to the problem. Another approach around that would be to add something like this to the code for each EditText view:
myEditTextView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
//Set the variable here that you call in your getter. Use `getText()` to get the string (e.g., myGetterVariable = myEditTextView.getText().
}
}

Related

Get and Put extra, DialogFragment

I'm writing an app where the user describes a problem and then receives advice. The user presses a button which shows a dialog with an EditText. Once the user presses OK, I want to get their input, but I'm having trouble with the extras. I've read similar questions, but I can't seem to find the problem. On a summary screen where I display the information, no text ever appears. Any help is appreciated!
I think the problem is when I call getText() on the EditText. Using log.d the mText is just an empty String.
Here is my code:
The fragment AdviceFragment from which the dialog is called:
private static final String DIALOG_TEXT = "text";
private static final int REQUEST_TEXT = 0;
private Advice mAdvice;
private boolean hasText;
...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState)
{
...
mTextButton = (Button) v.findViewById(R.id.textButton);
mTextButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
FragmentManager fm = getActivity().getSupportFragmentManager();
InputTextFragment dialog = new InputTextFragment();
dialog.setTargetFragment(AdviceFragment.this, REQUEST_TEXT);
dialog.show(fm, DIALOG_TEXT);
}
});
}
...
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != Activity.RESULT_OK)
{
return;
}
if (resultCode == REQUEST_TEXT)
{
String text = data.getStringExtra(InputTextFragment.EXTRA_TEXT);
if (text.length() > 0)
{
mAdvice.setText(text);
hasText = true;
}
else
{
mAdvice.setText(null);
hasText = false;
}
}
InputTextFragment dialog:
public class InputTextFragment extends DialogFragment
{
public static final String EXTRA_TEXT = "text";
private String mText;
private void sendResult(int resultCode)
{
if (getTargetFragment() == null)
{
return;
}
Intent i = new Intent();
i.putExtra(EXTRA_TEXT, mText.toString());
getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_input_text, null);
final EditText editText = new EditText(getActivity());
return new AlertDialog.Builder(getActivity())
.setView(v)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
String input = editText.getText().toString();
if (input.length() > 0)
{
mText = input;
}
else
{
return;
}
sendResult(Activity.RESULT_OK);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
{
...
})
.create();
}
}
final EditText editText = new EditText(getActivity());
You problem is here, your EditText is not added to your Dialog's View Tree. I think you should do like this:
final EditText editText = (EditText)v.findViewById(your_edittext_id);
Thanks, I think this has fixed part of the problem. Using log.d in the
onClick() method of setPositiveButton()shows that it is successfully
assigning the value. However, I'm still not getting anything when I
call onActivityResult(). Do you have any idea what's going wrong?
Look here, another typo problem:
if (resultCode == REQUEST_TEXT)
{
It should be requestCode.
I think this should fix your problem, but you'd better follow bean_droid's suggest and use an interface instead of calling the onActivityResult() method. It's because that method may be called by other part of your code, which you don't want.
Here try this:
Public class AdviceFragment extends Fragment implements OkClickListener{
#Override
Public void onClick(String data){
//DO YOUR CODE HERE
}
}
InputTextFragment
Public Class InputTextFragment extends DialogFragment{
public interface OkClickListener{
public void onClick(String data);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_input_text, null);
final EditText editText = new EditText(getActivity());
return new AlertDialog.Builder(getActivity())
.setView(v)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
String input = editText.getText().toString();
if (input.length() > 0)
{
mText = input;
}
else
{
return;
}
((OkClicklistener)getTargetFragment()).onClick(data);
}
})
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
{
...
})
.create();
}
}
}
replace sendResult(Activity.RESULT_OK); to getActivity().setResult(resultCode);

Android: Call function of Activity out of DialogFragment

I have a problem to call a function of my Activity out of DialogFragment. There are public functions in my MainActivity which I need to call for some calculations that are done in the DialogFragment. Everytime I try to call a function with getActivity(). there occurs the problem "Cannot resolve method".
Here is how I call the DialogFragment in the MainActivity:
FragmentManager fm = getSupportFragmentManager();
DialogWeekly dialogWeekly = new DialogWeekly();
dialogWeekly.show(getFragmentManager(), "fragment_dialogWeekly");
And this is how the DialogFragment looks like. I have added two comment lines where the mentioned problem occurs:
public class DialogReminder extends DialogFragment implements AdapterView.OnItemSelectedListener {
//--- Static Variables -----------------------------------------------------------------------//
private static final String MY_PREFS = "my_preferences";
private static Activity activity;
private static TimePicker timePicker;
private static View dialogReminderView;
//--- Integer Variables ----------------------------------------------------------------------//
private Integer weekday;
//--- String Variables -----------------------------------------------------------------------//
private String weekdayString;
//--- Other Variables ------------------------------------------------------------------------//
private SharedPreferences sharedPreferences;
/**
* Empty constructor required for DialogFragment
*/
public DialogReminder() { }
/**
* Called when a fragment is first attached to its activity.
* onCreate(Bundle) will be called after this
* #param activity Activity that is attached to this fragment
*/
public void onAttach(Activity activity) {
super.onAttach(activity);
}
//--- Override Functions ---------------------------------------------------------------------//
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_weekly, container);
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = getActivity().getSharedPreferences(MY_PREFS, Context.MODE_PRIVATE);
return createAlertDialog();
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Integer selectedItem = parent.getSelectedItemPosition();
weekdayString = parent.getItemAtPosition(pos).toString();
// Here is the problem: savePreferences -> cannot resolve method
getActivity().savePreferences("spinnerSelectionWeekday", String.valueOf(selectedItem));
weekday = selectedItem + 2;
if (weekday == 8) {
weekday = 1;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
//--- General Activity Functions -------------------------------------------------------------//
/**
*
* #return
*/
private AlertDialog createAlertDialog() {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(getResources().getString(R.string.optionReminder));
alert.setView(dialogReminderView);
alert.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alert.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
setReminder();
dialog.cancel();
}
});
setElementsGUI();
return alert.create();
}
/**
*
*/
private void setElementsGUI() {
Spinner spinner = (Spinner) dialogReminderView.findViewById(R.id.reminderWeekdaySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.reminderSpinnerArray, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(adapter);
spinner.setSelection(Integer.parseInt(sharedPreferences.getString("spinnerSelectionWeekday", "0")));
}
//--- Button Functions -----------------------------------------------------------------------//
/**
*
*/
private void setReminder() {
// Here is the problem: all functions with getActivity() -> cannot resolve method
getActivity().checkReminder();
getActivity().setWeekdayReminder(weekday);
getActivity(("hour", String.valueOf(timePicker.getCurrentHour()));
getActivity().savePreferences("minute", String.valueOf(timePicker.getCurrentMinute()));
getActivity().checkReminder();
String hour = String.valueOf(getActivity().getHour());
if (hour.length() < 2) {
hour = "0" + hour;
}
String minute = String.valueOf(getActivity().getMinute());
if (minute.length() < 2) {
minute = "0" + minute;
}
String time = hour + ":" + minute;
String message = getResources().getString(R.string.reminderToast, weekdayString, time);
Toast toast = Toast.makeText(getActivity().getApplicationContext(), message, Toast.LENGTH_LONG);
toast.show();
}
}
While getActivity() returns a MainActivity at runtime, the compiler has to assume that it's just an Activity object and that those methods don't exist (since an Activity has none of these methods). Hence the compiler error.
What you need to do is cast the Activity to a MainActivity object like so:
((MainActivity)getActivity()).savePreferences(...
In kotlin we can cast the Fragment to a MainActivity object like this
(activity as MainActivity).yourMethodeName()

How can I save values of editText that are dynamically created in a dialog Window within an xml file? - Android

I have created 2 classes and one Interface. One Interface that handles dialog clicks,a MainActivity class that hold a button and a textView. The MainActiviy class instantiates my second class(FireMissilesFragment) which contains an AlertDialog as a fragment. In the FireMisslesFragment I have dynamically created EditText. The problem with my app is that when I call (onsavenstancestate) in my mainActivity class in which FireMissilesFragment is instantiated in, i try to save my editText values so that when the popup closes and I restart it, the values of editText will maintain it's values once the popup opens again.
I have tried (onSaveInstanceState) method and the values hold;however, it is yet possible for me to recreate what was destroyed once the dialog is initiated again.Can Someone please shed some light on this matter.
Here is my code:
//===============================Interface=====================================//
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog,EditText[] editText);
public void onDialogNegativeClick(DialogFragment dialog);
}
//==========================MainActivity Class=============================//
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements ` NoticeDialogListener{
private EditText[] _edText;
private TextView _tv;
private Multiplication multi;
private Double[] s;
private String s1;
public static final String _SCORE1 = "score1";
public static final String _SCORE2 = "score2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_tv = (TextView)findViewById(R.id.textView1);
Button dAction = (Button)findViewById(R.id.button1);
s = new Double[2];
dAction.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {//open();
confirmFireMissiles();
}
});//ssetOnclickLi...
///success thank god.
//===================================================//
}
public void confirmFireMissiles() {
DialogFragment newFragment = new FireMissilesDialogFragment();
newFragment.show(getSupportFragmentManager(),"ff");
}//confirmFireMisslesClosing brace
// The below is what happens when the Button "multiply" in
Dialog window pops up.
#Override
public void onDialogPositiveClick(DialogFragment dialog,EditText[]
editText) {
_edText = editText; // is this association
multi = new Multiplication();
try{
// gets the text and stores to string array.
s[0]=Double.parseDouble(_edText[0].getText().toString());
s[1]=Double.parseDouble(_edText[0].getText().toString());
Log.d("hello", String.valueOf(s[0]));
}catch(NumberFormatException e){
_tv.setText("please Insert an Number and calculate again"); //
Log.d("Error", "place in numbers please");
}
s1 = String.valueOf(multi.multiply(s[0],s[1]));
//set Textview to s1.
_tv.setText(s1);
}
#Override
public void onDialogNegativeClick(DialogFragment dialog) {}
}
//================================================================================//
public class FireMissilesDialogFragment extends DialogFragment {
private AlertDialog.Builder builder;
private EditText[] _edText; // enable when ready
private NoticeDialogListener _mListener;
public static final String _SCORE1 = "score1";
public static final String _SCORE2 = "score2";
private Double[] s;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
builder = new AlertDialog.Builder(getActivity());
//shows added content to dialog.
// d = new String[2];
s = new Double[2];
if(savedInstanceState !=null){
s[0] = Double.parseDouble(savedInstanceState.getString(_SCORE1));
s[1] = Double.parseDouble(savedInstanceState.getString(_SCORE2));
Log.d("Hey",String.valueOf(s[0]));
_edText[0].setText(String.valueOf(s[0]));
showIt();
}else{
showIt();
}
//sets the characterisitcs of the dialogue.
builder.setTitle("We are all stars of the show.");
builder.setMessage( "we are strong")
.setPositiveButton("Multiply", new
DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
// enable when ready
_mListener.onDialogPositiveClick(FireMissilesDialogFragment.this,_edText);
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void showIt(){
// This piece of code creates a Linear layout that is suppose to show in a dialogue popup.
LayoutParams param = new
LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1.0f);
LinearLayout layout= new LinearLayout(getActivity());
layout.setLayoutParams(param);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.BLACK);
// Dynamically place EditText efficiently Inside Linear Layout.
_edText = new EditText[4];
for (int i = 0;i< _edText.length;i++) {
_edText[i] = new EditText(getActivity());
_edText[i].setInputType(InputType.TYPE_CLASS_NUMBER |
InputType.TYPE_NUMBER_FLAG_DECIMAL);
_edText[i].setTextSize(20)
try{
s[i] =Double.parseDouble(
_edText[i].getText().toString());
}catch(NumberFormatException e){
// Log.d("hello", "wrong input");
}
layout.addView(_edText[i]);
}
builder.setView(layout);
}
//============================================== Look over this code======////////////
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
_mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putDouble(_SCORE1,s[0]);
savedInstanceState.putDouble(_SCORE2,s[1]);
super.onSaveInstanceState(savedInstanceState);
}
}
You could probably use SharedPreferences and store the information there, and then set the text of the text edit to the result of the shared preferences? or did I get the whole idea wrong?
here is an example of a simple save function:
SharedPrefrences scores = getSharedPreferences("key_name_here", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = scores.edit();
editor.putInt("key1", key1Var);
editor.putInt("key2", key2Var);
editor.commit();
and to retrive:
Var = getPrefrences(Context.MODE_PRIVATE).getInt("key1",default value);
That should probably do the trick

Android Call Intent Null pointer exception

Im at my wits end here. I have a Class which Implements the OnClickListener cous i need the same action on Buttons accros my Application. This used to work just fine. But since I added some functionality by getting some needed data from the app preferences. startActivity throws a null pointer exception.Here is the class:
//Imports
public class CallClickListener extends Activity implements View.OnClickListener {
protected AppPreferences appPrefs;
String contactPersonName;
String contactPersonTelephone;
String name;
public CallClickListener(Context context){
Log.d("TRACE", "init CallClick");
appPrefs = new AppPreferences(context);
try {
JSONObject object = appPrefs.getConsultantObject();
contactPersonName = object.getString("contactPersonName");
contactPersonTelephone = object.getString("contactPersonTelephone");
name = object.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onClick(View view) {
final View v = view;
AlertDialog.Builder alert = new AlertDialog.Builder(view.getContext());
alert.setTitle("Anrufen");
alert.setMessage("Kontakt für " + name + ", " + contactPersonName + " anrufen");
alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+contactPersonTelephone));
startActivity(callIntent);// this line throws the exception
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
}
The Strings are all there from appPrefs, i also tried with hardcoding a phonenumber just incase. the Alert works fine, but as soon as i hit the positive Button, the app crashes
I add the Listener like this:
bCall.setOnClickListener(new CallClickListener(getApplicationContext()));
I added the necessary Call permissions.
I'm fairly new to Android dev, what am I missing?
Do this.... make the context object that you passed in the constructor into a field variable. and change startActivity to context.startActivity. It will work then.
EDIT: Highlighting the full solution.
bCall.setOnClickListener(new CallClickListener(getApplicationContext()));
should be changed to YourActivityClass.this instead of getApplicationContext.
Start Activity in the same task does not work with a context object that is not an Activity. So you need to either change the context to Activity or you start the activity in a new task. Also without calling startActivity on the context provided to your constructor you were getting the NPE because your CallClickListerner has no context.
Use activity context. Also check if you have initialized bCall. If you have not you will get NullPointerException.
bCall.setOnClickListener(ActivityName.this);
Also check this link to know when to use activity context and when to use application context
When to call activity context OR application context?
Edit:
Make sure you have added permission in manifest file
<uses-permission android:name="android.permission.CALL_PHONE" />
For reference use the below. My Class extends Activity
Button b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v1) {
// TODO Auto-generated method stub
final View v = v1;
AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());
alert.setTitle("Anrufen");
alert.setMessage("Kontakt für " );
alert.setPositiveButton("Anrufen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:8095992052"));
startActivity(callIntent);// this line throws the exception
}
});
alert.setNegativeButton("Abbrechen", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(v.getContext(), "Abbruch", Toast.LENGTH_SHORT).show();
}
});
alert.show();
}
});

Pass value outside of public void onClick

So im sure this is probably a fairly easy question but I am stumped because I am a beginner.
I am looking to pass a value from one class to another, and I have my helper function down and working just fine. If i create an integer outside of my onClick I can pass it no problem. If I create it inside the onClick though it doesn't seem to make it out.
package com.movi.easypar;
//import java.util.logging.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class EntryScreen extends Activity implements OnClickListener {
Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; <--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.entryscreen);
//******************//
//***DEFINE FONTS***//
//******************//
Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");
//*****************************************************//
//***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
//*****************************************************//
buttonSetHoles = (Button)findViewById(R.id.buttonSetHoles);
buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
textGameSetup = (TextView)findViewById(R.id.textGameSetup);
buttonSetHoles.setTypeface(merge);
buttonSetPlayers.setTypeface(merge);
buttonLetsGo.setTypeface(merge);
textGameSetup.setTypeface(merge);
buttonSetHoles.setText("Set Holes");
buttonLetsGo.setText("Lets Go");
buttonSetPlayers.setText("Set Players");
//******************************//
//***DEFINES BUTTON LISTENERS***//
//******************************//
buttonSetHoles.setOnClickListener(this);
buttonSetPlayers.setOnClickListener(this);
buttonLetsGo.setOnClickListener(this);
}
//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
#Override
public void onClick(View src) {
switch(src.getId()){
case R.id.buttonSetPlayers:
break;
case R.id.buttonSetHoles:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items = {"18", "9"};
builder.setTitle("Set Holes");
builder.setItems(items, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialogInterface, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
if (items[item].equals("9")){
EntryScreen.this.setHoles = 9; <---#### VALUE SET HERE ####
}
else if (items[item].equals("18")){
EntryScreen.this.setHoles = 18;
}
return;
}
});
builder.create().show();
return;
case R.id.buttonLetsGo:
//*********************************//
//***LAUNCHES ACTUAL APPLICATION***//
//*********************************//
TranslateAnimation slide = new TranslateAnimation(0, -500, 0,0 );
slide.setDuration(1000);
slide.setFillAfter(true);
buttonLetsGo.startAnimation(slide);
buttonSetPlayers.startAnimation(slide);
buttonSetHoles.startAnimation(slide);
Intent myIntent = new Intent(src.getContext(), EasyPar.class);
startActivityForResult(myIntent, 0);
break;
}
EntryScreen.this.finish();
}
public String getNames() {
return name1;
}
public void setNames(String playerName1) {
name1 = playerName1;
}
public int getHoles() {
return setHoles; <---- #### THIS DOES NOT SEE VALUE SET IN ONCLICK ####
}
}
This helper does not seem to be able to see the setHoles value that is created onClick.
Any suggestions? Thanks in advance!
It's a scope thing. A variable defined in a function has local scope, and will be destroyed when the function returns. You need a field to hold your value if you wish to retain it.
[EDIT]
Then allow me to elaborate. You can create a field by typing the following line outside a function, inside the class:
[Access][Type][Name];
ex:
class foo{
public int dice;
public void onClick(){
//now the dice's value is saved throught the lifecycle of the Activity
}
}
[EDIT]
I copied your code and ran it. (Modified just a little.)
public class Main extends Activity implements OnClickListener {
Button buttonSetHoles, buttonSetPlayers, buttonLetsGo;
TextView textGameSetup;
public int setHoles; //<--- declared here###############################
private String name1 = "Crista";
private String name2 = "Rob";
private String name3 = "Gato";
private String name4 = "Movi";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//******************//
//***DEFINE FONTS***//
//******************//
Typeface merge = Typeface.createFromAsset(getAssets(), "merge.otf");
//*****************************************************//
//***CREATES THE UI FOR EACH OF THESE IN JAVA MEMORY***//
//*****************************************************//
/*
buttonSetPlayers = (Button)findViewById(R.id.buttonSetPlayers);
buttonLetsGo = (Button)findViewById(R.id.buttonLetsGo);
textGameSetup = (TextView)findViewById(R.id.textGameSetup);
*/
buttonSetHoles = (Button) findViewById(R.id.buttonSetHoles);
/*
buttonSetHoles.setTypeface(merge);
buttonSetPlayers.setTypeface(merge);
buttonLetsGo.setTypeface(merge);
textGameSetup.setTypeface(merge);
buttonSetHoles.setText("Set Holes");
buttonLetsGo.setText("Lets Go");
buttonSetPlayers.setText("Set Players");
*/
//******************************//
//***DEFINES BUTTON LISTENERS***//
//******************************//.
buttonSetHoles.setOnClickListener(this);
/*
buttonSetPlayers.setOnClickListener(this);
buttonLetsGo.setOnClickListener(this);
*/
}
//*************************************************//
//***SETS ON CLICK FUNCTIONALITY FOR ALL BUTTONS***//
//*************************************************//
#Override
public void onClick(View src) {
switch (src.getId()) {
case R.id.buttonSetHoles:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
final CharSequence[] items = { "18", "9" };
builder.setTitle("Set Holes");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
if (items[item].equals("9")) {
setHoles = 9;// <---#### VALUE SET HERE ####
Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
}
else if (items[item].equals("18")) {
setHoles = 18;
Toast.makeText(Main.this, "getHoles()=" + getHoles(), Toast.LENGTH_SHORT);
}
return;
}
});
builder.create().show();
return;
}
//finish();
}
public String getNames() {
return name1;
}
public void setNames(String playerName1) {
name1 = playerName1;
}
public int getHoles() {
return setHoles;
}
}
And it seems to work just fine.
If you declare the variable inside the method, an external method is surely not able to see it, it's not in the same scope, you can still declare it outside and then set a value from inside the onClick() method.
Declare it as public/private variable outside the methods.
are you sure your setHoles is even being set? to 9 or 18? try adding a println(setHoles) in your onclick to ensure that the value is being set properly. Also, you are declaring your setHoles variable outside of onCreate but within the same class as getHoles() and onClick() right?
when you compare Strings always use equal method.
like:
if (items[item].equals( "9")){
}
and i prefer to user Setters and Getters on variables:
setHoles(int value){}
and
int getHoles(){}

Categories

Resources