Button doesn't respond on anything when pressed - android

What I'm facing is that whenever I press the button to save data to check if the fields is filled up and if it is then it will pass data to back4app but nothing happens even. The Android monitor/Run doesnt even show that the button is being pressed nor the Logcat.
I have a log in activity, register activity, and the main activity. After Logging in it will take you to the main activity which is empty for now but you need to use the navigation bar to take you to this activity and even the fields are filled or not the button does not respond at all. I put a toast to make sure that the button works but the toast doesnt show.
The code below I just copied in the registration form which is the same concept, fill the form test if theres something in there, if not then message will appear then it will send data to back4app. But the button in this problem doesnt work.
This is the button command
package com.example.back4app.userregistrationexample.Classes;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import com.example.back4app.userregistrationexample.R;
import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseUser;
public class Purchases extends AppCompatActivity {
private EditText establishmentView;
private EditText particularsView;
private EditText amountView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_bar_purchases);
establishmentView = (EditText) findViewById(R.id.Edt_Establishment);
particularsView = (EditText) findViewById(R.id.Edt_Particular);
amountView = (EditText) findViewById(R.id.Edt_purchasesnumber);
final Button save = (Button) findViewById(R.id.btn_purchasessave);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
boolean validationError = false;
StringBuilder validationErrorMessage = new StringBuilder("Please, insert ");
if (isEmpty(establishmentView)) {
validationError = true;
validationErrorMessage.append("an Establisment");
}
if (isEmpty(particularsView)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("a Particular");
}
if (isEmpty(amountView)) {
if (validationError) {
validationErrorMessage.append(" and ");
}
validationError = true;
validationErrorMessage.append("a Amount");
}
validationErrorMessage.append(".");
if (validationError) {
Toast.makeText(Purchases.this, validationErrorMessage.toString(), Toast.LENGTH_LONG).show();
return;
}
final ProgressDialog dlg = new ProgressDialog(Purchases.this);
dlg.setTitle("Please, wait a moment.");
dlg.setMessage("Signing up...");
dlg.show();
Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show();
}
});
}
private boolean isEmpty(EditText text) {
if (text.getText().toString().trim().length() > 0) {
return false;
} else {
return true;
}
}
private boolean isMatching(EditText text1, EditText text2){
if(text1.getText().toString().equals(text2.getText().toString())){
return true;
}
else{
return false;
}
}
private void alertDisplayer(String title,String message){
AlertDialog.Builder builder = new AlertDialog.Builder(Purchases.this)
.setTitle(title)
.setMessage(message)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
Intent intent = new Intent(Purchases.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ParseUser.logOut();
}
});
AlertDialog ok = builder.create();
ok.show();
}
}
Edit: This is the app_bar_purchases.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/txt_puchasesbal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="Balance" />
<EditText
android:id="#+id/Edt_Establishment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:ems="10"
android:hint="Establishment"
android:inputType="textPersonName" />
<EditText
android:id="#+id/Edt_Particular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:ems="10"
android:hint="Particulars"
android:inputType="textPersonName" />
<EditText
android:id="#+id/Edt_purchasesnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="197dp"
android:ems="10"
android:hint="Amount"
android:inputType="number" />
<Button
android:id="#+id/btn_purchasessave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="178dp"
android:text="Save" />
</RelativeLayout>
Heres what happen when I try to debug it and put break in boolean same thing happens when I put the break in button save it doesnt show anything on the Debug no errors or anything
EDIT I have pasted the right xml file
EDIT Does having RelativeLayout makes the button not respond?
EDIT I have pasted the whole java class
EDIT I also tried deleting everything else on the onClick except for
Toast.makeText(getApplicationContext(),"Data Saved",Toast.LENGTH_SHORT).show(); just to see if the button itself works but it didn't even show the toast

Please check your button id in xml file it's btn_addbalance
then check your id in java file it's btn_purchasessave
please correct this check below code
your line
final Button save = findViewById(R.id.btn_purchasessave);
replace with
final Button save = findViewById(R.id.btn_addbalance);
it will help to you

I took reference of a SO question,
Try using this :
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), "clicked", Toast.LENGTH_LONG).show();
}
});
And import this
import android.view.View.OnClickListener;
EDIT
Casting most of the android views is reduntant now but it can be the problem, Try to caste the button like this :
final Button save = (Button) findViewById(R.id.btn_purchasessave);
and try your code.
EDIT 2
Try using save.setOnClickListener(this) like this,
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText establishmentView;
private EditText particularsView;
private EditText amountView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
establishmentView = (EditText) findViewById(R.id.Edt_Establishment);
particularsView = (EditText) findViewById(R.id.Edt_Particular);
amountView = (EditText) findViewById(R.id.Edt_purchasesnumber);
final Button save = (Button) findViewById(R.id.btn_purchasessave);
save.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//do things here
}

Related

Use java file for a xml layout file used in a dialog box

I need to use a .java file to perform actions for a .xml layout. I have used this layout for a dialog box. I have linked the .java file to that layout but it doesn't seem to work at all.
This is the MainActivity
package com.example.ayush.projectfive;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alrt.show();
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}
This is the activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
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.ayush.projectfive.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EXIT"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
This is the dialog box layout
<?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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="LOGIN"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:layout_marginTop="20dp"
android:id="#+id/editText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:layout_marginTop="15dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="20dp"
android:id="#+id/button3" />
This is the .java file I'd like to link with.
package com.example.ayush.projectfive;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Second extends AppCompatActivity{
EditText et, et2;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
et = (EditText) findViewById(R.id.editText);
et2 = (EditText) findViewById(R.id.editText2);
btn2 = (Button) findViewById(R.id.button3);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = et.getText().toString();
String s2 = et.getText().toString();
if(s1.equals(s2)){
Toast.makeText(Second.this, "Login Successful", Toast.LENGTH_SHORT).show();
Intent i = new Intent(Second.this,Third.class);
startActivity(i);
}
else{
}
}
});
}
}
This is the .java file I'd like to link with [...]
public class Second extends AppCompatActivity{
Okay... That's what this code does
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
If you are trying to use the views that are contained within the dialog box and respond to the buttons there to login, then you need to remove the button from the activity, add alrt.show() in the onCreate instead of onClick and move the Activity start code into the dialog's onClick handlers.
It's also worth mentioning that the object is a builder, so you can do this
View dialogView = LayoutInflater.from(MainActivity.this).inflate(R.layout.mylayout, null);
final EditText editUsername = dialogView.findViewById(R.id.editText);
// TODO: Get other views from mylayout.xml
new AlertDialog.Builder(MainActivity.this)
.setIcon(R.mipmap.ic_launcher)
.setTitle("Login")
.setCancelable(false)
.setView(dialogView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String username = editUsername.getText().toString();
// TODO: Get more variables
// TODO: Verify credentials
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
})
.setNegativeButton("CANCEL", null)
.show();
And, as stated earlier, remove btn.setOnClickListener
just add alrt.show(); in your MainActivity.java e.g. here:
public class MainActivity extends AppCompatActivity {
Button btn;
AlertDialog.Builder alrt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
alrt = new AlertDialog.Builder(MainActivity.this);
alrt.setIcon(R.mipmap.ic_launcher);
alrt.setTitle("Login");
alrt.setCancelable(false);
alrt.setView(R.layout.mylayout);
alrt.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alrt.show();
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
}
});
}
}

Eclipse Android Checkbox

I would like to create checkbox with text that would behave something like what i have below in pseudo code:
Set in text in EditText field
if Button clicked:
create new Checkbox with entered text.
How do i go about doing that in Android?
So far ive got the button :) ... but dont know how to get new Checkbox with text in it ,if its clicked
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
Button create = (Button) findViewById(R.id.button1);
create.setOnClickListener(this);
EditText textInCheckBox = (EditText) findViewById(R.id.editText1);
}
#Override
public void onClick(View v) {
// TODO Automatisch generierter Methodenstub
switch (v.getId()) {
case R.id.button1:
//after creating new checkbox with text in it
//Editbox should restet text field to null;
break;
}
}
}
Here is some code that will demonstrate how to add checkboxes dynamically with the title you entered into the EditText.
MainActivity.java
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText txtTitle = (EditText) findViewById(R.id.txtTitle);
final Button btnCreate = (Button) findViewById(R.id.btnCreate);
final RadioGroup radContainer = (RadioGroup) findViewById(R.id.radContainer);
final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name = txtTitle.getText().toString().trim();
if (!name.isEmpty()) {
CheckBox checkBox = new CheckBox(MainActivity.this);
checkBox.setText(name);
checkBox.setLayoutParams(params);
radContainer.addView(checkBox);
} else {
Toast.makeText(MainActivity.this, "Enter a title for the checkbox", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp" >
<EditText
android:id="#+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone" />
<Button
android:id="#+id/btnCreate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create Checkbox" />
<RadioGroup
android:id="#+id/radContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Android speech to text using listview

The code below is used to convert speech to text on an android app. This code works when the speech button is pressed and whatever you say is transferred into text.
When the speech function closes, the the text is showing.
If you hit the speech button again, the previous text is cleared and the new text shows on the page. What i want to do is everytime i click the speak button, i want to show the word on the page.. then if i click the speak button again, i want the next word to appear under the first word in a list format.
i am looking to use listview but am struggling. here is the original code and the code i am trying to write to make it a listview. can anyone assist?
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/textView1"
android:layout_toLeftOf="#+id/textView1"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/btnSpeak"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/speak"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/txtText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Code:
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Ops! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
In your onActivityResult, you are overriding your TextView with the first word only(index 0).
You must iterate through the String array list like so:
String words = "";
for ( String word : text ) {
words += " " + word;
}
txtText.setText(words);
And here's a nice article to help you implement your ListView:
ListView tutorial
So now when you figured that out you add the words value in the listview.
Hope this helps,
Cheers

Cannot read EditText in fragment class

I have two tabs and each tab is a Fragment. In one of the fragments (Afragment.claas) I have a dialog box to insert some data, and I want to save them in a database. When I fill the text fields and press OK, the application stops.
I think when I initialize the edittext there is something wrong.
name = (EditText) activity.findViewById(R.id.etProviderName);
The above statement is returning null
Here is my code:
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class Afragment extends ListFragment {
DBAdapter db;
Activity activity;
private static final String fields[] = { "provider._providerName", "_providerMobile" };
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initAdapter();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
add();
return (true);
case R.id.menu_reset:
initAdapter();
return (true);
case R.id.menu_info:
Toast.makeText(activity, "Developed By Omar Al-Shammary", Toast.LENGTH_LONG)
.show();
return (true);
}
return (super.onOptionsItemSelected(item));
}
private void add() {
// TODO Auto-generated method stub
System.out.println("insider Add Method");
final View addView = activity.getLayoutInflater().inflate(R.layout.add_provider, null);
new AlertDialog.Builder(activity).setTitle("Add a Provider").setView(addView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
System.out.println("Befor calling insert in providers");
insertInProviders();
System.out.println("after calling insert in providers");
}
}).setNegativeButton("Cancel", null).show();
initAdapter();
}
private void initAdapter() {
activity = getActivity();
db = new DBAdapter(activity);
fillTheList();
}
public void fillTheList()
{
db.open();
Cursor data = db.getAllProviders();
CursorAdapter dataSource = new SimpleCursorAdapter(activity,
R.layout.row, data, fields,
new int[] { R.id.first, R.id.last });
setListAdapter(dataSource);
db.close();
setListAdapter(dataSource);
}
public void insertInProviders()
{
EditText name,location,number;
name = (EditText) activity.findViewById(R.id.etProviderName);
location = (EditText) activity.findViewById(R.id.etProviderName);
number = (EditText) activity.findViewById(R.id.etProviderName);
if(name == null)
System.out.println("EditeText name is null");
String provName = name.getText().toString();
if(location == null)
System.out.println("location is null");
String provLocation = location.getText().toString();
String provNumber = number.getText().toString();
System.out.println("Before Open");
db.open();
System.out.println("Before DB.Insert");
db.insertProvider(provName, provLocation, provNumber);
System.out.println("Before Close");
db.close();
}
}
add_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tvProviderName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/etProviderName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tvProvLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location" />
<EditText
android:id="#+id/etProvLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="#+id/tvProvNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number" />
<EditText
android:id="#+id/etProvNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
This is the code I use for declaring buttons in Fragments - maybe it can help you.
What I would do is access the inflated layout and then you should be able to access items underneath it. Let me know if that makes sense.
LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
// Register for the Button.OnClick event
Button b = (Button)theLayout.findViewById(R.id.frag1_button);
#Override
public void onClick(View v) {
Toast.makeText(Tab1Fragment.this.getActivity(), "OnClickMe button clicked", Toast.LENGTH_LONG).show();
}
});
return theLayout;
So maybe you should try this since you inflate your View as "addView"
name = (EditText) addView.findViewById(R.id.etProviderName);
The findViewById should not be called by the activity but by the enclosing view. You should have something like:
In the class
View aView;
In the method:
LayoutInflater inflater = getActivity().getLayoutInflater();
aView = inflater.inflate(R.layout.add_provider, null);
...
EditText name = (EditText) aView.findViewById(R.id.etProviderName);
I hope it helps.

EditText.getText Java.lang nullpointer exception

I'm having a problem with my android program.
I have about every thing working what i want, but now im trying to add a password dialog.
I want to show a Dialog with a EditText(enter password) and two buttons (ok, cancel).
(working fine till so far)
When clicking on the OK button the password should be saved in mij SendData Class.
But every time I try to get the content of the EditText it gives a Java.lang NullPointerException.
Can some one help me please?
(I don't have a stack trace as i don't know where to find it :( because i'm testing on android emulator.)
If there any thing else you need, please feel free to ask.
Here is the XML-FILE
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<requestFocus />
<EditText
android:id="#+id/txt_wachtwoord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/DialogOK"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txt_wachtwoord"
android:layout_below="#+id/txt_wachtwoord"
android:layout_marginTop="22dp"
android:text="OK" />
<Button
android:id="#+id/Cancel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/DialogOK"
android:layout_alignBottom="#+id/DialogOK"
android:layout_toRightOf="#+id/DialogOK"
android:text="Cancel" />
</RelativeLayout>
Here is the code:
package SebApp.Phone.Remote;
import java.net.DatagramSocket;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Vibrator;
import android.app.AlertDialog;
import android.content.DialogInterface;
public class PhoneRemote extends Activity {
private static final int VIBRATE_TIME = 100;
/** Called when the activity is first created. */
public Vibrator vibrator = null;
public sendData sd = new sendData();
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
//button to call the Dialog
Button ww = (Button) findViewById(R.id.ww);
//listener password button
ww.setOnClickListener(wwOnClickListener);
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
//there are a lot of settings, for dialog, cheout!
//set up button
Button button = (Button) dialog.findViewById(R.id.DialogOK);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try{
Toast.makeText(v.getContext(),"PWfield contains: " + ww.getText().toString(), Toast.LENGTH_SHORT).show();
//finish();
}catch(Exception e)
{
Toast.makeText(v.getContext(),e.toString(), Toast.LENGTH_SHORT).show();
}
}
});
dialog.show();
}catch(Exception E){
Toast.makeText(v.getContext(),E.toString(), Toast.LENGTH_SHORT).show();
}
}
};
}
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
//set up dialog
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
instead use
Button.OnClickListener wwOnClickListener = new Button.OnClickListener() {
public void onClick(View v) {
try{
Dialog dialog = new Dialog(PhoneRemote.this);
dialog.setContentView(R.layout.wachtwoord);
final EditText ww = (EditText) dialog.findViewById(R.id.txt_wachtwoord);
//set up dialog
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
....
you had declared EditText before setting the dialog view
I have yet to run your code. However, I would say the variable wwOnClickListener is being initialized before the onCreate method.
Therefore the layout is not set, as the layout is set within the onCreate method. To fix this I would pull out the following
final EditText ww = (EditText) findViewById(R.id.txt_wachtwoord);
From the buttonlistener and make ww a global variable which is initialized in the onCreate method.
I would expect that will resolve the issue.

Categories

Resources