NullPointerException when attempting to change textView text in fragment [duplicate] - android

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I am getting a nullPointerException for when I try to change the text of a textView within a fragment I have tried suggestions of putting getView() and getActivity() before findByViewId but they both result in errors. Thank you.
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_main, container, false);
//calls setdate function
setDate();
return rootview;
}
public void setDate(){
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
//need to use calendar class as Date is deprecated (old)
TextView textView = (TextView) getActivity().findViewById(R.id.info_text);
textView.setText("updated");
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="75dp">
<TextView
android:id="#+id/info_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test"
android:textSize="25dp"
android:textStyle="bold"
android:textColor="#252525" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view2"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="395dp"
card_view:cardCornerRadius="4dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="15dp">
<TextView
android:id="#+id/info_text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="placeholder_text"
android:textSize="25dp"
android:textStyle="bold"
android:textColor="#252525" />
</android.support.v7.widget.CardView>
</LinearLayout>
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at au.com.alexh.dayrater.MainFragment.setDate(MainFragment.java:43)
at au.com.alexh.dayrater.MainFragment.onCreateView(MainFragment.java:33)

Change your setDate to:
public void setDate(View v){
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
//need to use calendar class as Date is deprecated (old)
TextView textView = (TextView) v.findViewById(R.id.info_text);
textView.setText("updated");
}
and call it as :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview = inflater.inflate(R.layout.fragment_main, container, false);
//calls setdate function
setDate(rootview);
return rootview;
}

Related

Moving from fragment to activity giving error [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
findViewById returns NULL when using Fragment
(7 answers)
Closed 1 year ago.
Actually I saw a yt video of how to intent fragment and acticity and that is what I tried but don"t know how the error is coming.The error which I am having is ** java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.photoframeapp.HomeFragment.onCreateView(HomeFragment.java:31)**.I tried to sort out but I am not able to .
this is my HomeFragment.java class
public class HomeFragment extends Fragment {
TextView beginners;
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});
return view;
}
}
this is my mainpage.java file
public class mainpage extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
}
}
fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="387dp"
android:alpha="0.4"
android:background="#color/Purple" />
<TextView
android:id="#+id/beginners"
android:layout_width="match_parent"
android:layout_height="72dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-27dp"
android:layout_marginBottom="433dp"
android:freezesText="true"
android:text=" BEGINNERS CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="195dp"
android:alpha="0.4"
android:background="#color/DeepPink" />
<TextView
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-9dp"
android:layout_marginBottom="67dp"
android:text=" ADVANCED CALLIGRAPHY"
android:textSize="25dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="4dp"
android:alpha="0.4"
android:background="#color/DarkBlue" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="-8dp"
android:layout_marginBottom="261dp"
android:freezesText="true"
android:text=" INTERMEDIATE CALLIGRAPHY"
android:textSize="25dp" />
</RelativeLayout>
You have to initialize textView before using it.
View view = inflater.inflate(R.layout.fragment_home, container, false);
beginners = view.findViewById(R.id.beginners)
beginners.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(getActivity(),mainpage.class));
}
});

How to use CircleImageView library ( this : 'de.hdodenhof:circleimageview:3.1.0' ) inside fragment?

This is the error I'm gettingI'm trying to use this library in fragment inside onViewCreated() method, but i'm getting this casting error: - java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView cannot be cast to de.hdodenhof.circleimageview.CircleImageView. I don't know how to solve this error. can anybody please help me?
FragmentProfile.java :
public class FragmentProfile extends Fragment implements View.OnClickListener {
Button sign_out;
TextView user_number;
private CircleImageView userImage;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_profile, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
sign_out = getActivity().findViewById(R.id.user_logout);
userImage = getActivity().findViewById(R.id.user_display_name);
user_number = getActivity().findViewById(R.id.phone_number);
sign_out.setOnClickListener(this);
}
#Override
public void onClick(View view) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(getActivity(), Login.class));
getActivity().finish();
}
}
fragment_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="#dimen/dp_180"
android:scaleType="fitXY"
android:src="#drawable/money" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="#dimen/dp_95"
android:layout_height="#dimen/dp_95"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_140"
android:scaleType="centerCrop"
android:src="#mipmap/ic_launcher"
app:civ_border_color="#color/textColor"
app:civ_border_width="#dimen/dp_2" />
<TextView
android:id="#+id/user_display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/profile_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/dp_4"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="#string/username"
android:textColor="#color/border"
android:textSize="#dimen/sp_22" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/user_logout"
android:layout_below="#id/user_display_name"
android:layout_margin="#dimen/dp_12"
android:orientation="vertical">
<TextView
android:id="#+id/phone_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/dp_12"
android:layout_marginTop="#dimen/dp_30"
android:fontFamily="#font/balsamiq_sans_bold"
android:text="+91-987654321"
android:textColor="#android:color/black"
android:textSize="#dimen/dp_18" />
</LinearLayout>
<Button
android:id="#+id/user_logout"
android:layout_width="#dimen/dp_200"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/dp_30"
android:background="#drawable/button_shape"
android:text="#string/sign_out"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textStyle="bold" />
</RelativeLayout>
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// ... rest of the codes.
// Comment out code below.
// userImage = getActivity().findViewById(R.id.user_display_name);
// Replace it with code below.
userImage = getActivity().findViewById(R.id.profile_image);
// ... rest of the codes.
}
Here you are parsing a textview id to the CircleImageView. You should change your userImage = getActivity().findViewById(R.id.user_display_name); to userImage = getActivity().findViewById(R.id.profile_image);. That's why you are getting java.lang.ClassCastException:androidx.appcompat.widget.AppCompatTextView exception.
You issue is here:
private CircleImageView userImage;
userImage = getActivity().findViewById(R.id.user_display_name);
and in your layout:
<TextView
android:id="#+id/user_display_name"
You can not cast a TextView into a CircleImageView.

Nested viewgroup is null after binding with butterknife

Here's my fragment_photo_gallery.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/disconnected_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="gone">
<ImageView
android:id="#+id/disconnected_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_offline" />
<TextView
android:id="#+id/disconnected_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/disconnected_title_text"
android:textAppearance="?android:textAppearanceMedium" />
<TextView
android:id="#+id/disconnected_subtitle_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/disconnected_title_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:fontFamily="sans-serif"
android:gravity="center"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="#string/disconnected_subtitle_text"
android:textAppearance="?android:textAppearanceSmall"
android:textColor="#A2AAB0" />
<Button
android:id="#+id/disconnected_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/disconnected_subtitle_text"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:text="#string/disconnected_button_text" />
</RelativeLayout>
</RelativeLayout>
I bind all the stuff in the class which extends fragment:
public class PhotoGalleryFragment extends Fragment {
....
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
....
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
unbinder = ButterKnife.bind(this, v);
return v;
}
#Override public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
....
}
And get NPE on this method:
private void showDisconnectedView() {
disconnectedView.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
}
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setVisibility(int)' on a null object reference
My build gradle looks ok so I don't know why I get such an error
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
The guy who got similar problem figured out by changing annotationProcessor to apt but in my case, it just won't ever sync.
QUICK UPDATE
The problem still happened even if I replace
#BindView(R.id.disconnected_view)RelativeLayout disconnectedView;
with
disconnectedView = v.findViewById(R.id.disconnected_view); in my onCreateView method. So it must be not butterknife fault.
Use Butterknife without unbinder, You need to return already inflated view rather than creating new view & inflating it.
Also apply view specific code in onViewCreated() method.
public class PhotoGalleryFragment extends Fragment {
#BindView(R.id.disconnected_view)
RelativeLayout disconnectedView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_gallery, container, false);
ButterKnife.bind(this, v);
return v;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
disconnectedView.setVisibility(View.GONE);
}

My findViewById() returns null, but it shouldn't

My findViewById returns null, but it shouldn't.
As you'll see, the button is in the same XML file.
And calling getActivity() makes no difference at all
Here's my onCreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
Button request = (Button) getView().findViewById(R.id.button_request);
return rootView;
}
And here's my fragment_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container_relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context="br.com.igor.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/servic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_marginBottom="0dp"
android:paddingTop="15dp"
android:text="Sample instructions" />
<Button
android:id="#+id/button_request"
style="#style/button_default"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:layout_below="#id/servic"
android:text="Request" />
</RelativeLayout>
getView is returning null, not findViewById. Use rootView in place of getView
Button request = (Button) rootView.findViewById(R.id.button_request);
getView returns the view you inflated in onCreateView

android include change textView value

I include a layout in my fragment1.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:id="#+id/family_header"
layout="#layout/header" />
header xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dip"
android:background="#33cc66"
android:orientation="horizontal" >
<ImageView
android:id="#+id/nav_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/nav_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical" />
<ImageView
android:id="#+id/nav_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I want to change TextView "nav_text" value dynamically in my Fragment1.class . how to do it
you can create class that extends fragement....
public class DetailFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_rssitem_detail,
container, false);
return view;
}
public void setText(String item) {
TextView view = (TextView) getView().findViewById(R.id.detailsText);
view.setText(item);
}
}
in this class you can change value of textview dynemically.....please put your code here.so we can understand what is your actual problem.
Use this code in your fragment oncreateview method like this
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment1, container,false);
final LinearLayout headerLayout= (LinearLayout ) v.findViewById(R.id.family_header);
TextView txtHeaderText=(TextView)headerLayout.findViewById(R.id.nav_text);
txtHeaderText.setText("My New Text");
return v;
}
I hope it's work for you.

Categories

Resources