Android app keeps 'closing', but no errors being shown in logcat -
this main class forwarding username goalactivity class. cannot figure out issue is. keeps crashing unknown reason me. i've followed various tutorials, , cannot figure out issue. seems retrieve username correctly, , convert string. create intent , pass username value key.
mainactivity.java
import android.app.activity; import android.app.dialog; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.toast; import com.example.agray.carpediem.logindatabaseadapter; import com.example.agray.carpediem.r; import com.example.agray.carpediem.signupactivity; public class mainactivity extends activity { button btnsignin,btnsignup; logindatabaseadapter logindatabaseadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //create instance of sqlite database logindatabaseadapter=new logindatabaseadapter(this); logindatabaseadapter=logindatabaseadapter.open(); //create reference buttons used btnsignin=(button)findviewbyid(r.id.buttonsignin); btnsignup=(button)findviewbyid(r.id.buttonsignup); // signup button w/onclick listener btnsignup.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { // todo auto-generated method stub /// create intent signupactivity intent intentsignup=new intent(getapplicationcontext(),signupactivity.class); //start activity w/intent startactivity(intentsignup); } }); } // methods handleclick event of sign in button public void signin(view view) { final dialog dialog = new dialog(mainactivity.this); dialog.setcontentview(r.layout.login); dialog.settitle("login"); //get references of views final edittext edittextusername= (edittext)dialog.findviewbyid(r.id.edittextusernametologin); final edittext edittextpassword= (edittext)dialog.findviewbyid(r.id.edittextpasswordtologin); button btnsignin=(button)dialog.findviewbyid(r.id.buttonsignin); //signin button w/ onclicklistener btnsignin.setonclicklistener(new view.onclicklistener() { public void onclick(view view) { //store username , password strings string username=edittextusername.gettext().tostring(); string password=edittextpassword.gettext().tostring(); //fetch password db respective username string storedpassword=logindatabaseadapter.getsingleentry(username); // check if stored password matches password entered user if(password.equals(storedpassword)) { toast.maketext(mainactivity.this, "congrats: login successful " + username, toast.length_long).show(); dialog.dismiss(); // final edittext edittextusername= // (edittext)dialog.findviewbyid(r.id.edittextusernametologin); // string username=edittextusername.gettext().tostring(); //create intent start goals activity w/some data intent intro = new intent(getapplicationcontext(), goalactivity.class); //put username intent intro.putextra("user_name", username); startactivity(intro); } else { toast.maketext(mainactivity.this, "access denied: user name or password " + "does not match", toast.length_long).show(); } } }); dialog.show(); } @override protected void ondestroy() { super.ondestroy(); // close database logindatabaseadapter.close(); } }
here goalactivity class receives info mainactivity class.
goalactivity.java
import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.textview; public class goalactivity extends activity{ @override protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.goals_page); //get username intent string enteredusername = getintent().getstringextra("user_name"); final textview tv = (textview)findviewbyid(r.id.user_name_forwarded); tv.settext(enteredusername); } }
login.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" > <edittext android:id="@+id/edittextusernametologin" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="user name" android:ems="10" > <requestfocus /> </edittext> <edittext android:id="@+id/edittextpasswordtologin" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:inputtype="textpassword" android:hint="password" /> <button android:id="@+id/buttonsignin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="sign in" /> </linearlayout>
goals_page.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" android:weightsum="1"> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/welcome_goals" android:textsize="50sp"/> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/user_name_forwarded" android:text="@string/emptystring" android:layout_weight="0.09"/> </linearlayout>
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.agray.carpediem" > <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="carped" android:theme="@style/apptheme" > <activity android:name="com.example.agray.carpediem.mainactivity" android:label="carped" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".signupactivity"/> <activity android:name=".goalactivity"/> </application> </manifest>
try this, e.g. in activitya
:
intent = new intent(activitya.this, activityb.class); i.putextra("user_name", usernamestring); startactivity(i);
in activityb
:
bundle extras = getintent().getextras(); if (extras == null) { return; } string username = extras.getstring("user_name");