My app is always crashing because of this error. It says:
com.rengwuxian.materialedittext.MaterialEditText cannot be cast to android.view.ViewGroup.
I'm a beginner who's looking for some code on YouTube, but I can't figure it out.
<ImageView
android:id="#+id/imageView2"
android:layout_width="243dp"
android:layout_height="166dp"
android:layout_marginStart="8dp"
android:layout_marginTop="60dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/textView"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_marginStart="8dp"
android:layout_marginTop="44dp"
android:layout_marginEnd="8dp"
android:text="USERNAME"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.259"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="PASSWORD"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.255"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/usernameTIL"
app:layout_constraintVertical_bias="0.018" />
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/passwordTIL2"
android:layout_width="253dp"
android:layout_height="47dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.535"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<CheckBox
android:id="#+id/rememberCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Remember Me?"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/passwordTIL2" />
<Button
android:id="#+id/signInBTN"
android:layout_width="145dp"
android:layout_height="49dp"
android:layout_marginTop="28dp"
android:background="#color/signInBTN"
android:text="SIGN IN"
android:textColor="#android:color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rememberCB" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Dont't have an Account?"
android:textColor="#android:color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.323"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signInBTN" />
My Java:
public class sign_in extends AppCompatActivity {
EditText usernameTIL, passwordTIL;
Button signInBTN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
usernameTIL = (MaterialEditText) findViewById(R.id.usernameTIL);
passwordTIL = (MaterialEditText) findViewById(R.id.passwordTIL2);
signInBTN = (Button) findViewById(R.id.signInBTN);
//Init FireBase
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference table_user = database.getReference("User");
signInBTN.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final ProgressDialog mDialog = new Progress codeDialog(sign_in.this);
mDialog.setMessage("Please wait...");
mDialog.show();
table_user.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
//check if user doesn't have an account
if (dataSnapshot.child(usernameTIL.getText().toString()).exists()) {
//get user information
mDialog.dismiss();
User user = dataSnapshot.child(usernameTIL.getText().toString()).getValue(User.class);
if (user.getPassword().equals(passwordTIL.getText().toString())) {
Intent homeIntent = new Intent(sign_in.this, HomeScreen.class);
Common.curentUser = user;
startActivity(homeIntent);
finish();
} else {
Toast.makeText(sign_in.this, "Sign In Failed !!", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(sign_in.this, "User not found !!", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
});
}
}
MaterialEditText is an EditText
public class MaterialEditText extends AppCompatEditText {
it can not contain any widgets. In your layout you have
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/usernameTIL"
android:layout_width="251dp"
android:layout_height="46dp"
android:layout_marginStart="8dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.527"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<com.rengwuxian.materialedittext.MaterialEditText
android:layout_width="268dp"
android:layout_height="36dp"
android:hint="username" />
</com.rengwuxian.materialedittext.MaterialEditText>
a MaterialEditText containing another MaterialEditText, which is causing your app to crash for ClassCastException
According to this library's sample code, you must cast your MaterialEditText to EditTextlike this:
usernameTIL = (EditText) findViewById(R.id.usernameTIL);
passwordTIL = (EditText) findViewById(R.id.passwordTIL2);
Related
I have tried checking if any layout is overlapping or not. I have tried adding more space between the button and the border nothing seems to work. I'm not able to see where am I missing out
This is the activity where I'm trying to add setonclicklistenr:
public class addPost extends AppCompatActivity {
ImageView picgal,postt;
EditText descrip,compName;
Button pst,logout;
FirebaseAuth auth;
FirebaseStorage storage;
FirebaseDatabase database;
Uri uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_post);
picgal = findViewById(R.id.addpic);
descrip = findViewById(R.id.postDescription);
logout = findViewById(R.id.lgout);
compName = findViewById(R.id.comm);
auth = FirebaseAuth.getInstance();
storage = FirebaseStorage.getInstance();
logout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
auth.signOut();
startActivity(new Intent(addPost.this,postActivity.class));
}
});
This is the xml file:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/lgout"
android:layout_width="75dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:background="#drawable/post_button_blue"
android:enabled="false"
android:text="LOGOUT"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="#+id/postButton"
app:layout_constraintEnd_toStartOf="#+id/postButton"
app:layout_constraintTop_toTopOf="#+id/postButton"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/roboto_black"
android:text="Create Post"
android:textColor="#color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="#+id/postButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/postButton" />
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/postButton"
android:layout_width="75dp"
android:layout_height="40dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/post_button_white"
android:enabled="false"
android:text="post"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck" />
<View
android:id="#+id/view"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginTop="8dp"
android:background="#CFC9C9"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/postButton" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="User1"
android:textColor="#color/black"
android:textSize="#dimen/_14font_mdp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view" />
<EditText
android:id="#+id/comm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
android:ems="10"
android:hint="Enter Company Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/name"
app:layout_constraintTop_toBottomOf="#+id/name" />
<EditText
android:id="#+id/postDescription"
android:layout_width="355dp"
android:layout_height="287dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
android:ems="10"
android:gravity="top"
android:hint="Type to add your experience"
android:inputType="textMultiLine"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="#+id/name"
app:layout_constraintTop_toBottomOf="#+id/comm"
tools:ignore="TouchTargetSizeCheck" />
<ImageView
android:id="#+id/postpic"
android:layout_width="400dp"
android:layout_height="272dp"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:scaleType="centerCrop"
android:visibility="gone"
app:layout_constraintBottom_toTopOf="#+id/view3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/postDescription"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/postDescription"
app:srcCompat="#drawable/img2"
tools:visibility="invisible" />
<ImageView
android:id="#+id/addpic"
android:layout_width="41dp"
android:layout_height="43dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/imgg" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:text="Add to post"
android:textColor="#color/black"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/addpic"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/addpic" />
<View
android:id="#+id/view3"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:background="#CFC9C9"
app:layout_constraintBottom_toTopOf="#+id/addpic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Screenshot of the .XML file design
I can See You are setting "enabled=false" in logout Button could you remove that part or replace with "enabled=true"...I will work if you do so ..please try
So, I've been working on a social media.
When from the "profile viewer" the user wants to edit his profile I want to start another activity, but it's like that the button can't find the function that starts the activity.
Sorry for my poor English.
I hope you understood
Here is the code:
public class ProfileFragment extends Fragment {
private FirebaseUser user;
private DatabaseReference reference;
private String userid;
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull #NotNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
user = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users");
userid = user.getUid();
reference.child(userid).addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull #NotNull DataSnapshot snapshot) {
User userProfile = snapshot.getValue(User.class);
if (userProfile != null) {
String username = userProfile.username;
String description = userProfile.desc;
Integer followers = userProfile.followers;
Integer following = userProfile.following;
String displayname = userProfile.displayname;
Boolean verified = userProfile.verified;
TextView usernametxt = getActivity().findViewById(R.id.textView17);
TextView descriptiontxt = getActivity().findViewById(R.id.textView19);
TextView followerstxt = getActivity().findViewById(R.id.textView22);
TextView followingtxt = getActivity().findViewById(R.id.textView26);
TextView displaynametxt = getActivity().findViewById(R.id.textView28);
usernametxt.setText(username);
descriptiontxt.setText(description);
followerstxt.setText(followers.toString());
followingtxt.setText(following.toString());
displaynametxt.setText(displayname);
}
}
#Override
public void onCancelled(#NonNull #NotNull DatabaseError error) {
Toast toast = Toast.makeText(getContext(), "Failed to load profile", Toast.LENGTH_SHORT);
toast.show();
}
});
return inflater.inflate(R.layout.fragment_profile, container, false);
}
public void editprofile(View view) {
Intent pog = new Intent(getContext(), editprofile.class);
startActivity(pog);
}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Loading..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.885"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<TextView
android:id="#+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif-black"
android:text="Followers:"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.687"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.087" />
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif-black"
android:text="Bio:"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.157" />
<TextView
android:id="#+id/textView24"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif-black"
android:text="Following:"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.686"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.127" />
<TextView
android:id="#+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed-medium"
android:text="Your profile"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.023" />
<TextView
android:id="#+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Loading..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.337"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.099" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:fontFamily="sans-serif"
android:text="Loading..."
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.382"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.053" />
<TextView
android:id="#+id/textView26"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Loading..."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.883"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.127" />
<TextView
android:id="#+id/textView19"
android:layout_width="206dp"
android:layout_height="109dp"
android:layout_marginTop="28dp"
android:hint="no bio yet"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.213" />
<ImageView
android:id="#+id/imageView15"
android:layout_width="92dp"
android:layout_height="85dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.069"
app:srcCompat="#drawable/ic_baseline_account_circle_24" />
<Button
android:id="#+id/button"
android:layout_width="161dp"
android:layout_height="47dp"
android:background="#02f760"
android:text="Edit profile"
android:onClick="editprofile"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.396" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is the error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nordicalhavera.kitchub, PID: 10525
java.lang.IllegalStateException: Could not find method editprofile(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'button'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:447)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:405)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
I have two EditText in MyActivity. Here I have provided UI XML.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button_insta"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editUrli"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="177dp"
android:layout_height="39dp"
android:layout_marginEnd="81dp"
android:layout_marginRight="81dp"
android:text="Paste Instagram video link to download "
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="78dp"
android:layout_height="43dp"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:src="#drawable/insta"
app:layout_constraintEnd_toStartOf="#+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editUrl"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_download"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/fblogo"
android:layout_width="95dp"
android:layout_height="39dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="13dp"
android:contentDescription="TODO"
android:src="#drawable/fb"
app:layout_constraintBottom_toTopOf="#+id/editUrl"
app:layout_constraintEnd_toStartOf="#+id/textView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="84dp"
android:text="Paste FB video link to download"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fblogo"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
In my Activity java code has this button click code
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
inputURl = findViewById(R.id.editUrl);
editUrlinsta = findViewById(R.id.editUrli);
BtnDownload = (Button) findViewById(R.id.button_download);
downloadinsta = (Button) findViewById(R.id.button_insta);
BtnDownload.setOnClickListener(this);
downloadinsta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_download:
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
break;
case R.id.button_insta:
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
break;
default:
break;
}
}
The problem is The first button which I have named button_download is working as expected. But the second button button_insta not working as expected. what I want to do, is When one of the buttons clicks, getting data from EditText. The button_download works fine. the button_insta is not working fine.
I couldn't get value from editUrli when the button click. But same code works for button_download and editUrl
Try to separate those button click function like so :
BtnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
}
});
downloadinsta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
}
});
I've made a checklist app and when the user add's the check to the app I get the below error and a crash. Can someone provide some advice to how to fix this? As i'm using fragments, i've created a second Java file and refernced that to the XML as you cannot have more then one class in a single Java, i've tried merging them but get lots of errors and cannot use the public class 'extends' more then once.
Here's the error
Heres the XML
<TextView
android:id="#+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="#font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="#string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="#+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="#string/buttonText" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
Heres the 1st Java (Fragment)
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Heres the 2nd Java (CheckList)
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}
}
I have been pulling my hair out for the last 4 hours trying to find out how to do this.
I have checked SO for other questions but found nothing good.
I am an intermediate programmer and would be very appreciative if your comments and answers were written in a way that I can understand.
I want to be able to take the text that the user enters into the #+id/location Edittext box on the InputPage activity and display it in the #+id/ListName TextView on the InputPage2 activity when the button on the InputPage activity is pressed.
InputPage java code
public class InputPage extends Activity {
public String name;
public InputPage() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_page );
Button button = (Button)findViewById(R.id.addButton);
}
public InputPage(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String name(String name){
this.name = name;
EditText editText = (EditText)findViewById(R.id.location);
String value = editText.getText().toString();
return value;
}
public void toInputScreen2(View view) {
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent2 = new Intent(InputPage.this, InputPage2.class);
startActivity(intent2);
}
});
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".InputPage"
android:background="#color/BackgroundGrey">
<TextView
android:id="#+id/mapNo"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:text="#string/mapNoTextBox"
android:textColor="#color/textColour"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="16dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.04"
app:layout_constraintVertical_bias="0.13" />
<TextView
android:id="#+id/inputPageTitle"
android:layout_width="178dp"
android:layout_height="21dp"
android:text="#string/inputPageTitle"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/textColour"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.01999998" />
<EditText
android:id="#+id/noInput"
android:layout_width="48dp"
android:layout_height="39dp"
android:ems="10"
android:inputType="number"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.13"
app:layout_constraintHorizontal_bias="0.51" />
<TextView
android:layout_width="150dp"
android:layout_height="38dp"
android:text="#string/location"
android:textColor="#color/textColour"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="0.041"
app:layout_constraintVertical_bias="0.24" />
<EditText
android:id="#+id/location"
android:layout_width="177dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/hintText"
android:textColorHint="#color/hintTextColour"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.952"
app:layout_constraintVertical_bias="0.243" />
<EditText
android:id="#+id/dateBox"
android:layout_width="133dp"
android:layout_height="44dp"
android:ems="10"
android:inputType="date"
android:hint="#string/dateBoxHint"
android:textColorHint="#color/hintTextColour"
android:layout_marginLeft="61dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.335"
app:layout_constraintVertical_bias="0.374" />
<TextView
android:layout_width="101dp"
android:layout_height="34dp"
android:text="#string/dateBox"
android:textColor="#color/textColour"
android:textStyle="bold"
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.032"
app:layout_constraintVertical_bias="0.369" />
<Button
android:id="#+id/button2"
android:layout_width="165dp"
android:layout_height="58dp"
android:text="#string/continueButton"
android:background="#drawable/buttons"
android:layout_marginRight="8dp"
android:onClick="toInputScreen2"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintVertical_bias="0.778" />
</android.support.constraint.ConstraintLayout>
**InputPage2 java code **
public class InputPage2 extends Activity {
Button saveButton1;
EditText message;
String Message;
TextView[] pairs;
int num_match;
public TextView[] getPairs() {
return pairs;
}
public void setPairs(TextView[] pairs) {
this.pairs = pairs;
}
public int getNum_match() {
return num_match;
}
public void setNum_match(int num_match) {
this.num_match = num_match;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_page2);
message = (EditText) findViewById(R.id.textNotAtHomes);
}
public void toSaveField(View v) {
saveButton1 = (Button) findViewById(R.id.saveButton1);
saveButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Message = message.getText().toString();
try {
FileOutputStream fos = openFileOutput("Text.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
try {
osw.write(Message);
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Saved successfully", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Intent intent2 = new Intent(InputPage2.this, MainActivity.class);
startActivity(intent2);
}
public void linearLayout(TextView[] pairs, int num_match) {
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pairs = new TextView[num_match + 1];
for (int l = 1; l <= num_match; l++) {
pairs[1].setTextSize(15);
pairs[1].setLayoutParams(layoutParams);
pairs[1].setId(l);
linearLayout.addView(pairs[1]);
}
}
}
associated XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".InputPage2"
android:background="#ffffff"
tools:layout_editor_absoluteY="25dp"
tools:layout_editor_absoluteX="0dp"
android:backgroundTint="#color/BackgroundGrey">
<Button
android:onClick="toSaveField"
android:id="#+id/saveButton1"
android:layout_width="341dp"
android:layout_height="70dp"
android:background="#drawable/buttons"
android:text="#string/saveButton"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintVertical_bias="1.0"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:id="#+id/textNotAtHomes"
android:layout_width="346dp"
android:layout_height="477dp"
android:ems="10"
android:background="#color/hintTextColour"
android:inputType="textMultiLine"
android:hint="#string/hintText2"
android:textColorHint="#color/hintTextColour2"
android:layout_marginLeft="8dp"
android:gravity="fill_horizontal"
android:padding="6dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintVertical_bias="0.303" />
<TextView
android:id="#+id/ListName"
android:layout_width="344dp"
android:layout_height="37dp"
android:textColor="#color/blackText"
android:background="#color/whiteBackground"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintVertical_bias="0.0" />
</android.support.constraint.ConstraintLayout>
Any help is appreciated .
You can pass data between activities like this
Intent intent = new Intent(activity2.this, activity1.class);
intent.putExtra("message", message);
startActivity(intent);
and get it with the other activity
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");