My app crashes when I touch save button (in Eclipse) - android

I'm trying to do a Contact Manager but I have one problem: My app crashes when I touch save button for saving my new contact, and I don't know why because Eclipse don't says anything is wrong in my code. I touch the button and the app crashes, but it creates a new database.
public class Insertarcontactes extends Activity {
private TextView mTextView;
private EditText mNom;
private EditText mCognoms;
private EditText mAdressa;
private EditText mFixe;
private EditText mMobil;
private EditText mEmail;
private DatabaseManager mDBM;
protected Cursor mCursor;
private SimpleCursorAdapter mAdapter;
private Button mguardar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.insertarcontactes);
mDBM = new DatabaseManager();
mDBM.openDB(this);
mTextView = (TextView) findViewById(R.id.textView1);
mNom = (EditText) findViewById(R.id.nom);
mCognoms = (EditText) findViewById(R.id.cognoms);
mAdressa = (EditText) findViewById(R.id.adressa);
mFixe = (EditText) findViewById(R.id.telefonfix);
mMobil = (EditText) findViewById(R.id.telefonmobil);
mEmail = (EditText) findViewById(R.id.email);
mguardar = (Button)findViewById(R.id.guardar);
}
public void guardalo(View v) {
if(!mNom.getText().toString().equals("")) {
mDBM.insertarContacto(
mNom.getText().toString(),
mCognoms.getText().toString(),
mAdressa.getText().toString(),
mFixe.getText().toString(),
mMobil.getText().toString(),
mEmail.getText().toString()
);
mNom.setText("");
mCognoms.setText("");
mAdressa.setText("");
mFixe.setText("");
mMobil.setText("");
mEmail.setText("");
mCursor.requery();
mAdapter.notifyDataSetChanged();
Intent vesalallista = new Intent(this, Llistacontactes.class);
startActivity(vesalallista);
}
}
public void tornaenrere(View v){
Intent tornaenrere = new Intent(this, Menuprincipal.class);
startActivity(tornaenrere);
}
#Override
protected void onDestroy() {
mDBM.closeDB();
super.onDestroy();
}

Go to your android manifest file and add write contacts permission

Your mCursor is un-initialized, and you are calling its requery() method mCursor.requery(); in guardalo() method. Hence it should be giving you NullPointerException
You need to initialize this mCursor object in onCreate() method.

Related

How do I keep data in my Deque in Android Studio on Buttonclick?

I am trying to save data to a Deque on buttonclick and display the data in a tablelayout. My issue is that every time I trigger the onclick, the Deque loses all previous data. I tried to instantiate the Deque outside the onCreate method and it did not work. I am using a Deque as a stack because I need to display the data LIFO. Any help on this would be very much appreciated. Here is what I've tried so far:
public class MainActivity extends AppCompatActivity {
private Button buttonAdd;
private EditText editText1;
private Deque<String> input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText1 = (EditText)findViewById(R.id.edit_text_1);
final Deque<String> input = new ArrayDeque<String>();
buttonAdd = (Button) findViewById(R.id.button_add);
buttonAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TableLayout table = (TableLayout) findViewById(R.id.tableLayout1);
String data = editText1.getText().toString();
input.addFirst(data);
Deque<String> inputData = input;
while (!inputData.isEmpty()) {
String s = inputData.removeFirst();
TableRow row = new TableRow(MainActivity.this);
TextView textViewData = new TextView(MainActivity.this);
textViewData.setText(s);
textViewData.setGravity(Gravity.CENTER);
row.addView(textViewData);
table.addView(row);
}
}
});
}
}
The problem is this line:
String s = inputData.removeFirst();
With this operation, you remove (delete) the next entry and since you do it in a loop:
while (!inputData.isEmpty()) {
it simply empties the queue.
What you want is to iterate over the queue without removing anything:
for (String element : inputData) {
textViewData.setText(s);
}

Keeping strings in a login dialog window when orientation change. Android

I'm trying to keep what users typed in my login dialog window when orientation changing, but i always receive this error message:
Attempt to invoke virtual method 'android.view.View
android.widget.RelativeLayout.findViewById(int)' on a null object
reference.
There's the code:
public class ReservationActivity extends AppCompatActivity {
ImageView uDeM_Logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reservation);
uDeM_Logo = (ImageView)findViewById(R.id.UdeM_Logo);
dimensions();
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
RelativeLayout login = (RelativeLayout)findViewById(R.id.loginLayout);
EditText userField = (EditText) login.findViewById(R.id.userEditText);
EditText passField = (EditText) login.findViewById(R.id.passEditText);
String user = userField.getText().toString();
String pass = passField.getText().toString();
outState.putString("User", user);
outState.putString("Pass", pass);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
RelativeLayout login = (RelativeLayout) findViewById(R.id.loginLayout);
EditText userField = (EditText) login.findViewById(R.id.userEditText);
EditText passField = (EditText) login.findViewById(R.id.passEditText);
String user = savedInstanceState.getString("User");
String pass = savedInstanceState.getString("Pass");
userField.setText(user);
passField.setText(pass);
}
public void onConfigurationChanged(Configuration nouvOrient) {
if(nouvOrient.orientation == Configuration.ORIENTATION_LANDSCAPE ||
nouvOrient.orientation == Configuration.ORIENTATION_PORTRAIT)
dimensions();
}
public void dimensions() {
Display display = getWindowManager().getDefaultDisplay();
Point grandeur = new Point();
display.getSize(grandeur);
double hauteur = grandeur.y, dim = hauteur * 0.2492;
int dimsInt = (int) dim;
ViewGroup.LayoutParams parametres = uDeM_Logo.getLayoutParams();
parametres.width = dimsInt;
parametres.height = dimsInt;
uDeM_Logo.setLayoutParams(parametres);
}
public void loginDialog(View log){
final Dialog login = new Dialog(this);
login.setContentView(R.layout.login_dialog);
Button btnLogin = (Button)login.findViewById(R.id.dialogLoginBtn);
Button btnCancel = (Button)login.findViewById(R.id.dialogCancelBtn);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(ReservationActivity.this,
"Login Sucessfull", Toast.LENGTH_SHORT).show();
}
});
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
login.dismiss();
}
});
login.show();
}
}
It seems that you do not need to recreate the activity on orientation change. You can just set this configuration for your activity in your manifest.
android:configChanges="keyboardHidden|orientation
don't do that in onRestoreInstanceState()
RelativeLayout login = (RelativeLayout) findViewById(R.id.loginLayout);
EditText userField = (EditText) login.findViewById(R.id.userEditText);
EditText passField = (EditText) login.findViewById(R.id.passEditText);
But instead make these login, userField, passField a field variables in your Activity class as follows:
private RelativeLayout login;
private EditText userField, passField;
Also move the above 3 code lines from onSaveInstanceState() to onCreate() but modify them to work with your field variables as follows:
login = (RelativeLayout) findViewById(R.id.loginLayout);
userField = (EditText) login.findViewById(R.id.userEditText);
passField = (EditText) login.findViewById(R.id.passEditText);
And I recommend you to restore state at onCreate() instead of onRestoreInstanceState() as follows:
// Check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// Restore value of members from saved state
String user = savedInstanceState.getString("User");
String pass = savedInstanceState.getString("Pass");
userField.setText(user);
passField.setText(pass);
} else {
// Probably initialize members with default values for a new instance
}
The reason is because onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart()

Hi! How do I store the fields and display the results on a different activity ? Please

I want the fields entered by the user to be displayed as a list view of the studentnoText and courseField in a new activity..
public class Student extends Activity {
private String totalStudents [];
private EditText studentnoText;
private EditText studentnameText;
private EditText courseField;
private Spinner departmentSpinner;
private EditText dobField;
private Button registerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student);
studentnoText = (EditText) findViewById(R.id.studentnoText);
studentnameText = (EditText) findViewById(R.id.studentnameText);
dobField = (EditText) findViewById(R.id.dobField);
courseField = (EditText) findViewById(R.id.courseField);
departmentSpinner = (Spinner) findViewById(R.id.departmentSpinner);
registerButton = (Button) findViewById(R.id.registerButton);
}
public void registerButtonPressed (View view){
Log.v("Register","Register Pressed!");
}
}
USE PUT EXTRA TO SEND DATA
mIntent.putExtra("TEXT", studentnoText.gettext().toString());
mIntent.putExtra("TEXT_1", studentName.gettext().toString());
USE GET EXTRA TO GET DATA
String strNO,strName;
strNO= extras.getString("TEXT");
strName = extras.getString("TEXT_1");

EditText to phone memory?

I have found a very good solution to my problem on another post (Save entered text in editText via button)
however when I implement this code, my application crashes. Any advice would be appreciated, the error I receive is that the "String or" in the method makeTag() is not used. Please have a look
private Button savenotebutton1;
private SharedPreferences savednotes;
private EditText editText1;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.x1);
savenotebutton1 = (Button) findViewById(R.id.savenotebutton1);
editText1 = (EditText) findViewById(R.id.noteEditText1);
savednotes = getSharedPreferences("notes",MODE_PRIVATE);
editText1.setText(savednotes.getString("tag", "Default Value")); //add this line
savenotebutton1.setOnClickListener(saveButtonListener);
}
private void makeTag(String tag){
String or = savednotes.getString(tag, null);
SharedPreferences.Editor preferencesEditor = savednotes.edit();
preferencesEditor.putString("tag",tag); //change this line to this
preferencesEditor.commit();
}
public OnClickListener saveButtonListener = new OnClickListener(){
#Override
public void onClick(View v) {
if(editText1.getText().length()>0){
makeTag(editText1.getText().toString());
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText1.getWindowToken(),0);
}
}
};
}
You should replace this
String or = savednotes.getString(tag, null);
With
String or = savednotes.getString("tag", "Default Value")
Under your makeTag() function
Update: Error is about you are not register your activity into manifest.xml file.

Getting null reference exception when switching from one intent to another intent

I am getting null reference exception in the childActivity intent.I am newbie to the android development.Please help me to resolve this problem.What i am doing to sending data from ParentActivity to ChildActivity.
ParentActivity :I have 3 EditText fields in the ParentActivity.I want to display these 3 fields into ChildActivity.There is a button control so when i click on it,It has to switch from ParentActivity to ChildActivity.
public class ParentActivity extends Activity {
private EditText EmpId;
private EditText EmpName;
private EditText Gender;
private Button btnShowInfo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parent);
InitializeControls();
}
private void InitializeControls() {
EmpId = (EditText) findViewById(R.id.editText1);
EmpName = (EditText) findViewById(R.id.editText2);
Gender = (EditText) findViewById(R.id.editText3);
btnShowInfo = (Button) findViewById(R.id.button1);
final Context context = this;
btnShowInfo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent ParentIntent = new Intent(context, ChildActivity.class);
ParentIntent.putExtra("Eid", EmpId.getText().toString());
ParentIntent.putExtra("EName", EmpName.getText().toString());
ParentIntent.putExtra("EGender", Gender.getText().toString());
startActivity(ParentIntent);
}
});
}
}
ChildActivity :: It has to show the values which i entered in the ParentActivity.
public class ChildActivity extends Activity {
private TextView txtView1;
private TextView txtView2;
private TextView txtView3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
InitializeControls();
}
private void InitializeControls() {
txtView1 = (TextView) findViewById(R.id.editText1);
txtView2 = (TextView) findViewById(R.id.editText2);
txtView3 = (TextView) findViewById(R.id.editText3);
GetDataFromIntent();
}
private void GetDataFromIntent() {
Bundle extras = getIntent().getExtras();
if (extras == null) {
txtView1.setText("No data is received.");
} else {
txtView1.setText(extras.getString("Eid"));
txtView2.setText(extras.getString("EName"));
txtView3.setText(extras.getString("EGender"));
}
}
}
Exception :: This is what i am getting in the Console window.
01-05 15:04:46.210: E/AndroidRuntime(1658): Caused by: java.lang.NullPointerException
01-05 15:04:46.210: E/AndroidRuntime(1658): at com.example.androidexample1.ChildActivity.GetDataFromIntent(ChildActivity.java:36)
01-05 15:04:46.210: E/AndroidRuntime(1658): at com.example.androidexample1.ChildActivity.InitializeControls(ChildActivity.java:26)
01-05 15:04:46.210: E/AndroidRuntime(1658): at com.example.androidexample1.ChildActivity.onCreate(ChildActivity.java:19)
I am newbie to the android world.Please help on this.
So the line of code that is causing the issue is:
txtView1.setText(extras.getString("Eid"));
A null pointer exception essentially means that you are trying to call a function on something that's null. In this case, it could be either txtView1 or extras. As you check to see if extra is null, it must in fact be the txtView1 that is null. Per Android documents, findViewById returns:
Returns
The view that has the given id in the hierarchy or null
Thus, if you misspelled your text labels, it could cause this problem. A good way to check is by adding this right before your error.
Log.v("Child","txtView1="+txtView1);

Categories

Resources