We are currently working on an android app using a local SQLite database. First connection with the database to check the login was working fine, however the moment we made a second connection to get the info from the logged in user (basicly store his username - I know not the most secure thing, but besides the point - and use a WHERE) the application crashes when we enter the screen's On Create.
We tried for several hours to fix this nearly commenting each and every part of the new code, but we still get the same error.
Error
06-08 14:13:48.159 3358-3358/com.example.wilmar.rentacube E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.wilmar.rentacube, PID: 3358
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wilmar.rentacube/com.example.wilmar.rentacube.Profile.Profile}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.wilmar.rentacube.DAO.UserDao.getUserForSession()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2760)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5944)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
To get Data out of the database:
public void getUserForSession(){
if (!database.isOpen())
open();
profile = new Profile();
//This line bellow seems to cause the issues, why we do not know
database = dbhelper.getReadableDatabase();
String query = "SELECT USERNAME, NAME, EMAIL, PHONENUMBER, BUSINESS, BIO, BRANCHE FROM " + DBOpenHelper.getUserTable() + " WHERE USERNAME EQUALS " + profile.logedinUser;
Cursor cursor = database.rawQuery(query, null);
String username, name, email, phonenumber, business, bio, branche;
// username = "not found";
// email = "not found";
// phonenumber = "not found";
// business = "not found";
// bio = "not found";
// branche = "not found";
username = cursor.getString(0);
name = cursor.getString(1);
email =cursor.getString(2);
phonenumber = cursor.getString(3);
business = cursor.getString(4);
bio = cursor.getString(5);
branche = cursor.getString(6);
if (database.isOpen())
close();
}
The class where we do the profile (the place that crashes when we enter it)
public class Profile extends Activity {
ImageView contactImageImgView;
MainActivity contacten = new MainActivity();
public UserDao userDao;
public String logedinUser;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
logedinUser = Login.userSession.getUsername();
userDao.getUserForSession();
Button editProfileButton = (Button) findViewById(R.id.BeditProfile);
Button addContactButton = (Button) findViewById(R.id.AdContactButton);
//Text ChangeProfilePictureText = (Text) findViewById(R.id.ChangeProfilePicture);
String Name = getIntent().getStringExtra("Name");
String eMail = getIntent().getStringExtra("Mail");
String Mobile = getIntent().getStringExtra("Mobile");
String Adress = getIntent().getStringExtra("Adress");
String Bio = getIntent().getStringExtra("Bio");
contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);
TextView tv_Name = (TextView) findViewById(R.id.Name);
TextView tv_Mail = (TextView) findViewById(R.id.Email);
TextView tv_Mobile = (TextView) findViewById(R.id.Mobile);
TextView tv_Adress = (TextView) findViewById(R.id.Adress);
TextView tv_Bio = (TextView) findViewById(R.id.Bio);
tv_Name.setText(Name);
tv_Mail.setText(eMail);
tv_Mobile.setText(Mobile);
tv_Adress.setText(Adress);
tv_Bio.setText(Bio);
if(Cube.fromUnit){
editProfileButton.setVisibility(View.GONE);
//ChangeProfilePictureText.replaceWholeText("");
tv_Name.setText(Cube.Name);
tv_Mail.setText(Cube.eMail);
tv_Mobile.setText(Cube.Mobile);
tv_Adress.setText(Cube.Adress);
tv_Bio.setText(Cube.Bio);
}
contactImageImgView.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){ // error @ View v, cannot resolve symbol v , expected ;
Intent intent = new Intent();
intent.setType("image*/");
intent.setAction(intent.ACTION_GET_CONTENT);
startActivityForResult(intent.createChooser(intent, "Select Profile Image"), 1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_maps, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menu_home) {
Intent i = new Intent(this, HomeScreen.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.BeditProfile) {
Intent i = new Intent(Profile.this, editProfile.class);
startActivity(i);
}
}
public void onActivityResult(int reqCode, int resCode, Intent data) {
if(resCode == RESULT_OK){
if(reqCode == 1)
contactImageImgView.setImageURI(data.getData());
}
}
}
Any help solving this would be greatly appreciated. Thank you in advance.
Aucun commentaire:
Enregistrer un commentaire