This code is for call an array string from MainActivity.java class.
TextView infoEnviada;
infoEnviada = (TextView) findViewById(R.id.reslt);
String[] array = getIntent().getStringArrayExtra("resultados");
infoEnviada.setText(array);
}
The problem is in line infoEnviada.setText(array); because it says: Cannot resolve method setText(java.lang.String[]) I need help, because i want to call that string, and print it in a ListView or TextView, if it's possible.
Error log:
03-06 16:54:45.814 6978-6978/com.example.pablo.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.pablo.myapplication.encuesta$1.onClick(encuesta.java:59)
at android.view.View.performClick(View.java:4162)
at android.view.View$PerformClick.run(View.java:17088)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
On click event code:
Button sig = (Button) findViewById(R.id.env); // cargo el boton
sig.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = getApplicationContext();
EditText preg5 = (EditText) findViewById(R.id.editText2);
EditText preg4 = (EditText) findViewById(R.id.editText);
EditText nom = (EditText) findViewById(R.id.editText3);
Spinner cur = (Spinner) findViewById(R.id.spinnerp);
RadioGroup gruporadio = (RadioGroup) findViewById(R.id.Grupo1);
RadioGroup gruporadio2 = (RadioGroup) findViewById(R.id.Grupo2);
RadioGroup gruporadio3 = (RadioGroup) findViewById(R.id.Grupo3);
String strNombre = nom.getText().toString();
String curso = cur.getSelectedItem().toString();
String strPregCinco = preg5.getText().toString();
String strPregCuatro = preg4.getText().toString();
if(gruporadio.getCheckedRadioButtonId() == -1 || gruporadio2.getCheckedRadioButtonId() == -1 || gruporadio3.getCheckedRadioButtonId() == -1 )
{
Toast.makeText(context,"¡No marcaste ninguna respuesta!",Toast.LENGTH_LONG).show();
}
else if (strNombre.matches("") || strPregCinco.matches("") || strPregCuatro.matches(""))
{
Toast.makeText(context,"¡Dejaste campos vacíos!",Toast.LENGTH_LONG).show();
}
else
{
ListView listaresultados = (ListView) findViewById(R.id.reslt);
ArrayList respuestas = new ArrayList();
ArrayAdapter adaptador2 = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, respuestas);
listaresultados.setAdapter(adaptador2);
respuestas.add(strNombre);
respuestas.add(curso);
respuestas.add(strPregCuatro);
respuestas.add(strPregCinco);
Intent intent = new Intent (encuesta.this, res8.class);
intent.putExtra("resultados", respuestas);
startActivity(intent);
Intent pas = new Intent(encuesta.this, MainActivity.class);
Toast.makeText(context,"¡Encuesta enviada!",Toast.LENGTH_LONG).show();
startActivity(pas);
}
}
});
The error seems to be at line 59. But actually gruporadio, gruporadio2, and gruporadio3 exists.
RadioGroup gruporadio = (RadioGroup) findViewById(R.id.Grupo1);
RadioGroup gruporadio2 = (RadioGroup) findViewById(R.id.Grupo2);
RadioGroup gruporadio3 = (RadioGroup) findViewById(R.id.Grupo3);
<RadioGroup
android:id="#+id/Grupo1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1/2 hora a 1 hora" />
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1 hora o 2 horas" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 horas o más" />
</RadioGroup>
<RadioGroup
android:id="#+id/Grupo2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1/2 hora a 1 hora" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1 hora o 2 horas" />
<RadioButton
android:id="#+id/radioButton5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2 horas o más" />
</RadioGroup>
<RadioGroup
android:id="#+id/Grupo3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RadioButton
android:id="#+id/radioButton6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Norte de Bogotá (calle 72 en adelante)" />
<RadioButton
android:id="#+id/radioButton7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Suba (noroccidente)" />
<RadioButton
android:id="#+id/radioButton8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Centro - Chapinero - Teusaquillo" />
<RadioButton
android:id="#+id/radioButton9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Alrededores de Bogotá" />
<RadioButton
android:id="#+id/radioButton10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sur de Bogotá" />
<RadioButton
android:id="#+id/radioButton11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Occidente (Puente Aranada - Fontibón - Engativá)" />
</RadioGroup>
The problem is in line infoEnviada.setText(array); because it says:
Cannot resolve method setText(java.lang.String[]) I need help, because
i want to call that string, and print it in a ListView or TextView, if
it's possible.
the error occurs because you're trying to pass an array to the setText(...) method which is not valid.
try this:
infoEnviada.setText(Arrays.toString(array))
the problem in your code is that settext method dosnt take arrays as parameter... pass the array as String instead
infoEnviada.setText(Arrays.toString(array));
Related
I'm developing a simple app, the user will have to fill in their name, email, rate the app, and describe their experience(optional). If their is an empty field(name and email), an error message will pop up. I'm having trouble with Toast.LENGTH_LONG() at the end of the code. It keep saying 'method call expected'.
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
//Create an array adapter
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.rate_array, android.R.layout.simple_spinner_dropdown_item);
//Specify the layout for the drop down menu
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Apply the adapter to the spinner
spinner.setAdapter(adapter);
final EditText name = (EditText) findViewById(R.id.Name);
final EditText e_mail = (EditText) findViewById(R.id.email);
final Button submit = (Button) findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(name.getText().length() == 0){
name.setError("Please fill in your name");
}
else{
if(e_mail.getText().length() == 0){
e_mail.setError("Please fill in your email");
}
else{
Toast.makeText(this, "Validation successful",
Toast.LENGTH_LONG()).show();
}
}
}
});
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.infinite_space.myapplication.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView"
android:layout_width="292dp"
android:layout_height="37dp"
android:text="Enter Feedback details to b sent to the developers"
android:visibility="visible"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"
android:layout_weight="0.18" />
<EditText
android:id="#+id/Name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Your Name"
android:inputType="textPersonName"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="67dp" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown"
tools:layout_editor_absoluteY="176dp"
tools:layout_editor_absoluteX="16dp"
android:prompt="#string/rate_level"
android:entries="#array/rate_array" />
<EditText
android:id="#+id/email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Your email"
android:inputType="textEmailAddress"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="122dp" />
<EditText
android:id="#+id/details"
android:layout_width="fill_parent"
android:layout_height="103dp"
android:ems="10"
android:hint="Details..."
android:inputType="text"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="227dp" />
<CheckBox
android:id="#+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Would you like an email respond ?"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="343dp" />
<Button
android:id="#+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Send Feedback"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="385dp" />
</LinearLayout>
Toast.LENGTH_LONG is a (constant) field and not a method. Change LENGTH_LONG() to LENGTH_LONG.
Try this :
Toast.makeText(this, "Validation successful",Toast.LENGTH_LONG).show();
Should be either LENGTH_LONG for a long period of time or LENGTH_SHORT for a short period of time. Get rid of the () after LENGTH_LONG.
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
Basically, I'm trying to make this eating healthy app based on their BMI. The calculator is done but now I'm stuck at this where the user clicks on one of these 3 buttons and that list in the Spinner item and the ImageView changes accordingly (if user clicks breakfast button, then the spinner list and image change to breakfast and so). Even the image is not working. The app runs but when I click the button it terminates and I don't know how to fix it. if it's possible, can you guys also let me know the way I'm doing my Spinner will work or not?
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button Toch;
Button BTbtn;
Button LunchBtn;
Button DinnerBtn;
Button Submit;
EditText inweight;
EditText inheight;
EditText inage;
TextView BMR;
RadioButton rdM;
RadioButton rdF;
ImageView maimage;
Spinner spinner;
/**
Button Submit = (Butt
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button Submit = (Button)findViewById(R.id.Sumbit);
final Button Toch = (Button) findViewById(R.id.Toch);
Toch.setOnClickListener(this);
final Button BTbtn = (Button) findViewById(R.id.BTbtn);
BTbtn.setOnClickListener(this);
final Button LunchBtn = (Button) findViewById(R.id.LunchBtn);
LunchBtn.setOnClickListener(this);
final Button DinnerBtn = (Button) findViewById(R.id.DinnerBtn);
DinnerBtn.setOnClickListener(this);
//EditText
final EditText inweight = (EditText) findViewById(R.id.inweight);
final EditText inheight = (EditText) findViewById(R.id.inheight);
final EditText inage = (EditText) findViewById(R.id.inage);
//Text View
final TextView BMR = (TextView) findViewById(R.id.BMR);
// final TextView FDName=(TextView)findViewById(R.id.FDName);
//RadioButton
final RadioButton rdM = (RadioButton) findViewById(R.id.rdM);
final RadioButton rdF = (RadioButton) findViewById(R.id.rdF);
//ImageView
final ImageView maimage = (ImageView) findViewById(R.id.maimage);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.BTbtn: {
maimage.setImageResource(R.drawable.breakfast);
//ArrayAdapter bList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.breakfastList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.LunchBtn: {
maimage.setImageResource(R.drawable.dinner);
//ArrayAdapter lList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.lunchList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.DinnerBtn: {
maimage.setImageResource(R.drawable.lunch);
//ArrayAdapter dList = ArrayAdapter.createFromResource(getApplicationContext(), android.R.array.dinnerList, android.R.layout.simple_dropdown_item_1line);
}
case R.id.Sumbit: {
double weight = Double.parseDouble(inweight.getText().toString());
double height = Double.parseDouble(inheight.getText().toString());
double age = Double.parseDouble(inage.getText().toString());
double gender;
if (rdM.isChecked()) {
gender = 66;
double ans = gender + (13.7 * weight) + (5 * height) - (6.8 * age);
BMR.setText("" + (int) ans);
}
if (rdF.isChecked()) {
gender = 655;
double ans = gender + (9.6 * weight) + (1.8 * height) - (4.7 * age);
BMR.setText("Your Calories require : " + (int) ans);
}
}
case R.id.Toch: {
inweight.setHint(R.string.chweight);
inheight.setHint(R.string.chhight);
inage.setHint(R.string.chage);
}
}
}
}
R.layout.activity_main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.cmleu_000.myapplication.MainActivity">
<TextView
android:text="Food Name:"
android:textSize="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
<Button
android:id="#+id/BTbtn"
android:text="Breakfast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/LunchBtn"
android:layout_alignRight="#+id/radioGroup2"
android:layout_alignEnd="#+id/radioGroup2" />
<Button
android:id="#+id/LunchBtn"
android:text="Lunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/DinnerBtn"
android:layout_alignRight="#+id/BTbtn"
android:layout_alignEnd="#+id/BTbtn" />
<Button
android:id="#+id/DinnerBtn"
android:text="Dinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/maimage"
android:layout_alignLeft="#+id/LunchBtn"
android:layout_alignStart="#+id/LunchBtn" />
<ImageView
android:id="#+id/maimage"
android:src="#drawable/dinner"
android:layout_width="150dp"
android:layout_height="120dp"
android:layout_above="#+id/qweight"
android:layout_alignLeft="#+id/qhight"
android:layout_alignStart="#+id/qhight" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Sumbit"
android:layout_alignRight="#+id/Sumbit"
android:layout_alignEnd="#+id/Sumbit"
android:id="#+id/radioGroup2">
<RadioButton
android:id="#+id/rdF"
android:text="F"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rdM"
android:layout_toRightOf="#+id/rdM"
android:layout_toEndOf="#+id/rdM"
android:layout_gravity="left" />
<RadioButton
android:id="#+id/rdM"
android:text="M"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/radioGroup"
android:layout_toRightOf="#+id/qage"
android:layout_toEndOf="#+id/qage"
android:layout_gravity="left" />
</RadioGroup>
<TextView
android:text="Your weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/qweight"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:id="#+id/inweight"
android:hint="Plase input your weight (KG)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/qhight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Your height"
android:id="#+id/qhight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/inheight"
android:layout_alignRight="#+id/qweight"
android:layout_alignEnd="#+id/qweight" />
<EditText
android:id="#+id/inheight"
android:hint="Please input your height (CM)"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/qage"
android:layout_alignRight="#+id/inweight"
android:layout_alignEnd="#+id/inweight" />
<TextView
android:text="Your age"
android:id="#+id/qage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/inage"
android:layout_alignLeft="#+id/BMR"
android:layout_alignStart="#+id/BMR" />
<EditText
android:id="#+id/inage"
android:hint="Please input your age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Sumbit"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="#+id/Sumbit"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:text="Your BMR"
android:id="#+id/BMR"
android:textStyle="bold"
android:textSize="10pt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/inheight"
android:layout_alignStart="#+id/inheight" />
<Button
android:text="中文"
android:id="#+id/Toch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/BMR"
android:layout_toLeftOf="#+id/Sumbit"
android:layout_toStartOf="#+id/Sumbit" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_above="#+id/BTbtn"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Logcat:
08-24 22:43:52.039 3104-3104/com.example.cmleu_000.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cmleu_000.myapplication, PID: 3104
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.example.cmleu_000.myapplication.MainActivity.onClick(MainActivity.java:92)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
08-24 22:43:53.601 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 8.662ms
08-24 22:44:10.123 3104-3110/com.example.cmleu_000.myapplication W/art: Suspending all threads took: 5.787ms
*new error after edditing
You've defined the maimage variable twice. The class variable maimage that you're trying to access in the onClick(...) method is null (it was never initialized).
To fix this, change:
final ImageView maimage = (ImageView) findViewById(R.id.maimage);
to:
maimage = (ImageView) findViewById(R.id.maimage);
This way you will initialize the class variable instead of defining a new variable with the same name.
The same goes for all the other variables that you're defining again in the onCreate(...) method.
I am trying to have the user fill out a form then those fields auto-populate into an email. I was able to convert the EditText to strings as well as the spinner, but I am confused as to how to do it with RadioGroup. I want the text from the button which is selected to appear in the email. I know that getText won't work, but what will. Thank you
JAVA
//This happens when you press the submit button at the bottom of the form.
public void submit(View button) {
// Do click handling here
//What happens here is the input from each field is taken in and converted into
//Strings and placed into their correct variables.
final EditText FirstNameField = (EditText) findViewById(R.id.EditTextFirstName);
fName = FirstNameField.getText().toString();
final EditText LastNameField = (EditText) findViewById(R.id.EditTextLastName);
lName = LastNameField.getText().toString();
final EditText emailField = (EditText) findViewById(R.id.EditTextEmail);
email = emailField.getText().toString();
final EditText feedbackField = (EditText) findViewById(R.id.EditTextFeedbackBody);
feedback = feedbackField.getText().toString();
final Spinner feedbackSpinner = (Spinner) findViewById(R.id.SpinnerFeedbackType);
feedbackType = feedbackSpinner.getSelectedItem().toString();
final RadioGroup ApproveorReject = (RadioGroup) findViewById(R.id.radiochoice);
fAnswer = ApproveorReject.getText().toString();
//calls the sendEmail() from below to pull up a list of apps installed
//on the phone that can handle emailing.
sendEmail();
}
//This bit of code pulls up a list of apps that can handle emailing.
private void sendEmail() {
//When the user chooses an app of the list that pops up, these lines below
//will auto-populate the fields of the email with the input from above.
//The user can take one last look at the email before hitting that send button
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, feedbackType);
i.putExtra(Intent.EXTRA_TEXT, lName + ", " + fName + "\n" + email + "\n" + feedback + fAnswer );
startActivity(Intent.createChooser(i, "Send mail..."));
}
}
XML
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:id="#+id/TextViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/feedbacktitle"
android:textSize="10pt">
</TextView>
<!--First Name-->
<EditText
android:id="#+id/EditTextFirstName"
android:layout_height="wrap_content"
android:hint="#string/feedbackfirst"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#+id/TextViewTitle">
</EditText>
<!--Last Name-->
<EditText
android:id="#+id/EditTextLastName"
android:layout_height="wrap_content"
android:hint="#string/feedbacklast"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextFirstName">
</EditText>
<!-- Email -->
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/feedbackemail"
android:inputType="textEmailAddress"
android:layout_width="fill_parent"
android:layout_below="#id/EditTextLastName">
</EditText>
<Spinner
android:id="#+id/SpinnerFeedbackType"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:entries="#array/Types_of_Errors"
android:layout_below="#+id/EditTextEmail">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/feedbackbody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent"
android:layout_below="#+id/SpinnerFeedbackType">
</EditText>
<RadioGroup
android:id="#+id/radiochoice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/EditTextFeedbackBody"
android:orientation="horizontal">
<RadioButton
android:id="#+id/AcceptRadio"
android:layout_width="wrap_content"
android:layout_marginStart="50dp"
android:layout_height="wrap_content"
android:text="#string/acceptbutton" />
<RadioButton
android:id="#+id/RejectRadio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rejectbutton"
android:layout_marginStart="115dp" />
</RadioGroup>
<EditText
android:id="#+id/RejectResponse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/rejectreason"
android:layout_marginTop="410dp"
android:layout_alignParentEnd="true"
android:layout_marginRight="20dp"
android:inputType="text"/>
<Button
android:id="#+id/ButtonSend"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sendform"
android:layout_centerHorizontal="true"
android:layout_marginTop="590dp"
android:onClick="submit"/>
</RelativeLayout>
Try this
RadioButton btn=(RadioButton)findViewById(ApproveorReject.getCheckedRadioButtonId());
String selectedText=btn.getText().toString();
Try
ApproveorReject.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb=(RadioButton)findViewById(checkedId);
String param = rb.getText();
}
});
I want that when I click on an element of GridView displayed letter, which I clicked. But initially I got here error when I wanted to take the position of the element. If I understood correctly, gridview does not see the textview, but i don't know why.
Error:
5312-5312/standandroid.ru.words E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:240)
at android.content.res.MiuiResources.getText(MiuiResources.java:131)
at android.widget.Toast.makeText(Toast.java:273)
at standandroid.ru.words.MainActivity$1$1.onItemClick(MainActivity.java:70)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1130)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2818)
at android.widget.AbsListView$1.run(AbsListView.java:3498)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5137)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:756)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:572)
at miui.dexspy.DexspyInstaller.main(DexspyInstaller.java:171)
at dalvik.system.NativeStart.main(Native Method)
MainActivity.java :
public class MainActivity extends Activity {
TextView txt, textTV;
EditText etxt;
Button btn;
ArrayAdapter<String> adapter;
GridView gridView;
String name;
char c;
ArrayList arrayList=new ArrayList();
int i;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (TextView) findViewById(R.id.textView);
etxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
textTV = (TextView) findViewById(R.id.tv);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = etxt.getText().toString();
TreeSet list = new TreeSet();
for (i = 0; i < name.length(); i++) {
c = name.charAt(i);
list.add(c);
}
arrayList = new ArrayList(list);
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.item, R.id.tv, arrayList);
gridView = (GridView) findViewById(R.id.gridmain);
gridView.setAdapter(adapter);
ViewGrid();
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, position,Toast.LENGTH_LONG).show();
}
});
}
});}
private void ViewGrid(){
gridView.setNumColumns(4);
gridView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH); }}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/textView"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="175dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:singleLine="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="48dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/editText"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<GridView
android:id="#+id/gridmain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:layout_alignTop="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></GridView>
</RelativeLayout>
item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tv"
android:textSize="40sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
http://i.stack.imgur.com/j5S93.png
Ohh! I see, the issue is that Toast is thinking of position as a resourceID. If you see Toast specs here, you would see that you can define resourceId or a CharSequence. So make sure you are passing the position as a String. Something like:
Toast.makeText(MainActivity.this, String.valueOf(position),Toast.LENGTH_LONG).show();
I'm doing a very simple login screen where the user inputs their username and password, clicks the login button, and then the inputs are displayed on the next screen.
I get a NullPointerException every time I try to change the textview values. I've been googling for a long time and come up with nothing, and it has to be something simple that I'm just completely missing.
Following is my code:
public class LoggedIn extends Activity{
String username = "";
String password = "";
TextView name;
TextView pwd;
Button infoDisp;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.logged_in);
Intent intent = getIntent();
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
username = intent.getStringExtra("nameInfo");
password = intent.getStringExtra("passInfo");
name.setText(username);
pwd.setText(password);
}
}
Edit: I changed the bundle to an intent above, also here's the rest of the code (The first activity)
public class LoginActivity extends Activity {
Button loginBtn;
Button registerBtn;
EditText username;
EditText pword;
static String name;
static String pass;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) this.findViewById(R.id.username);
pword = (EditText) this.findViewById(R.id.password);
loginBtn = (Button) this.findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0){
Intent intent = new Intent(LoginActivity.this, LoggedIn.class);
name = username.getText().toString();
pass = pword.getText().toString();
intent.putExtra("nameInfo", name);
intent.putExtra("passInfo", pass);
startActivity(intent);
}
});
}
}
I have no error checking for if the value is null on purpose because this was supposed to be just a quick run and done thing. I figure as long as I input something in each EditText, then the strings can't be null and I won't have a problem... right?
Stacktrace:
FATAL EXCEPTION: main
Process: com.example.loginscreen, PID: 1677
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.LoggedIn}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.loginscreen.LoggedIn.onCreate(LoggedIn.java:30)
at android.app.Activity.performCreate(Activity.java:5243)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
... 11 more
Aaaand xml files:
activity_login:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Login:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="38dp"
android:text="Password:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/textView1"
android:ems="10" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/textView2"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="26dp"
android:text="Login" />
<Button
android:id="#+id/registerBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/loginBtn"
android:layout_alignBottom="#+id/loginBtn"
android:layout_alignLeft="#+id/textView3"
android:layout_marginLeft="46dp"
android:text="Register" />
<CheckBox
android:id="#+id/remPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/loginBtn"
android:layout_below="#+id/loginBtn"
android:layout_marginTop="19dp"
android:text="Remember Password" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/remPass"
android:layout_marginTop="28dp"
android:layout_toRightOf="#+id/textView1"
android:text="Forgot Password" />
logged_in:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Logged In!"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="56dp"
android:text="Username: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="56dp"
android:text="Password: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/recUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_marginLeft="33dp"
android:layout_toRightOf="#+id/textView2"
android:editable="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/recPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/textView4"
android:editable="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/infoBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView3"
android:layout_marginTop="62dp"
android:text="Press to Display Info" />
The problem is because you are finding id for TextView s are wrong.. so just change from
username = (TextView) this.findViewById(R.id.username);
pword = (TextView) this.findViewById(R.id.password);
to
username = (TextView) this.findViewById(R.id.recUsername);
pword = (TextView) this.findViewById(R.id.recPassword);
You should replace this in your LoggedIn Activity. You have wrong ids when initialization of views logged_in.xml
username = (EditText) this.findViewById(R.id.username);
pword = (EditText) this.findViewById(R.id.password);
loginBtn = (Button) this.findViewById(R.id.loginBtn);
With
TextView username = (TextView)findViewById(R.id.recUsername);
TextView pword = (TextView)findViewById(R.id.recPassword);
loginBtn = (Button)findViewById(R.id.infoBtn);
Change
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
to
name = (TextView)findViewById(R.id.recUsername);
pwd = (TextView)findViewById(R.id.recPassword);
In LoggedIn.java
Your NUllPointerExceptipon is ccoz you referencing wrong ids for your views. findViewById looks for a view in the current infalted layout. Since it does not find one. Your initialization fails leading to NullPointerExcpetion.
I think there is a problem with following line
Intent intent = new Intent(LoginActivity.this, LoggedIn.class);
change it to
Intent intent = new Intent(this, LoggedIn.class);
or
Intent intent = new Intent(getApplicationContext(), LoggedIn.class);
In your LoggedIn class the ids are wrong
name = (TextView)findViewById(R.id.username);
pwd = (TextView)findViewById(R.id.password);
change the above ids to defined ids in the logged_in.xml layout and try again
Change this...
Bundle bundle = getIntent().getExtras();
to...
Intent bundle = getIntent();
Then use this to retrieve extras....
username = bundle.getStringExtra("nameInfo");
password = bundle.getStringExtra("passInfo");
Update:
You are trying to get TextViews using those ids which does not exist in the current layout xml...that is, you are using wrong id to retrieve your Textviews. So, change those ids in these following lines...
username = (TextView) this.findViewById(R.id.username);
pword = (TextView) this.findViewById(R.id.password);
to
username = (TextView) this.findViewById(R.id.recUsername);
pword = (TextView) this.findViewById(R.id.recPassword);