I'm trying to get some values from a dialog. I have an activity called Splash that shows a dialog with a form. It displays the form wel but i gives me an error when i try to get that values (nullpointer).
I have the folowing code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//datos objeto
nombre = (TextView) findViewById(R.id.tv_nombre);
padre = (TextView) findViewById(R.id.tv_nom_padre);
madre = (TextView) findViewById(R.id.tv_nom_madre);
rgSexo = (RadioGroup) findViewById(R.id.rg_sexo);
fecha_nac = (DatePicker) findViewById(R.id.dp_fecha_nac);
rb_m = (RadioButton) findViewById(R.id.rb_masc);
rb_f = (RadioButton) findViewById(R.id.rb_fem);
perfil = new PerfilCRUD(this);
perfil.open();
//first time
if (perfil.primeraVez()){
//muestro el popup
Log.v(TAG, "prim vez");
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_perfil);
dialog.setTitle("Datos del usuario");
dialog.show();
Button botOk = (Button) dialog.findViewById(R.id.b_ok);
botOk.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Log.v(TAG, "boton ok");
Log.v(TAG, nombre.getText().toString());
}
});
It gives me the error when i try to get the nombre.getText().toString() value. It happens that with all variables/controls values. It forces to close the application.
Thanks!!
Its because nombre is in your original layout. Either call a TextView in the layout that you inflate in the dialog or create a class level variable String name = ""; then in
if (perfil.primeraVez()){
name = nombre.getText().toString();
then you can use name in your dialog
Related
problem: I set point breaks at the following code:
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
, both showed up as null when I ran the app in debug mode. workoutName contains a simple String that is passed to a new activity, whereas workoutDays constains an array of String.
the full code is provided below:
public class CreateWorkoutActivity extends Activity {
public final String TAG = this.getClass().getSimpleName();
protected String[] workoutDays = new String[7];
protected String workoutName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_workout);
Button mNextButton = (Button) findViewById(R.id.next_button1);
CheckBox satBox = (CheckBox) findViewById(R.id.sat_checkbox);
CheckBox sunBox = (CheckBox) findViewById(R.id.sun_checkbox);
CheckBox monBox = (CheckBox) findViewById(R.id.mon_checkbox);
CheckBox tuesBox = (CheckBox) findViewById(R.id.tues_checkbox);
CheckBox wedBox = (CheckBox) findViewById(R.id.wed_checkbox);
CheckBox thursBox = (CheckBox) findViewById(R.id.thurs_checkbox);
CheckBox friBox = (CheckBox) findViewById(R.id.fri_checkbox);
final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1);
workoutName = mWorkoutName.getText().toString();
Log.i(TAG, workoutName);
if (satBox.isChecked()) {
workoutDays[0] = new String(satBox.getText().toString());
}
if (sunBox.isChecked()) {
workoutDays[1] = new String(sunBox.getText().toString());
}
if (monBox.isChecked()) {
workoutDays[2] = new String(monBox.getText().toString());
}
if (tuesBox.isChecked()) {
workoutDays[3] = new String(tuesBox.getText().toString());
}
if (wedBox.isChecked()) {
workoutDays[4] = wedBox.getText().toString();
}
if (thursBox.isChecked()) {
workoutDays[5] = satBox.getText().toString();
}
if (friBox.isChecked()) {
workoutDays[6] = friBox.getText().toString();
Log.i(TAG, workoutDays[6]);
}
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, workoutDays.toString());
Intent intent = new Intent(CreateWorkoutActivity.this, WorkoutRoutinesActivity.class);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_NAME, workoutName);
intent.putExtra(WorkoutRoutinesActivity.EXTRA_WORKOUT_DAYS, workoutDays);
Log.i(TAG, workoutDays.toString());
startActivity(intent);
}
});
}
The problem is not in the intent, but in the way you obtain workoutName (this is the null value). You create the activity, set up final EditText mWorkoutName = (EditText) findViewById(R.id.workout_name1); and then immediately ask for the input value through workoutName = mWorkoutName.getText().toString();, but at this time the user still hasn't entered anything. You should put that second line in the listener below (so its activated only after the user presses mNextButton. It's a good idea to put some check after it and send a message to user that they need to fill in that field (if it is indeed necessary).
Looks like the values for workoutName and workoutDays are not filled in initially when the view is created. You should move retrieving the value from the text fields to your onClickListener function.
you checking the CheckBox and EditText in onCreate, absolutely the EditText will be empty and all CheckBox it not checked
I am developing a android App which is totally based on request and response from servlet.I have populate some data in customize Alert-dialog Where i using two thing one is cross button that will delete item from list in alert-dialog and update view of alert dialog , second thing is close button that will suppose to dismiss this alert-dialog. I am showing full coding of my alert-dialog. I calling alert dialog on button click by all these method.
intiliazeOrderListDialog();
showOrderListDialog();
My decleration is as follow
public AlertDialog detailsDialog, orderDialog;
AlertDialog.Builder builder;
Now i am going to post my intiliazeOrderListDialog() block.
public void intiliazeOrderListDialog() {
builder = new AlertDialog.Builder(MainScreen.this);
mContext = getApplicationContext();
inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
orderDialogLayout = inflater.inflate(R.layout.my_order_list,(ViewGroup)findViewById(R.id.order_list_root));
orderList = (ListView) orderDialogLayout.findViewById(R.id.order_list);
ibOrderDelete = (ImageButton)orderDialogLayout.findViewById(R.id.deleteOrder);
tvPrice = (TextView) orderDialogLayout.findViewById(R.id.order_list_total);
tvTaxes = (TextView) orderDialogLayout.findViewById(R.id.order_list_taxes);
tvTotal = (TextView) orderDialogLayout.findViewById(R.id.order_list_grand_total);
Button bclose = (Button) orderDialogLayout.findViewById(R.id.close);
Button bPlaceOrder = (Button) orderDialogLayout.findViewById(R.id.my_order_placeorder);
bclose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
orderDialog.dismiss();
System.out.println(" click on closowse");
}
});
bPlaceOrder.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
System.out.println("Place order click");
palceMyOrdertoServer();
new SendOrderFromTable().execute();
System.out.println("place order to server is called");
String msg = "Your Order is Successfully placed to Kitcken";
Message msgObject = new Message();
msgObject.what = 1;
msgObject.obj = msg;
addMenuItemHandler.sendMessage(msgObject);
orderDialog.dismiss();
}
});
}
and at last i am going to post showOrderListDialog(); block
public void showOrderListDialog() {
builder.setView(orderDialogLayout);
orderDialog = builder.create();
orderDialog.show();
}
I know i have posted too many codes but its conveniences for those who want to help me . I have a very simple problem why my
orderDialog.dismiss();
is not working for me.? Thanks in advance to all .
Finally i solve my own issue . "Self help is best help ;;".
It was issue of calling method in setOnClickListener.
I just call it,
android:clickable="true"
android:onClick="clickHandler"
if (v.getId() == R.id.myOrder) {
System.out.println("Click my Order");
System.out.println("OrderListAdapter.totalCount ="
+ OrderListAdapter.totalCount);
// select COUNT(*) from CDs;
int jcount = 0;
jcount = countjournals();
System.out.println("jcount = " + jcount);
// Count implementation at my Order
if (jcount < 1) {
alertShow();
} else {
intiliazeOrderListDialog();
showOrderListDialog();
}
// startActivity(new Intent(RestaurantHome.this,
// MyOrderList.class));
}
I have a problem. I do not know how to call a different class. the code is for a button (when you click the button a message appears in random) so i thought it would be better if i placed it in a different class as i have also used arrays.Now i do not know how to call the class.
This is my code.I want to call "Firstin" inside SecondActivity.
public class SecondActivity extends Activity {
private Firstin mFirst = new Firstin ();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
//finds textview
final Text Ftext = (Text) findViewById(R.id.textView2);
#SuppressWarnings("unused")
final Text Stext = (Text) findViewById(R.id.textView3);
//finds button view
Button btnView = (Button) findViewById(R.id.button1);
#SuppressWarnings("unused")
Button btnView2 = (Button) findViewById(R.id.button2);
btnView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String Answer = mFirst.firstAnswer();
Ftext.setTextContent(Answer);
}
});
this is the class I'm trying to call:
package com.example.insultgenerator;
import java.util.Random;
public class Firstin {
public String firstAnswer(){
String [] mResults={
"its cool",
"we cool",
"im cool",
"he cool",
"she cool"
};
// the button was clicked so replace the answer label with answer
String Answer = "" ;
// the two double is a 'empty string
Random RandomGen = new Random();// telling the program to construct a random generator
int RandomNum= RandomGen.nextInt(mResults.length);
Answer = mResults[RandomNum];
return(Answer);
}
}
You should cast to TextView
final TextViewFtext = (TextView) findViewById(R.id.textView2);
#SuppressWarnings("unused")
final TextViewStext = (TextView) findViewById(R.id.textView3);
then use TextView.setText(...) method:
String Answer = mFirst.firstAnswer();
Ftext.setText(new Firstin().firstAnswer());
}
});
then last call the method from the other class with new Firstin().firstAnswer() which creates a instance of the class and executes the method.
I'm trying to get a string value from a listview and put into another view. I've managed to do this successfully for my first listview but it won't seem to work for my third. ( I managed to get it to work but its stopped working now for reasons I cant understand) I am using the exact same method and the information i'm trying to retrieve is being retrieved as I have confirmed while running the problem is its just not getting changed.
Code
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.statseditor);
dbHelper = new PlayerStatsDatabase(this);
dbHelper.open();
Bundle extras = getIntent().getExtras();
if(extras !=null){
//String value = extras.getString("Name");
}
//Clean all data
// dbHelper.deleteAllStats();
//Add some data
//dbHelper.insertSomeCountries();
//Generate ListView from SQLite Database
//displayListView();
// list = getListView();
PlayerNameText = extras.getString("Name");
btnSave2 = (Button) findViewById(R.id.btnSaveStats);
btnClear = (Button) findViewById(R.id.btnClearStats);
txtGoalsScored = (EditText) findViewById(R.id.txtGoal);
txtMinutesPlayed = (EditText) findViewById(R.id.txtMinPlayed);
txtSubstituteIn = (EditText) findViewById(R.id.txtSubIn);
txtSubstituteOut = (EditText) findViewById(R.id.txtSubOut);
radioSubIn = (RadioButton) findViewById (R.id.rdoSubIn);
radioSubOut = (RadioButton) findViewById (R.id.rdoSubOut);
playerRating = (RatingBar) findViewById (R.id.ratingBar1);
//playerTitle = (TextView) findViewById (R.id.textTitle);
playerName = (TextView) findViewById (R.id.textView1);
playerName.setText(PlayerNameText);
//playerTitle.setText (PlayerNameText);
checkBox = (CheckBox) findViewById (R.id.checkBox1);
if (checkBox.isChecked()){
checkBox.append("Booked");
}
final CharSequence checkText = checkBox.getText();
//final Context context =
btnSave2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
PlayerStatsDatabase db = new PlayerStatsDatabase(getApplicationContext());
db.open();
db.createStats(txtGoalsScored.getText().toString(), txtMinutesPlayed.getText().toString(),txtSubstituteIn.getText().toString(),txtSubstituteOut.getText().toString(), checkText.toString());
db.close();
dipsplayPlayerName();
displayListView();
//Intent intent = new Intent(context, ListItemsActivity.class);
// startActivity(intent);
}
});
}
In this first instance where I use this method playerName actually gets changed to PlayerNameText
But further down when I try to use it again it doesnt work
private void dipsplayPlayerName() {
setContentView (R.layout.statslist);
Bundle extras = getIntent().getExtras();
PlayerNameText = extras.getString("Name");
playerTitle = (TextView) findViewById (R.id.textTitle);
playerTitle.setText (PlayerNameText);
}
Any ideas? The is no LogCat as the app doesnt crash
Put the text data you received into a field and use it instead of getting it from the intent again.
Also, check that displayListView() doesn't change the textView.
Suppose my current activity is Main.java and I have already declared its layout through setContentView(R.layout.layout1) from its onCreate method. Now, is it in any way possible for me to access a different layout? For e.g., assuming there is another layout - layout2 which has TextView with id tv, then I won't be able to execute the following code from Main.java :
TextView text = (TextView) findViewById(R.id.tv);
text.setText("blah blah");
Is there any way that I can set tv's value from Main.java.
My actual code is the following
setContentView(R.layout.layout);
Button button = (Button) findViewById(button);
button(buttonListener);
Dialog dialog;
Inside the listener, I have the following code:
TextView dialogTitle = (TextView) findViewById(R.id.dialog_title);
dialogTitle.setText("Email");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View customView = getLayoutInflater().inflate(R.layout.dialog, null);
builder.setView(customView);
dialog = builder.create();
dialog.show();
The problem that I am facing is that dialog_title is in dialog.xml and not in layout.xml
You can always inflate any XML layout you want at any time:
View layout2 = LayoutInflater.from(this).inflate(R.layout.layout2, null);
You can use Bundles
In Activity 1
String your_string = "Hello, World!";
Bundle bundle = new Bundle();
bundle.putString("The key for this string", your_string );
Intent ActivityToLaunch= new Intent(this, ActivityB.class);
ActivityToLaunch.putExtras(bundle);
this.startActivity(ActivityToLaunch);
In Activity 2
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2); //Setup some layout, set to your own
String content = getIntent().getExtras().getString("The key for this string");
TextView text = (TextView) findViewById(R.id.tv);
text.setText(content);
}
The thread starter said that he wanted to raise a custom dialog, so here goes the edit
This is my class which will generate a custom Dialog:
public class ErrorDialog {
TextView msgTextView;
Button toSettings;
final Context c;
Dialog errorDialog;
/**
* #param c The Context
* #param title Title of the Dialog
* #param msg Message og the Dialog
* #param textOnButton The text on the button
*/
public ErrorDialog(final Context c, String title, String msg, String textOnButton) {
this.c = c;
errorDialog = new Dialog(c);
errorDialog.setContentView(R.layout.error_dialog);
errorDialog.setTitle(title);
msgTextView = (TextView) errorDialog.findViewById(R.id.errorMSG);
msgTextView.setText(msg);
toSettings = (Button) errorDialog.findViewById(R.id.toSettings);
toSettings.setText(text);
toSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//doing operations when the user clicks my button in the dialog.
}
});
errorDialog.show();
errorDialog.setCancelable(true);
}
}
Use this class this way:
new ErrorDialog(getApplicationContext(), "My Title", "My Message to the user", "Text on the button");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
View customView = getLayoutInflater().inflate(R.layout.dialog, null);
builder.setView(customView);
TextView dialogTitle = (TextView) customView.findViewById(R.id.dialog_title);
dialogTitle.setText("Email");