Invoking findViewById on a null object. Which isn't null - java

So I have a settings fragment which contains, well, settings.
On line 22 I have a findview which finds a textview. It worked wonders until today (Didn't change a thing). Now I imagine it will also fail the rest of the finds but since it crashes on this one, we will never know.
Why is that?
XML file:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="ACCOUNT"
android:textColor="#2b2a29"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/SettingsFragmentAccountSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Account Settings"
android:textColor="#2b2a29"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="THEME"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Dark Mode"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Switch
android:id="#+id/SettingsFragmentDarkModeSwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:scaleX="1.25"
android:scaleY="1.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.94"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentAccountSettings" />
<TextView
android:id="#+id/r"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="OTHER"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentDarkModeSwitch" />
<TextView
android:id="#+id/SettingsFragmentFAQ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="FAQ"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/r" />
<TextView
android:id="#+id/SettingsFragmentSupport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Support"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentFAQ" />
<TextView
android:id="#+id/SettingsFragmentPolicy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Content Policy & EULA"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentSupport" />
<TextView
android:id="#+id/SettingsFragmentUSerAgreement"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="User Agreement"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentPolicy" />
<TextView
android:id="#+id/SettingsFragmentBugReport"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="Report A Bug"
android:textColor="#color/black"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentUSerAgreement" />
<TextView
android:id="#+id/Build"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#a8a4a1"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:text="BUILD INFORMATION"
android:textColor="#color/black"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/SettingsFragmentBugReport" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="1.0.0"
android:textSize="15dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.042"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/Build" />
</androidx.constraintlayout.widget.ConstraintLayout>
The fragment code:
package com.example.create4me;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class Settings extends Fragment {
TextView accSettings, FAQ, BugReport, Policy, UserAgg, Support;
Switch darkModeSwitch;
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
accSettings = (TextView) getView().findViewById(R.id.SettingsFragmentAccountSettings); <--- This one
FAQ = (TextView) getView().findViewById(R.id.SettingsFragmentFAQ);
BugReport = (TextView) getView().findViewById(R.id.SettingsFragmentBugReport);
Policy = (TextView) getView().findViewById(R.id.SettingsFragmentPolicy);
UserAgg = (TextView) getView().findViewById(R.id.SettingsFragmentUSerAgreement);
Support = (TextView) getView().findViewById(R.id.SettingsFragmentSupport);
darkModeSwitch = (Switch) getView().findViewById(R.id.SettingsFragmentDarkModeSwitch);
accSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), AccountSettings.class);
startActivity(intent);
}
});
BugReport.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BugReport.class);
startActivity(intent);
}
});
FAQ.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Policy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
UserAgg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
Support.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDiag();
}
});
darkModeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
/*
In here, put 0 into MySqli DB if the isChecked value = False.
Put 1 into MySqli DB if the isChecked value = 1.
*/
}
});
return inflater.inflate(R.layout.settings, container, false);
}
void alertDiag(){
new AlertDialog.Builder(getContext())
.setTitle("Well here we are")
.setMessage("Normally, the user by now would've gone into a website explaining the content of the pressed TextView.\n"
+ "Since we do not have a website or a domain, we show this AlertDialog. Sorry for the turnout!")
.setNegativeButton(android.R.string.no, null)
.show();
}
}

You need to do on the first line of onCreateView:
View view = inflater.inflate(R.layout.settings, container, false);
Then do for each of your findViewById:
view.findViewById(R.id.your_view);
You're inflating the view last instead of first.
Then at the end of onCreateView():
return view;

Please try below way to initialize and return in your oncreateview
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.settings, container, false);
//Init all here like
accSettings = (TextView) view. findViewById(R.id.SettingsFragmentAccountSettings)
//and then return like
return view;
}
Hope it may help you

Related

Android java, Side drawer menu is showing up whenever the app is launched and the app is getting crashed on closing the menu

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
The terminal shows that the error is with the onOptionsItemSelected function
I have referred many StackOverflow posts regarding errors with side navigation drawer but I couldn't find the solution
Logcat
2022-05-08 17:13:15.175 867-867/com.example.books E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.books, PID: 867
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1736)
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1722)
at androidx.appcompat.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:287)
androidx.appcompat.app.ActionBarDrawerToggle.onOptionsItemSelected(MainActivity.java:245)
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
at android.app.Activity.onMenuItemSelected(Activity.java:4324)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected
at androidx.appcompat.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:188)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30250)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/dl">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SearchView
android:id="#+id/searchView"
android:layout_width="277dp"
android:layout_height="34dp"
android:layout_alignParentTop="true"
android:background="#drawable/bg_white_rounded"
android:iconifiedByDefault="false"
android:queryHint="Search book,title,author, etc.."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.035"
app:queryBackground="#drawable/bg_white_rounded"
app:submitBackground="#drawable/bg_white_rounded"
tools:ignore="MissingConstraints">
</SearchView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:contentPadding="#dimen/cardview_default_elevation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage5"
android:layout_marginLeft="10dp"
android:layout_width="76dp"
android:layout_height="100dp"
android:src="#drawable/bookdonation" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.136"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:clickable="true"
android:src="#drawable/onlinepurchase" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView3"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" >
<ImageView
android:id="#+id/idIVCourseImage2"
android:layout_marginTop="10dp"
android:layout_width="96dp"
android:layout_height="80dp"
android:src="#drawable/prouddonars" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FICTION AND NON-FICTION BOOKS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.115"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.519" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRVCourse"
android:layout_width="403dp"
android:layout_height="333dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
tools:ignore="MissingConstraints">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Purchase books"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.122"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Donate books"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276"
android:clickable="true"/>
<TextView
android:id="#+id/textView11"
android:layout_width="89dp"
android:layout_height="17dp"
android:fontFamily="#font/inter_medium"
android:text="Proud donars"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.869"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.275" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="407dp"
android:layout_height="148dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.401"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="99dp"
android:layout_height="99dp"
android:src="#drawable/medical"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="MEDICAL HEALTH AND SCIENCE BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Biochemistry| Physiotherapy | Phathalogy"></TextView>
<Button
android:id="#+id/buttonmed"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order "
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:layout_marginBottom="10dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/fictionandnonfiction"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="EXPLORE THE WIDE RANGE OF FICTION AND NON FICTION"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Science Fiction| Short stories | Teens"></TextView>
<Button
android:id="#+id/buttoneng"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/ncert"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="NCERT BOOKS FOR CLASS 1-12"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Physics| Chemistry | Mathematics"></TextView>
<Button
android:id="#+id/buttonfiction"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/competative"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text=" WIDE RANGE OF AVAILABLE COMPITATIVE EXAM BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Banking| UPSC| SSC"></TextView>
<Button
android:id="#+id/buttonfinance"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.777"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_favorite" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="36dp"
android:layout_height="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.045"
app:srcCompat="#drawable/cart1" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="32dp"
android:layout_height="27dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.889"
app:layout_constraintStart_toStartOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_location" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idnav_bar"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
tools:ignore="InvalidId,MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Mainactivity.java
package com.example.books;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationView;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.view.MenuItem.OnMenuItemClickListener;
import java.util.Objects;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button medbutton;
private RecyclerView courseRV;
public DrawerLayout dl;
private Toolbar toolbar;
public ActionBarDrawerToggle abdt;
public TextView TVdonars;
public TextView TVdonatebooks;
public TextView TVpurchasebooks;
public ImageView wishlistimg;
private ArrayList<CourseModel> courseModelArrayList;
private int[] mImages = new int[]{
R.drawable.academic, R.drawable.engineering, R.drawable.medical, R.drawable.fiction
};
private String[] mImagesTitle = new String[]{
"Academic", "Engineering", "Medical", "Fiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Classic Books Donar");
medbutton = findViewById(R.id.buttonmed);
wishlistimg = findViewById(R.id.imageView5);
//button = (Button) findViewById(R.id.button2);
courseRV = findViewById(R.id.idRVCourse);
TVpurchasebooks = findViewById(R.id.textView9);
TVdonatebooks = findViewById(R.id.textView10);
TVdonars = findViewById(R.id.textView11);
TVdonatebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Register.class);
startActivity(intent);
}
});
TVdonars.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Prouddonars.class);
startActivity(intent);
}
});
TVpurchasebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Purchasebooks.class);
startActivity(intent);
}
});
final NavigationView nav_view = (NavigationView) findViewById(R.id.idnav_bar);
nav_view.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.select_catogery) {
Toast.makeText(MainActivity.this, "Select Catogery",
Toast.LENGTH_SHORT).show();
}
if (id == R.id.engineering) {
Toast.makeText(MainActivity.this, "Engineering", Toast.LENGTH_SHORT).show();
}
if (id == R.id.medical) {
Toast.makeText(MainActivity.this, "Medical", Toast.LENGTH_SHORT).show();
}
return true;
}
});
dl = (DrawerLayout) findViewById(R.id.dl);
abdt = new ActionBarDrawerToggle(this, dl, toolbar, R.string.open, R.string.close);
abdt.setDrawerIndicatorEnabled(true);
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
dl.addDrawerListener(abdt);
abdt.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
courseModelArrayList = new ArrayList<>();
courseModelArrayList.add(new CourseModel("Design of Machine Elements, Fourth Edition", "V
B Bhandari", R.drawable.designofmachineelements));
courseModelArrayList.add(new CourseModel("Fluid Mechanics, Sixth Edition", "Munsun,
Okilshi,Huebsch & Rothmayer", R.drawable.fluidmechanics));
courseModelArrayList.add(new CourseModel("Fundamentals of Thermodynamics", "Claus
Borgnakke & Richard E Sonntag", R.drawable.thermodynamics));
courseModelArrayList.add(new CourseModel("Shigley's Mechanical Engineering Design",
"Richard G Budynas & JJ Keith Nisbett", R.drawable.mechanicalengineeringdesign));
courseModelArrayList.add(new CourseModel("Atlas of Human Anatomy, Seventh Edition", "Frank
H. Netter, MD ", R.drawable.atlasofhumananatomy));
courseModelArrayList.add(new CourseModel("Data structures and algorithms", " Alfred V.Aho,
John E.Hopcroft, Jeffrey D.Ullman", R.drawable.datastructures));
courseModelArrayList.add(new CourseModel("Textbook of Clinical Neuroanatomy, Third
Edition", "Vishram Singh ", R.drawable.clinicalneuroanatomy));
// we are initializing our adapter class and passing our arraylist to it.
CourseAdapter courseAdapter = new CourseAdapter(this, courseModelArrayList);
// below line is for setting a layout manager for our recycler view.
// here we are creating vertical list so we will provide orientation as vertical
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
// in below two lines we are setting layoutmanager and adapter to our recycler view.
courseRV.setLayoutManager(linearLayoutManager);
courseRV.setAdapter(courseAdapter);
medbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Medical.class);
startActivity(intent);
}
});
wishlistimg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Wishlist.class);
startActivity(intent);
}
});
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (abdt.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

setOnClickListener crashes the app - Android

I've 3 activity in my app. I set setOnClickListener on all the imagebutton in all the 3 activity. When I run the application and on clicking the first button in the first activity the app crash, but if I comment out addListenerOnButton code in the second activity the app works just fine. Any help is appreciated
This is the code of MainActivity.java
package com.example.caa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageButton;
import android.view.View;
public class MainActivity extends AppCompatActivity {
ImageButton maschio, femmina;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
maschio = (ImageButton) findViewById(R.id.Uomo);
femmina = (ImageButton) findViewById(R.id.Donna);
maschio.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(MainActivity.this, Scelta_Simbolo.class);
String genere = "maschio";
i.putExtra("genere", genere);
startActivity(i);
/*SERVE PER VEDERE DIRETTAMENTE SU SCHERMO UN MESSAGGIO
Toast.makeText(MainActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();*/
}
});
femmina.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(MainActivity.this, Scelta_Simbolo.class);
String genere = "femmina";
i.putExtra("genere", genere);
startActivity(i);
}
});
}
}
And activity_main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sfondo"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pagina 1 - TEST"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<ImageButton
android:id="#+id/Donna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.826"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.661"
app:srcCompat="#drawable/woman" />
<ImageButton
android:id="#+id/Uomo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.793"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.238"
app:srcCompat="#drawable/man" />
<TextView
android:id="#+id/Voce_Uomo"
android:layout_width="157dp"
android:layout_height="35dp"
android:text="Voce Maschile "
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.208"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.28" />
<TextView
android:id="#+id/Voce_Donna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Voce Femminile"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.218"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.629" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is second class code: Scelta_Simbolo.java (without comment on setOnClickListener so in this case crash)
package com.example.caa;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class Scelta_Simbolo extends MainActivity {
ImageButton si_no, su_giu, ok_nope;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scelta_simbolo);
addListenerOnButton();
}
public void addListenerOnButton() {
si_no = (ImageButton) findViewById(R.id.si_no_button);
// su_giu = (ImageButton) findViewById(R.id.su_giu_button);
// ok_nope = (ImageButton) findViewById(R.id.ok_nope_button);
Bundle receiveBundle = this.getIntent().getExtras();
final String receiveValue = receiveBundle.getString("genere");
si_no.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
/* Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "si_no";
i.putExtra("scelta", receiveValue);
startActivity(i);*/
}
});
/* su_giu.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "su_giu";
i.putExtra("scelta", scelta);
startActivity(i);
}
});
ok_nope.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent i = new Intent(Scelta_Simbolo.this, Ripr_Audio.class);
String scelta = "ok_nope";
i.putExtra("scelta", scelta);
startActivity(i);
}
});
*/
}
}
And the xml activity_scelta_simbolo.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sfondo">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pagina 2 - TEST"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="#+id/textView3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.057" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scegli quale tipologia di simboli vuoi utlizzare:"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="25sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.149" />
<ImageButton
android:id="#+id/si_no_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/textView2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.336"
app:srcCompat="#drawable/si_no" />
<ImageButton
android:id="#+id/su_giu_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/si_no_button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/si_no_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.531"
app:srcCompat="#drawable/su_giu" />
<ImageButton
android:id="#+id/ok_nope_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/su_giu_button"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/su_giu_button"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.728"
app:srcCompat="#drawable/ok_nope" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thank you

Hide/Show TextView when touching button inside a Fragment

So I am here asking for your kind help.
I am trying to make a TextView visible and gone by touching a single button.
It did work inside a MainActivityJava but when doing the same inside a Fragment Activity, I am facing many troubles.
It seems that the setOnClickListener does not match with fragment but I do not know how to deal with it...
package com.androidbegin.locumatix;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class Tab1Fragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
return view;
}
Button button = getActivity().findViewById(R.id.buttonTaureauBrute);
TextView textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
textView.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
}
};
}
<?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"
android:background="#drawable/wallpaper_selection">
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="#+id/imageButtonTaureauSon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:scaleType="fitXY"
app:srcCompat="#drawable/prononciation_icone" />
<Button
android:id="#+id/buttonTaureauBrute"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="5"
android:text="Prendre le taureau par les cornes"
android:textAlignment="center"
android:onClick="onClick"/>
</LinearLayout>
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_weight="1"
android:gravity="center"
android:visibility="gone"
android:text="Affronter une difficulté avec détermination"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
app:srcCompat="#drawable/taureau" />
<Button
android:id="#+id/button10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Exemples :"
android:textAlignment="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NATURE :\nverbe"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="REGISTRE :\nstandard"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="NIVEAU :\nB1"
android:textAlignment="center"
android:textColor="#color/colorAccent" />
</LinearLayout>
<Button
android:id="#+id/button14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EXPLICATIONS :" />
<Button
android:id="#+id/button11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="USAGES :" />
<Button
android:id="#+id/button13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lexique :" />
<Button
android:id="#+id/button12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Traductions :" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
Try this code
public class Tab1Fragment extends Fragment {
Button button;
TextView textView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_tab1, container, false);
button = getActivity().findViewById(R.id.buttonTaureauBrute);
textView = getActivity().findViewById(R.id.textView3);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if( view.getVisibility() == View.GONE ) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.GONE);
}
}
};
return view;
}
}

unable to get value from edittext into string

I'm trying to make an android app which will show a Toast on button click. That Toast contains the number entered by the user in edittext field. The problem is that i am entering text to edittext(Numeric) field and on button click, Toast isn't showing the text entered by me. Toast is completely blank.
Here is my code:-
Activity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends AppCompatActivity {
String username, password;
Button payNGO, payGO;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_login);
payNGO = (Button) findViewById(R.id.payngo);
payGO = (Button) findViewById(R.id.paygo);
usernameField = (EditText) findViewById(R.id.forno);
passwordField = (EditText) findViewById(R.id.dob);
username = usernameField.getText().toString();
password = passwordField.getText().toString();
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
}
}
Activity.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=".login"
android:background="#drawable/back">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/forno"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438"
app:srcCompat="#drawable/ico" />
<EditText
android:id="#+id/forno"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="20dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Number"
android:inputType="number"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/dob"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="124dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Date of Birth"
android:inputType="numberPassword"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="92dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/payngo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="20dip"
android:layout_marginStart="15dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (NGO)"
tools:layout_editor_absoluteX="204dp"
tools:layout_editor_absoluteY="440dp" />
<Button
android:id="#+id/paygo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="15dip"
android:layout_marginStart="20dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (GO)"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="440dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#drawable/rect_back_text"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dip"
android:layout_weight="1"
android:text="#string/linef"
android:textAlignment="textEnd"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="505dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/heart"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dip"
android:layout_weight="1"
android:text="#string/linee"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="171dp"
tools:layout_editor_absoluteY="505dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Since you are getting the value in onCreate which will be executed even before you enter anything, your Toast shows blank data.
Move the getText() methods to onClick to achieve expected result as follows:
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
Toast.makeText(login.this, usernameField.getText().toString(), Toast.LENGTH_LONG).show();
you can use this code it definitely work..

How to open a browser through a Relative Layout

I have tried following 2 different links with no success.
https://stackoverflow.com/questions/3536165/how-can-imageview-link-to-web-page
and
https://stackoverflow.com/questions/3505788/how-to-open-default-browser/3505804#3505804
I do apologize if this is a simple fix but i'm that nooby and getting frustrated because according to those examples it should work.
Here is my Xml layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/grey"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="#color/grey"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:padding="5sp" >
<ImageView
android:id="#+id/logo_about"
android:layout_width="85dp"
android:layout_height="105dp"
android:layout_gravity="center_vertical"
android:layout_weight="0.11"
android:src="#drawable/web_icon" />
</LinearLayout>
<TextView
android:id="#+id/tv_content_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_marginTop="5sp"
android:background="#color/grey"
android:gravity="center_horizontal"
android:padding="5sp"
android:text="#string/info_about_us"
android:textColor="#color/black"
android:textSize="15sp" />
<!-- Contact Us -->
<RelativeLayout
android:id="#+id/contact"
android:clickable="true"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/img_icon"
android:clickable="true"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_email" />
<TextView
android:id="#+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/img_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator"
android:layout_toRightOf="#+id/img_icon"
android:singleLine="true"
android:text="#string/title_contact_us"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name"
android:layout_alignRight="#+id/tv_name"
android:layout_below="#+id/tv_name"
android:singleLine="true"
android:text="#string/info_contact_us"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator"
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:id="#+id/devider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Facebook -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/facebook_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_facebook" />
<TextView
android:id="#+id/tv_name1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/facebook_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/facebook_icon"
android:layout_toRightOf="#+id/facebook_icon"
android:singleLine="true"
android:text="#string/title_facebook"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name1"
android:layout_alignRight="#+id/tv_name1"
android:layout_below="#+id/tv_name1"
android:singleLine="true"
android:text="#string/info_facebook"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Google Plus -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:clickable="true"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/google_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_googleplus" />
<TextView
android:id="#+id/tv_name2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/google_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/google_icon"
android:layout_toRightOf="#+id/google_icon"
android:singleLine="true"
android:text="#string/title_google_plus"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name2"
android:layout_alignRight="#+id/tv_name2"
android:layout_below="#+id/tv_name2"
android:singleLine="true"
android:text="#string/info_google_plus"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Website -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/website_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_website" />
<TextView
android:id="#+id/tv_name3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/website_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator3"
android:layout_toRightOf="#+id/website_icon"
android:singleLine="true"
android:text="#string/title_website"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name3"
android:layout_alignRight="#+id/tv_name3"
android:layout_below="#+id/tv_name3"
android:singleLine="true"
android:text="#string/info_website"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- Rate Us -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/rate_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_rate_me" />
<TextView
android:id="#+id/tv_name4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rate_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator4"
android:layout_toRightOf="#+id/rate_icon"
android:singleLine="true"
android:text="#string/title_rate_us"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name4"
android:layout_alignRight="#+id/tv_name4"
android:layout_below="#+id/tv_name4"
android:singleLine="true"
android:text="#string/info_rate_us"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
<!-- More Apps -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50sp"
android:background="#drawable/btn_bg" >
<ImageView
android:id="#+id/apps_icon"
android:layout_width="40sp"
android:layout_height="40sp"
android:layout_centerVertical="true"
android:layout_marginLeft="5sp"
android:scaleType="fitXY"
android:src="#drawable/icon_more_app" />
<TextView
android:id="#+id/tv_name5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/apps_icon"
android:layout_marginLeft="5sp"
android:layout_marginRight="5sp"
android:layout_toLeftOf="#+id/img_indicator5"
android:layout_toRightOf="#+id/apps_icon"
android:singleLine="true"
android:text="#string/title_more_app"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_content5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_name5"
android:layout_alignRight="#+id/tv_name5"
android:layout_below="#+id/tv_name5"
android:singleLine="true"
android:text="#string/info_more_app"
android:textColor="#color/black"
android:textSize="13sp" />
<ImageView
android:id="#+id/img_indicator5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5sp"
android:src="#drawable/ic_action_next_item" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="#color/grey" />
</RelativeLayout>
</LinearLayout>
Now my Fragment.java
package com.wny.wecare.fragment;
import android.app.Fragment;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
return mRootView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
}
}
do this rather bro
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
if(v == img)
Context.startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://infinitycodeservices.com"))); //context stands 4 ur activity
}
Edit 1: i change the intent to Intent this was the reason for the earlier variable error..
so retry & seee
let me know if it works.. im feeling i left something.
According to my understanding, your public void onClick(View v) not called you just declare but don't set Listener,then when you set listener to ImageView on onClick() that never call too.
I think you should setListener() in onCreateView() by using anonymous inner class like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
});
return mRootView;
}
or If you still use implement View.OnClickListener() use this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRootView = inflater.inflate(R.layout.fragment_about_us, container, false);
ImageView Img = (ImageView) getView().findViewById(R.id.img_indicator);
Img.setOnClickListener(this);
return mRootView;
}
#Override
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
}
Hope this helps
After Hours of reading i finally solved this issue.
Thanks to this link Correct way to call onClickListener on fragments
Thank You all for your help, very appreciated.
Here is my full AboutUsFragment.Java
package com.wny.wecare.fragment;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import com.wny.wecare.MainActivity;
import com.wny.wecare.R;
public class AboutUsFragment extends Fragment implements OnClickListener {
ImageView Contact;
ImageView Facebook;
ImageView Google;
ImageView Website;
ImageView Rate;
ImageView Apps;
public static final String TAG = AboutUsFragment.class.getSimpleName();
private View mRootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mRoot = inflater.inflate(R.layout.fragment_about_us, null);
Contact = (ImageView) mRoot.findViewById(R.id.contact_icon);
Contact.setOnClickListener(this);
Facebook = (ImageView) mRoot.findViewById(R.id.facebook_icon);
Facebook.setOnClickListener(this);
Google = (ImageView) mRoot.findViewById(R.id.google_icon);
Google.setOnClickListener(this);
Website = (ImageView) mRoot.findViewById(R.id.website_icon);
Website.setOnClickListener(this);
Rate = (ImageView) mRoot.findViewById(R.id.rate_icon);
Rate.setOnClickListener(this);
Apps = (ImageView) mRoot.findViewById(R.id.apps_icon);
Apps.setOnClickListener(this);
return mRoot;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.contact_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:infinitycodeservices#gmail.com")));
break;
case R.id.facebook_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://facebook.com/infinitycodeservices")));
break;
case R.id.google_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://plus.google.com/105796163192090141980/")));
break;
case R.id.website_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));
break;
case R.id.rate_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
case R.id.apps_icon:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/developer?id=InfinityCodeServices")));
break;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
Intent intent;
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
String cameback="CameBack";
intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("Comingback", cameback);
startActivity(intent);
return true;
}
return false;
}
}
You are in a Fragment so I think you should invoke startActivity() method using:
getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://infinitycodeservices.com")));

Categories

Resources