Nested viewgroup is null after binding with butterknife - android

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);
}

Related

Unable to remove dialog background

Hello there I'm trying to remove the background of my dialog with
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
and it worked fine
But after implementing the shadow effect for my dialog this issue comes up again even if the background drawable is set to transparent
this is the line I have added
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
This is a screenshot of it
Full code
sort_image_dialog_fragment.java
public class sort_image_dialoge_fragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.dialog_rounded_background);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
getDialog().getWindow().setBackgroundDrawableResource(android.R.drawable.dialog_holo_light_frame);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setDimAmount(0.8f);
getDialog().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
return view;
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
}
sort_image.xml
<RelativeLayout 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">
<com.google.android.material.card.MaterialCardView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:backgroundTint="#color/grey"
android:elevation="12dp"
app:cardCornerRadius="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="5dp"
android:fontFamily="#font/roboto"
android:text="Sort by"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/divider_line_sort_by"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/sort_textView"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:backgroundTint="#color/lite_grey"
app:cardCornerRadius="50dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<TextView
android:id="#+id/latest_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:fontFamily="#font/roboto"
android:text="Latest"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<TextView
android:id="#+id/most_popular_sort_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/latest_sort_textView"
android:layout_alignParentStart="true"
android:layout_centerInParent="true"
android:layout_marginStart="30dp"
android:layout_marginTop="25sp"
android:fontFamily="#font/roboto"
android:text="Most Popular"
android:textAlignment="center"
android:textColor="#color/lite_grey"
android:textSize="25sp" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true">
<RadioButton
android:id="#+id/latest_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp" />
<RadioButton
android:id="#+id/most_popular_sort_radio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checked="true" />
</RadioGroup>
</RelativeLayout>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</RelativeLayout>
Problem
The issue not related to the layout because whatever layout you assign to a dialog; the dialog applies it to its squared window; and the dialog window is white rectangle by default (i.e. doesn't have rounded corners).
The outside part of this rectangle layout is dimmed by default.
Therefore the white color you see on the corners is related to the dialog window background.
Solution
So, in order to make a rounded corners dialog; you have to change that on its window with below 2 steps:
Step 1: Create a theme in the themes.xml/styles.xml that removes the dialog window background:
<style name="NoBackgroundDialogTheme" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowBackground">#null</item>
</style>
Apply this theme by overriding getTheme() in your custom DialogFragment class:
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
Step 2:: Create a rounded drawable exactly with your card radius (20dp):
res\drawable\rounded_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="20dp" />
</shape>
Apply this to the dialogFragment view using view.setBackgroundResource() and remove other window calls that you provided in onCreateView():
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
UPDATE:
Make sure to use the lower case #null not #Null.
The sort_image_dialog_fragment.java should now look like:
public class sort_image_dialog_fragment extends DialogFragment {
#Nullable
#org.jetbrains.annotations.Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.sort_image, container, false);
view.setBackgroundResource(R.drawable.rounded_background);
return view;
}
#Override
public int getTheme() {
return R.style.NoBackgroundDialogTheme;
}
}
Result:
Side note: the name convention of classes should start with upper case, and follow camel case pattern; so the sort_image_dialog_fragment would be SortImageDialgFragment instead.

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.

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

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;
}

White rectangle on top of DialogFragment

When using a dialogFragment I get a white space rectangle at the top of the fragment without knowing why.
What I should get
What I actually get
My DialogFragment:
public class SelectDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
View v = inflater.inflate(R.layout.select_frag, container, false);
return v;
}
}
select_frag.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="#000"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="New Text"
android:id="#+id/textView" />
</RelativeLayout>
You should set style without title:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, 0);
}
Also you can found more useful example here

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