android.support.v7.widget.AppCompatEditText Error - java

Error:
android.support.v7.widget.AppCompatEditText{8701551bVFED..CL.
.......484,0-747,118 #7f0b006c app:id/name_text
I am trying to set user's name information to FirebaseUser "DisplayName" during the registration. I am new to android, java and firebase. But couldn't find any solution despite my all researchs. id/name_text is the edit text which takes name from the user in registation. I thought it's a string issue, but couldn't fix.
Help. Thanks.
Here are the codes.
public class RegisterActivity extends AppCompatActivity {
#BindView(R.id.register_button) Button mRegisterButton;
#BindView(R.id.surname_edittext) EditText mSurnameEditText;
#BindView(R.id.name_text) EditText mNameEditText;
#BindView(R.id.password_edittext) EditText mPasswordEditText;
#BindView(R.id.mail_edittext) EditText mMailEditText;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ButterKnife.bind(this);
mAuth = FirebaseAuth.getInstance();
}
#OnClick(R.id.register_button)
public void register_button(View view){
RegisterUser();
}
private void RegisterUser() {
String surname = mSurnameEditText.getText().toString().trim();
String name = mNameEditText.getText().toString().trim();
String password = mPasswordEditText.getText().toString().trim();
String mail = mMailEditText.getText().toString().trim();
if (TextUtils.isEmpty(name)) {
Toast.makeText(this, "enter the name", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(surname)) {
Toast.makeText(this, "enter the surname", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(this, "enter the password", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(mail)) {
Toast.makeText(this, "enter the mail", Toast.LENGTH_SHORT).show();
return;
}
mAuth.createUserWithEmailAndPassword(mail,password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(RegisterActivity.this,"Successfully",Toast.LENGTH_SHORT).show();
savetheinfo();
}
else{
Toast.makeText(RegisterActivity.this,"Could not registered , please try again...",Toast.LENGTH_SHORT).show();
}
}
});
}
private void savetheinfo() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(String.valueOf(mNameEditText)).build();
user.updateProfile(profileUpdates);
}
}
public class ProfileActivity extends AppCompatActivity {
#BindView(R.id.user_name) TextView mUserName;
private FirebaseAuth mAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ButterKnife.bind(this);
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
setDataToView(user);
}
private void setDataToView(FirebaseUser user) {
mUserName.setText(user.getDisplayName());
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="24dp"
android:text="REGISTER"
android:layout_marginBottom="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: "/>
<EditText
android:id="#+id/name_text"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname: "/>
<EditText
android:id="#+id/surname_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mail: "/>
<EditText
android:id="#+id/mail_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password: "/>
<EditText
android:id="#+id/password_edittext"
android:layout_width="100dp"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="#+id/register_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_marginTop="50dp"/>
</LinearLayout>

String.valueOf(mNameEditText) is equivalent to mNameEditText.toString() if the view is not null. You're probably looking for mNameEditText.getText().toString() which gives you the content of the EditText element.

I was able to resolve the issue as follows:
I simply used mNameEditText.getText().toString(). I forgot the .getText() in between.

Related

Messages are not showing, Firebase Chat

After message sending it isn't showing. But I'm receiving the message in Firebase Database. What's wrong with it? Have you any suggestions?
MainActivity.class
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
FirebaseUser currentUser;
private FirebaseListAdapter<ChatMessage> adapter;
private final static String TAG = "LOG";
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
checkAuth();
input = (EditText)findViewById(R.id.input);
FloatingActionButton fab =
(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (input.getText().toString().equals(null) && input.getText().toString().equals("")) {
Log.i(TAG, "Mesage is empty");
} else {
// Read the input field and push a new instance
// of ChatMessage to the Firebase database
FirebaseDatabase.getInstance()
.getReference()
.push()
.setValue(new ChatMessage(input.getText().toString(),
FirebaseAuth.getInstance()
.getCurrentUser()
.getDisplayName())
);
displayChatMessages();
// Clear the input
input.setText("");
}
}
});
}
private void displayChatMessages() {
ListView listOfMessages = (ListView) findViewById(R.id.list_of_messages);
//Suppose you want to retrieve "chats" in your Firebase DB:
Query query = FirebaseDatabase.getInstance().getReference();
FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
.setQuery(query, ChatMessage.class)
.setLayout(R.layout.message)
.build();
//Finally you pass them to the constructor here:
adapter = new FirebaseListAdapter<ChatMessage>(options){
#Override
protected void populateView(View v, ChatMessage model, int position) {
// Get references to the views of message.xml
TextView messageText = (TextView) v.findViewById(R.id.message_text);
TextView messageUser = (TextView) v.findViewById(R.id.message_user);
TextView messageTime = (TextView) v.findViewById(R.id.message_time);
// Set their text
messageText.setText(model.getMessageText());
messageUser.setText(model.getMessageUser());
Log.i(TAG, model.getMessageUser() + ": " + model.getMessageText());
// Format the date before showing it
messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
model.getMessageTime()));
}
};
listOfMessages.setAdapter(adapter);
}
public void checkAuth(){
// Check if user is signed in (non-null) and update UI accordingly.
currentUser = mAuth.getCurrentUser();
if (currentUser != null) {
displayChatMessages();
///
} else {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
}
#Override
public void onStart() {
super.onStart();
checkAuth();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_toStartOf="#id/fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/fab">
<EditText
android:id="#+id/input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:clickable="true"
android:tint="#android:color/white"
app:fabSize="mini"
app:layout_constraintStart_toStartOf="#+id/textInputLayout"
/>
<ListView
android:id="#+id/list_of_messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:divider="#android:color/transparent"
android:dividerHeight="12dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
message.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/message_user"
android:textStyle="normal|bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/message_user"
android:layout_alignParentEnd="true"
android:id="#+id/message_time" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/message_user"
android:layout_alignParentStart="true"
android:layout_marginTop="5dp"
android:id="#+id/message_text"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textSize="18sp" />
By the way, it's not a good solution to obey type more text.
It looks like your post is mostly code; please add some more details.

java.lang.NullPointerException in Firebase Realtime Database fetching activity

I have beenn struggling to find out why I am getting the error: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
Below is the full description of the error:
2021-03-15 14:15:49.906 24447-24447/com.goldencrestit.quicky2 D/AndroidRuntime: Shutting down VM
2021-03-15 14:15:49.907 24447-24447/com.goldencrestit.quicky2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.goldencrestit.quicky2, PID: 24447
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.goldencrestit.quicky2.ViewProfileActivity$1.onDataChange(ViewProfileActivity.java:64)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:264)
at android.app.ActivityThread.main(ActivityThread.java:7581)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:980)
This error pops up when I click on the "View your Profile" button in order to load the profile page.
Below is the expected result. It is support to fetch data from my firebase realtime database to fill the dummy texts:
Check below my codes:
activity_company_portal.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#color/bg_color"
android:orientation="vertical"
tools:context=".CompanyPortal">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar_home" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:padding="20dp"
android:orientation="horizontal">
<ImageButton
android:id="#+id/logout_btn"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_baseline_power_settings_new_24" />
</LinearLayout>
<ImageView
android:id="#+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
app:srcCompat="#drawable/logo" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<Button
android:id="#+id/view_job"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/button_bg"
android:layout_marginTop="20dp"
android:textColor="#color/text_color_secondary"
android:textSize="16sp"
android:letterSpacing=".2"
android:text="#string/view_all_jobs" />
<Button
android:id="#+id/add_job"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/button_bg"
android:layout_marginTop="50dp"
android:textColor="#color/text_color_secondary"
android:layout_below="#id/view_job"
android:textSize="16sp"
android:letterSpacing=".2"
android:text="#string/add_a_new_job" />
<Button
android:id="#+id/edit_profile"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/button_bg"
android:layout_marginTop="50dp"
android:textColor="#color/text_color_secondary"
android:layout_below="#id/add_job"
android:textSize="16sp"
android:letterSpacing=".2"
android:text="#string/edit_your_profile" />
<Button
android:id="#+id/view_profile"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/button_bg"
android:layout_marginTop="50dp"
android:textColor="#color/text_color_secondary"
android:layout_below="#id/edit_profile"
android:textSize="16sp"
android:letterSpacing=".2"
android:text="#string/view_your_profile" />
</RelativeLayout>
</LinearLayout>
CompanyPortalActivity.java
public class CompanyPortalActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private FirebaseUser mCurrentUser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_company_portal);
Toolbar toolbar = findViewById(R.id.toolbar_home);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Quicky Job Portal");
Button viewJobs = findViewById(R.id.view_job);
Button postJobs = findViewById(R.id.add_job);
Button edit_profile = findViewById(R.id.edit_profile);
Button view_profile = findViewById(R.id.view_profile);
viewJobs.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), IndividualPostActivity.class)));
postJobs.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), PostJobsActivity.class)));
edit_profile.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), EditProfileActivity.class)));
view_profile.setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), ViewProfileActivity.class)));
mAuth = FirebaseAuth.getInstance();
mCurrentUser = mAuth.getCurrentUser();
ImageButton mLogoutBtn = findViewById(R.id.logout_btn);
mLogoutBtn.setOnClickListener(v -> {
mAuth.signOut();
sendUserToLogin();
});
}
#Override
protected void onStart() {
super.onStart();
if(mCurrentUser == null){
sendUserToLogin();
}
}
private void sendUserToLogin() {
Intent loginIntent = new Intent(getApplicationContext(), CompanyLoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
finish();
}
}
activity_view_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
android:background="#color/bg_color"
tools:context=".ViewProfileActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"/>
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="50dp"
app:srcCompat="#drawable/logo" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_color_primary"
android:layout_marginTop="10dp"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:text="Company Name" />
<TextView
android:id="#+id/company_name"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:layout_marginStart="40dp"
android:textColor="#color/text_color_secondary"
android:text="Golden Crest Tech"
android:background="#drawable/text_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_color_primary"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:text="Business Type" />
<TextView
android:id="#+id/business_type"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:layout_marginStart="40dp"
android:textColor="#color/text_color_secondary"
android:text="Technology"
android:background="#drawable/text_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_color_primary"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:text="Phone Number" />
<TextView
android:id="#+id/phone_number"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:layout_marginStart="40dp"
android:textColor="#color/text_color_secondary"
android:text="08020345678"
android:background="#drawable/text_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_color_primary"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:text="Company Address" />
<TextView
android:id="#+id/address"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:layout_marginStart="40dp"
android:textColor="#color/text_color_secondary"
android:text="Surulere, Lagos"
android:background="#drawable/text_display" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/text_color_primary"
android:layout_marginEnd="40dp"
android:layout_marginStart="40dp"
android:layout_marginBottom="5dp"
android:text="Company Detail" />
<EditText
android:id="#+id/editTextTextMultiLine"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:background="#drawable/text_display_2"
android:clickable="false"
android:editable="false"
android:ems="10"
android:enabled="false"
android:fontFamily="#font/open_sans"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/company_detail_dummy"
android:textColor="#color/text_color_secondary"
android:textSize="14sp" />
<Button
android:id="#+id/edit_profile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/button_bg"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:textColor="#color/text_color_secondary"
android:textAllCaps="false"
android:textSize="16sp"
android:text="Edit Profile" />
</LinearLayout>
</ScrollView>
ViewProfileActivity.java
public class ViewProfileActivity extends AppCompatActivity {
private DatabaseReference myRef;
TextView company_name_txt, business_type_txt, phone_number_txt, company_address_txt;
EditText company_detail_txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
company_name_txt = findViewById(R.id.company_name);
business_type_txt = findViewById(R.id.business_type);
phone_number_txt = findViewById(R.id.phone_number);
company_address_txt = findViewById(R.id.address);
company_detail_txt = findViewById(R.id.company_detail);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Objects.requireNonNull(getSupportActionBar()).setTitle("Company Profile");
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
Objects.requireNonNull(getSupportActionBar()).setDisplayShowHomeEnabled(true);
myRef = FirebaseDatabase.getInstance().getReference().child("Company Profile");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
String companyName = snapshot.child("company_name").getValue().toString();
String phoneNumber = snapshot.child("phone_number").getValue().toString();
String companyAddress= snapshot.child("address").getValue().toString();
String businessType = snapshot.child("business_type").getValue().toString();
company_name_txt.setText(companyName);
business_type_txt.setText(businessType);
company_address_txt.setText(companyAddress);
phone_number_txt.setText(phoneNumber);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {}
});
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (item.getItemId()==android.R.id.home){
finish();
}
return super.onOptionsItemSelected(item);
}
}
Below is what I am trying to display on the Job Detail Layout:
The error seems to come from this piece of code:
myRef = FirebaseDatabase.getInstance().getReference().child("Company Profile");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
String companyName = snapshot.child("company_name").getValue().toString();
String phoneNumber = snapshot.child("phone_number").getValue().toString();
String companyAddress= snapshot.child("address").getValue().toString();
String businessType = snapshot.child("business_type").getValue().toString();
company_name_txt.setText(companyName);
business_type_txt.setText(businessType);
company_address_txt.setText(companyAddress);
phone_number_txt.setText(phoneNumber);
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {}
});
The above code is loading all data from /Company Profile and then trying to read the company_name property from it. But if we look at the data you shared, there is no /Company Profile/company_name property, so that explains why getValue() returns null and the toString() then leads to the exception you get.
It seems your JSON tree consists of: /Company Profile/$uid/$pushid. How to property load the data from this structure depends on how much information you already know in your code, so let's look at the options:
If you know both the UID and the push ID of the information you want to load, you can do load just that with:
myRef = FirebaseDatabase.getInstance().getReference().child("Company Profile");
String uid = "6babpG...MxLg33"; // must be the exact, complete value
String pushid = "-MVmyqx...3rwsAO"; // must be the exact, complete value
myRef.child(uid).child(pushid().addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
String companyName = snapshot.child("company_name").getValue().toString();
...
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
If you know the UID, but not the push ID, of the information you want to load, you can load all push IDs and loop over them with:
myRef = FirebaseDatabase.getInstance().getReference().child("Company Profile");
String uid = "6babpG...MxLg33"; // must be the exact, complete value
myRef.child(uid).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
String companyName = snapshot.child("company_name").getValue().toString();
...
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
So here we loop over dataSnapshot.getChildren() to get across the unknown push IDs from the database. This means we can have multiple child nodes, so you may want to consider using a list view/recycler view for displaying the data.
The final case is if you know neither the UID nor the push ID of the data you want to display. In that case you need two nested loops to navigate over the data, one for the UIDs in the JSON, and one for the push IDs. So:
myRef = FirebaseDatabase.getInstance().getReference().child("Company Profile");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
for (DataSnapshot snapshot: userSnapshot.getChildren()) {
String companyName = snapshot.child("company_name").getValue().toString();
...
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
throw error.toException(); // never ignore errors
}
});
You'll note that the solution is quite similar to the previous one, but now with a second loop. Here too, you'll have to consider what is the best UI element to display the structure, as now you're navigating/parsing an entire tree-like structure from the database.

My Firebase SigninWithEmailandPassword did not work

my code does not read any statement eventhough i have enter correct email and password.
the firebase connection already define and the email and password already in the database.
this is my code for activity_login
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nani.mrt.Login">
<ImageView
android:id="#+id/icon1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#drawable/ic_navlogo"
android:layout_width="100dp"
android:layout_height="100dp" />
<android.support.design.widget.TextInputLayout
android:layout_below="#+id/icon1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_input_email"
>
<EditText
android:id = "#+id/login_email"
android:hint="Enter your email"
android:inputType="textCapWords"
android:maxLines="1"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_below="#+id/login_input_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/login_input_password"
>
<EditText
android:id = "#+id/login_password"
android:hint="Enter your password"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:inputType = "textPassword"
android:maxLines="1"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:text="SIGN IN"
android:background="#263238"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="login"
android:id="#+id/btnsignin"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_below="#+id/btnsignin"
android:text="FORGOT PASSWORD?"
android:clickable="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
style="#style/Widget.AppCompat.Button.Borderless"
android:textColor="#color/colorPrimaryDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnforgot"
android:onClick="forgotpassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnforgot"
android:id="#+id/login_layout_or"
android:gravity="center"
android:orientation="horizontal">
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:layout_margin="5dp"
android:background="#c4c8c9" />
<TextView
android:padding="5dp"
android:text="OR"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="200dp"
android:layout_height="1dp"
android:layout_margin="5dp"
android:background="#c4c8c9" />
</LinearLayout>
<TextView
android:layout_below="#+id/login_layout_or"
android:text="Register an account" android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="register"
android:layout_centerHorizontal="true"
android:textStyle="bold"
style="#style/Widget.AppCompat.Button.Borderless"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/register"
/>
and this is my code for Login activity
public class Login extends AppCompatActivity implements View.OnClickListener {
private Button btnsignin;
private EditText editTextemail, editTextpassword;
private TextView register, btnforgot;
private ProgressDialog progressDialog;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener firebaseAuthListener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnsignin = (Button) findViewById(R.id.btnsignin);
btnforgot = (TextView) findViewById(R.id.btnforgot);
editTextemail = (EditText) findViewById(R.id.login_email);
editTextpassword = (EditText) findViewById(R.id.login_password);
register = (TextView) findViewById(R.id.register);
btnsignin.setOnClickListener(this);
btnforgot.setOnClickListener(this);
register.setOnClickListener(this);
mAuth = FirebaseAuth.getInstance();
}
#Override
public void onClick(View view) {
if (view == btnsignin) {
// signinUser(editTextemail.getText().toString(),editTextpassword.getText().toString());
String email = editTextemail.getText().toString().trim();
final String password = editTextpassword.getText().toString().trim();
if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)) {
Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
return;
}
//authenticate user
mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
// If sign in fails, display a message to the user. If sign in succeeds
// the auth state listener will be notified and logic to handle the
// signed in user can be handled in the listener.
if (!task.isSuccessful()) {
// there was an error
if (password.length() < 6) {
Toast.makeText(Login.this, "Try Again", Toast.LENGTH_LONG).show();
// editTextpassword.setError(getString(R.string.minimum_password));
}
} else {
Intent intent = new Intent(Login.this, HomeNavigation.class);
intent.putExtra("usernamekey", editTextemail.getText().toString());
startActivity(intent);
finish();
}
}
});
}
if (view == btnforgot) {
Intent intent = new Intent(Login.this, ForgotPassword.class);
startActivity(intent);
}
if (view == register) {
Intent intent = new Intent(Login.this, RegisterAs.class);
startActivity(intent);
}
}

app unforunatly stopped on data parsing on intent

I am getting a run time error "RemindMe is unfortunately stopped" on AddReminder activity when I passed a username from logging activity to Menu then attempt to add reminder activity, it unfortunately stops. Also logcat is not showing me anything (its empty).
Loging.XML
<?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.champ.remindme2.Login"
android:background="#drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#drawable/remind_me_logo"
android:contentDescription="Logo" />
<EditText
android:id="#+id/edtitem"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/username_rounded_edited_text"
android:inputType="text"
android:hint="Username"
android:textAlignment="center"
android:layout_marginTop="37dp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/password"
android:layout_width="265dp"
android:layout_height="wrap_content"
android:padding="10dip"
android:background="#drawable/pass_rounded_edited_text"
android:inputType="text"
android:hint="Password"
android:textAlignment="center"
android:layout_below="#+id/edtitem"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LoginButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
style="#style/ButtonText"
android:onClick="Login"
android:layout_below="#+id/password"
android:layout_alignLeft="#+id/password"
android:layout_alignStart="#+id/password"
android:layout_alignRight="#+id/password"
android:layout_alignEnd="#+id/password" />
<Button
android:id="#+id/txtSignup"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
style="#style/ButtonText"
android:onClick="Signup"
android:layout_below="#+id/LoginButton"
android:layout_alignLeft="#+id/LoginButton"
android:layout_alignStart="#+id/LoginButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView3"
android:src="#drawable/fb_loging"
android:layout_alignBottom="#+id/txtSignup"
android:layout_toRightOf="#+id/txtSignup"
android:layout_toEndOf="#+id/txtSignup" />
</RelativeLayout>
Loging.java
public class Login extends AppCompatActivity {
EditText ed1Username, ed2Pass;
String Username;
int counter=3;
Button LoginButton;
// TextView txtAttempts;
//private final int interval = 3000;
/*private Runnable runnable = new Runnable(){
public void run() {
LoginButton.setEnabled(true);
}
}; */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
LoginButton= (Button)findViewById(R.id.LoginButton);
ed1Username=(EditText)findViewById(R.id.edtitem);
Username=ed1Username.getText().toString();
ed2Pass=(EditText)findViewById(R.id.password);
}
public void Login(View v){
if (ed1Username.getText().toString().equals("Admin") && ed2Pass.getText().toString().equals("123")){
Toast.makeText(Login.this, "Login Successful", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, Menu.class);
intent.putExtra("Username",Username);
startActivity(intent);
}
else {
Toast.makeText(Login.this, "Login Failed", Toast.LENGTH_LONG).show();
counter--;
}
// txtAttempts.setText("Attempts Left: " + counter);
if (counter == 0) {
LoginButton.setEnabled(false);
// new Timer().schedule((TimerTask) runnable,interval);
}
}
public void Signup(View v) {
Intent intent = new Intent(this, signup.class);
startActivity(intent);
}
}
Menu.XML
<?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"
android:background="#drawable/back"
tools:context="com.example.champ.remindme2.Menu">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView4"
android:src="#drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Reminder"
android:id="#+id/button"
android:onClick="addReminder"
android:layout_below="#+id/imageView4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp"
android:layout_marginTop="49dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View List"
android:id="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_marginLeft="32dp"
android:layout_marginStart="32dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Shop"
android:id="#+id/button3"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/button"
android:layout_alignStart="#+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="View profile"
android:id="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_alignLeft="#+id/button2"
android:layout_alignStart="#+id/button2" />
</RelativeLayout>
Menu.java
public class Menu extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
startActivity(intent);
}
}
AddReminder.xml
<?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.champ.remindme2.AddReminder"
android:background="#drawable/back">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView6"
android:src="#drawable/remind_me_logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp" />
<EditText
android:id="#+id/edtitem"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:padding="5dp"
android:background="#drawable/rounded_edited_text"
android:inputType="text"
android:textAlignment="center"
android:layout_alignBottom="#+id/plusButton"
android:layout_alignLeft="#+id/imageView6"
android:text="Add Item"
android:layout_alignTop="#+id/plusButton" />
<Button
android:id="#+id/plusButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
style="#style/ButtonText"
android:onClick="plus"
android:layout_below="#+id/imageView6"
android:layout_alignParentRight="true"
android:layout_alignLeft="#+id/NextButton"
android:layout_alignStart="#+id/NextButton" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="47dp" />
<Button
android:id="#+id/NextButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
style="#style/ButtonText"
android:onClick="AddEventPlace"
android:layout_alignTop="#+id/BackButton"
android:layout_alignRight="#+id/imageView6" />
<Button
android:id="#+id/BackButton"
android:background="#drawable/blue_botton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
style="#style/ButtonText"
android:onClick="Back"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/edtitem"
android:layout_alignStart="#+id/edtitem" />
</RelativeLayout>
AddReminder.java
public class AddReminder extends AppCompatActivity {
String item;
//Passing Value Through String
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
}
public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
String Username = intent.getExtras().getString("Username");
intent.putExtra("Username", Username);
intent.putExtra("item",item);
startActivity(intent);
}
}
Check my updated code, Hope it will help...
1) Updated Menu Activity -
public class Menu extends AppCompatActivity {
String username;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
username = getIntent().getStringExtras("Username");
}
public void addReminder(View v) {
Intent intent = new Intent(this, AddReminder.class);
intent.putExtra("Username", username);
startActivity(intent);
}
}
2) Updated AddReminder Activity -
public class AddReminder extends AppCompatActivity {
String item;
String username;
//Passing Value Through String
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_reminder);
EditText edtItem= (EditText)findViewById(R.id.edtitem);
item=edtItem.getText().toString();
username = getIntent().getStringExtras("Username");
}
public void AddEventPlace(View v){
Intent intent = new Intent(this, AddEventPlace.class);
intent.putExtra("Username", username);
intent.putExtra("item",item);
startActivity(intent);
}
}

Why am I getting a NullPointerException when setting read permissions to Facebook login button?

I'm having a problem with integrating Facebook in my project.
I added the dependency to my build.gradle file and the imports and stuff are fine, but when I call the setReadPermissions method of the LoginButton class, I'm getting null pointer.
Here's the code I developed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);
// lang selection
lang = getIntent().getIntExtra("lang", 0);
AssetManager am = getApplicationContext().getAssets();
tf = Typeface.createFromAsset(am, "fonts/Biko_Regular.ttf");
lblNombreApp = (TextView) findViewById(R.id.lblTurismoMain);
lblNombreApp.setTypeface(tf);
btnLoginOptions = (Button) findViewById(R.id.btnOpenLoginOptions);
btnLoginOptions.setTypeface(tf);
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions("public_profile email");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
if (AccessToken.getCurrentAccessToken() != null)
// facebook
Constantes.tipoLogin = 2;
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
}
});
// espanol
if (lang == 1) {
lblNombreApp.setText(R.string.lblTurismo);
btnLoginOptions.setText(R.string.lblLoginPopUp);
}
// ingles
else if (lang == 2) {
lblNombreApp.setText(R.string.lblTurismoEN);
btnLoginOptions.setText(R.string.lblLoginPopUpEN);
}
if (!isNetworkConnected()) {
if (lang == 1)
Toast.makeText(getApplicationContext(),
R.string.err_no_connection, Toast.LENGTH_LONG).show();
else if (lang == 2)
Toast.makeText(getApplicationContext(),
R.string.err_no_connectionEN, Toast.LENGTH_LONG).show();
finish();
}
}
I have no idea of what's going on.. Thanks in advance.
EDIT
Here is the layout.. It's a pop up, that's being shown in the main activity.
<?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:id="#+id/llPopUp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
tools:context=".UI.MainActivity" >
<TextView
android:id="#+id/lblLoginPopUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/lblNoRegistradoPopUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="7dp"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/btnPopupExistente"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="15dp"
android:onClick="existingUser" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="15dp" />
<Button
android:id="#+id/btnNewPopUp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginTop="15dp"
android:onClick="newUser" />
</LinearLayout>
Thanks in advance.
If this is the troubling line:
loginButton = (LoginButton) findViewById(R.id.login_button);
make sure the id R.id.login_button exists in your R.layout.activity_main, e.g.:
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
Also when setting Read Permissions you'd probably want to pass a List<String> (see docs)

Categories