ERROR: android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView - java

Here is what is going on in my OnCreate method. the recycler view is defined within the class ( RecyclerView rvTweets;)
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timeline);
client = TwitterApp.getRestClient(this);
rvTweets = findViewById(R.id.rvTweets);
tweets = new ArrayList<>();
adapter = new TweetsAdapter(this, tweets);
rvTweets.setLayoutManager(new LinearLayoutManager(this));
rvTweets.setAdapter(adapter);
populateHomeTimeline();
}
There error says: android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvScreenName"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:layout_toEndOf="#+id/ivProfileImage">
<ImageView
android:id="#+id/ivProfileImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="false"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
tools:srcCompat="#tools:sample/avatars" />
<TextView
android:id="#+id/tvScreenName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginStart="5dp"
android:layout_marginTop="0dp"
android:layout_toEndOf="#+id/ivProfileImage"
tools:text="jesusislord" />
<TextView
android:id="#+id/tvBody"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/tvScreenName"
android:layout_alignParentStart="false"
android:layout_marginStart="5dp"
android:layout_marginTop="2dp"
android:layout_toEndOf="#id/ivProfileImage"
tools:text="this is sample text" />
</RelativeLayout>
I tried adding maven(jitpack) to the all programs (in build:gradle project) and the raizlabs dependencies to my build:gradle app file

Related

Hello ,my app crashes every time I invoke Spinner which exists in a Fragment called "Firstfragment"

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//This Spinner is not located in the MainActivity but in A fragment
Spinner s = (Spinner) findViewById(R.id.spinner33);
s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
String msupplier=s.getSelectedItem().toString();
Log.e("Selected item : ", msupplier);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Main MainActivity
<?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_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/drawer">
<include
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_marginTop="104dp"
app:headerLayout="#layout/nav_header"
app:menu="#menu/nav_menu" />
</androidx.drawerlayout.widget.DrawerLayout>
//first Fragment
<?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:id="#+id/Fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2196F3"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView4"
android:layout_width="match_parent"
android:layout_height="773dp"
android:src="#mipmap/background"
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" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_700"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
android:id="#+id/watBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:drawableTop="#drawable/ic_baseline_looks_one_24"
android:editable="true"
android:enabled="true"
android:focusable="auto"
android:gravity="center"
android:includeFontPadding="true"
android:linksClickable="true"
android:onClick="WattMthd"
android:text="#string/WAT"
app:backgroundTint="#android:color/holo_blue_dark"
tools:layout_editor_absoluteX="35dp"
tools:layout_editor_absoluteY="129dp" />
<Button
android:id="#+id/ampBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_two_24"
android:editable="true"
android:gravity="center"
android:onClick="ampMthd"
android:text="#string/AMP"
android:textColorLink="#FFFFFF"
app:backgroundTint="#android:color/darker_gray" />
<Button
android:id="#+id/voltBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_3_24"
android:editable="true"
android:gravity="center"
android:onClick="voltMthd"
android:text="فولت"
app:backgroundTint="#android:color/holo_blue_dark" />
<Button
android:id="#+id/kWbtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableTop="#drawable/ic_baseline_looks_4_24"
android:editable="true"
android:gravity="center"
android:onClick="kWMthd"
android:text="كيلو"
app:backgroundTint="#android:color/holo_blue_dark" />
</LinearLayout>
<Spinner
android:id="#+id/spinner33"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:dropDownWidth="match_parent"
android:entries="#array/SpinerConvertFrom"
android:foreground="#drawable/ic_baseline_arrow_drop_down_circle_24"
android:foregroundGravity="left|center"
android:gravity="center"
android:textAlignment="center"
android:visibility="visible"
app:flow_verticalAlign="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout" />
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/spinner33">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/Text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/AMP"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Volt"
android:inputType="numberDecimal"
android:selectAllOnFocus="true"
android:textAlignment="center" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If you are calling the spinner initialization from the fragment but it exists in the main activity it will crash, you need to call it in the activity, or move it to the xml of the framgnet.
activity_main belongs to MainActivity. So there is a fragment_first which belongs to FirstFragment. The spinner you are trying to initialize belongs to fragment_first, so you have to use it inside FirstFragment or move it (spinner) to activity_main
Hi bro inside you activity_main.xml you didn't define spinner, so you can't get view which you didn't create in your xml file, you can get it from your fragment, or you must move Spinner to activity_main.xml.

android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView

I wrote a little code, but I had a problem. Error that I encounter as: android.widget.RelativeLayout cannot be cast to androidx.recyclerview.widget.RecyclerView
  I looked at the questions that look like this, but the answers that were given did not solve my problem. Can you help me?
Java code as:
public class Fehrest extends AppCompatActivity {
DatabaseManager dbManager = new DatabaseManager();
int tedad,tedad_fav;
private String[] Name, Text,Image,seen, English, Farsi;
private int[] id,fav,id_sen;
Items items = new Items();
SharedPreferences sharedP;
RecyclerView recyclerView;
Story_Adapter adapter;
ArrayList<Book_Items> MyArrayList = new ArrayList<>();
String button_name,tbl_name="story_tbl";
TextView txt_toolbar,txt_sen;
BrokenView brokenView;
BrokenTouchListener listener;
EditText edt_search;
LinearLayout lin_main;
RelativeLayout rlt_buttons;
boolean night_mode,show_english=true;
int day=0,date_mod,today;
Button btn_love,btn_pand,btn_tanz,btn_mazhabi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movies_list);
recyclerView = findViewById(R.id.rcl_movies);
edt_search = findViewById(R.id.edt_search);
rlt_buttons = findViewById(R.id.rlt_buttons);
txt_toolbar = findViewById(R.id.txt_toolbar);
lin_main = findViewById(R.id.lin_main);
txt_sen = findViewById(R.id.txt_sen);
btn_love = findViewById(R.id.btn_love);
btn_pand = findViewById(R.id.btn_pand);
btn_mazhabi = findViewById(R.id.btn_mazhabi);
btn_tanz = findViewById(R.id.btn_tanz);
brokenView = BrokenView.add2Window(Fehrest.this);
listener = new BrokenTouchListener.Builder(brokenView).build();
sharedP = getSharedPreferences(Items.SETTINGS, Context.MODE_PRIVATE);
night_mode = sharedP.getBoolean(Items.NIGHT_MODE,false);
today = sharedP.getInt(Items.TODAY,0);
date_mod = sharedP.getInt(Items.DATE_MODIFIED,0);
day = (today - date_mod);
get_night();
Get_sentence();
adapter = new Story_Adapter(this, MyArrayList);
recyclerView.setLayoutManager(new LinearLayoutManager(Fehrest.this,LinearLayoutManager.VERTICAL,false));
recyclerView.setAdapter(adapter);
button_name = getIntent().getStringExtra("button");
and this my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fehrest"
android:orientation="vertical"
android:id="#+id/lin_main"
android:background="#drawable/background4">
<LinearLayout
android:id="#+id/lin_toolbar"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginBottom="8dp"
android:background="#color/nephritis"
android:orientation="horizontal"
android:padding="5dp"
android:weightSum="7"
>
<EditText
android:id="#+id/edt_search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="6"
android:background="#drawable/textviewstyle2"
android:gravity="center"
android:hint="جستجو..."
android:padding="4dp"
android:textColorHint="#color/White"
android:textColor="#color/White"
android:visibility="gone"/>
<TextView
android:id="#+id/txt_toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="6"
android:text="لیست خانوائخ خاا"
android:textColor="#color/White"
android:textSize="21sp"
android:fontFamily="#font/medium"/>
<ImageView
android:id="#+id/img_search"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_weight="1"
android:src="#drawable/magnify"
android:onClick="img_search_click"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rcl_movies"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:visibility="gone"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rlt_buttons">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_marginRight="20dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_sen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="20dp"
android:background="#drawable/textviewstyle"
android:fontFamily="#font/zahraroosta"
android:padding="8dp"
android:text="Sentence"
android:textColor="#color/description"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_love"
android:layout_width="290dp"
android:layout_height="70dp"
android:layout_below="#id/txt_sen"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:background="#drawable/back_btn3"
android:fontFamily="#font/boldmayriad"
android:onClick="btn_click"
android:paddingEnd="40dp"
android:text="#string/euphorbiaceae"
android:textAllCaps="false"
android:textColor="#color/White"
android:textSize="24sp" />
This is part of my MLX code and the tags are not closed for this reason.

How to set dynamically weight to LinearLayout? [duplicate]

This question already has answers here:
Dynamically changing the linearlayout width or height on Android
(4 answers)
Closed 4 years ago.
In my application I want set dynamically weight to LinearLayout and for this I write below codes, but when running application show me Force close error!
Force close message in logCat :
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
My XML codes :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
tools:context=".activity.UpdateActivity">
<!--Header-->
<RelativeLayout
android:id="#+id/dialogEndCount_headerLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size120"
android:background="#color/tabHomeColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/size40"
android:text="#string/updateDialogTitle"
android:textColor="#color/white"
android:textSize="#dimen/font16" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/dialogUpdate_animate"
android:layout_width="#dimen/size150"
android:layout_height="#dimen/size150"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/size20"
app:lottie_autoPlay="true"
app:lottie_colorFilter="#color/white"
app:lottie_fileName="update.json"
app:lottie_loop="true" />
</RelativeLayout>
<!--Content-->
<RelativeLayout
android:id="#+id/dialogEndCount_contentLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogEndCount_buttonsLay"
android:layout_below="#+id/dialogEndCount_headerLay"
android:layout_marginBottom="#dimen/size10"
android:layout_marginTop="#dimen/size10"
android:background="#drawable/bg_dialog_vip_white">
<!--Type-->
<TextView
android:id="#+id/dialogUpdate_infoTypeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/size15"
android:layout_marginRight="#dimen/size15"
android:layout_marginTop="#dimen/size10"
android:gravity="center_vertical"
android:text="نوع بروز رسانی : اجباری"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<!--Content-->
<ScrollView
android:id="#+id/dialogUpdate_infoContentScrollLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogUpdate_downloadProgress"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_below="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size20">
<RelativeLayout
android:id="#+id/dialogUpdate_infoContentLay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/dialogUpdate_infoContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/updateContent"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<TextView
android:id="#+id/dialogUpdate_infoContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/dialogUpdate_infoContentTitle"
android:layout_marginTop="#dimen/size5"
android:gravity="center_vertical"
android:textColor="#color/favColorON"
android:textSize="#dimen/font14" />
</RelativeLayout>
</ScrollView>
<!--Download layout-->
<com.tellfa.colony.view.DownloadProgressView
android:id="#+id/dialogUpdate_downloadProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size10" />
</RelativeLayout>
<!--Buttons-->
<LinearLayout
android:id="#+id/dialogEndCount_buttonsLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size60"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!--Cancel-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_cancelBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_cancelBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/ic_close"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_cancelBtnImg"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text="#string/updateCancel"
android:textColor="#color/white"
android:textSize="#dimen/font14" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--Direct-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_directBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/green"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_directBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/appro_ic_download_circle"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_directBtnImg"
android:ellipsize="end"
android:gravity="center"
android:layout_marginRight="#dimen/size1"
android:singleLine="true"
android:text="#string/updateOK"
android:textColor="#color/white"
android:textSize="#dimen/font12" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
My JAVA codes:
public class UpdateActivity extends BaseActivity {
#BindView(R.id.dialogEndCount_buttonsLay)
LinearLayout dialogEndCount_buttonsLay;
private Window window;
private Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//Initialize
ButterKnife.bind(this);
activity = this;
window = activity.getWindow();
//Set color to statusBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.tabHomeColor));
}
//Set dynamically weight
dialogEndCount_buttonsLay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
How can I fix this problem?
If you want set dynamically weightSum just use this code :
dialogEndCount_buttonsLay.setWeightSum(3f);
i hope help you
You're mixing LinearLayout and RelativeLayout. Change your layout to use a LinearLayout then weight is an available property on your LayoutParams.
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
params.weight = 1f

Android fragment oncreateview is called but view is not inflated and there are no errors

So I've done this a fair amount of times but I'm truly stumped this time. My fragment is called from my activity with no problems and although the onCreateView() is called, nothing at all shows up, still no errors. I've spent a couple hours looking for similar questions here with no luck.
Here is my swapFragment() method that calls my fragment:
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flgenre, genresFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
Here is my GenresFragment.java:
public class GenresFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnListFragmentInteractionListener mListener;
private DatabaseReference mPostReference;
String userID = FirebaseAuth.getInstance().getCurrentUser().getUid();
private ArrayList<String> genreList = new ArrayList<>();
private MyGenreRecyclerViewAdapter myGenreRecyclerViewAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public GenresFragment() {
}
// TODO: Customize parameter initialization
#SuppressWarnings("unused")
public static GenresFragment newInstance(int columnCount) {
GenresFragment fragment = new GenresFragment();
Bundle args = new Bundle();
args.putInt(ARG_COLUMN_COUNT, columnCount);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mini_add, container, false);
if (rootView == null){
Log.e("TEST1", "Is Null");
}else {
Log.e("TEST2", "Not Null");
}
return rootView;
}
public void failure(){
Toast.makeText(getActivity(),"Something Went Wrong",Toast.LENGTH_LONG).show();
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnListFragmentInteractionListener) {
mListener = (OnListFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnListFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnListFragmentInteractionListener {
// TODO: Update argument type and name
void onListFragmentInteraction(String item);
}
}
And my fragment_mini_add.xml:
<FrameLayout 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="#80000000"
tools:context="com.nocrat.fanti.ProfileActivity"
android:id="#+id/flgenre">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:layout_gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="#string/add_new_project"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="24sp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/rLayout1"
android:layout_below="#id/textView4">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="#string/project_name"
android:gravity="center_vertical"
/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:hint="#string/group_name"
android:inputType="textPersonName"
android:layout_below="#id/editText"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:padding="10dp"
android:layout_below="#id/rLayout1">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create"
android:textColor="#FFD700"
android:textSize="18sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:textColor="#FFD700"
android:textSize="18sp" />
</LinearLayout>
</RelativeLayout>
</FrameLayout>
Here is my onClickListener that calls swapFragment():
addGenre.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
swapFragment();
}
});
Here is my activity xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
android:background="#ffffff"
android:orientation="vertical"
android:id="#+id/flgenre">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:elevation="40dp"
android:background="#ffffff">
<include
layout="#layout/app_bar_profile"
android:layout_height="190dp"
android:layout_width="match_parent"
android:id="#+id/app1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView3"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8musicalnotes64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Genres"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/rounded_genres"
android:gravity="center"
android:clickable="true"
android:id="#+id/addGenres">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
app:srcCompat="#drawable/ic_add_white_48px" />
<TextView
android:id="#+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"
android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView5"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8resume64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:focusableInTouchMode="true">
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Bio" />
<EditText
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Tell us about yourself..."
android:textColor="#000000"
android:textSize="14sp" />
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="edit"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="save"
android:textColor="#FF00FF"
android:layout_gravity="right"
android:gravity="right"
android:padding="5dp"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView6"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8trophy64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Average Rank"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_margin="20dp"
android:elevation="20dp"
android:background="#ffffff"
android:padding="10dp">
<LinearLayout
android:layout_width="50dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView7"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
app:srcCompat="#drawable/icons8meeting64" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Previous Collaborations"
android:layout_gravity="center"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
I finally figured out the answer, all I had to do was change my root element(linear layout) to frame layout and it worked. Hope this helps someone. Thanks to everyone for their suggestions.
Try to modify you swapFragment() with below code by replacing replace() with add()
private void swapFragment() {
GenresFragment genresFragment = new GenresFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.flgenre, genresFragment);
fragmentTransaction.commit();
}
Other people already asked for the activity xml, but for what I could see right now, it looks like you are inflating the fragment's layout in a FrameLayout that is contained in it's own layout.
Meaning, the container you are inflating into should not be in the fragment's layout, but in the activity's layout.
Unless I understood it wrong, you should not have the with id/flgenre in fragment_mini_add.xml but in Activity_main.xml (or whatever the layout of the activity is)
Edit: in both activity and the fragment_mini_add.xml there is a layout with the same id you are trying to inflate into. I'm not sure if this causes the problem, but I imagine this could be the cause.
You should remove android:id="#+id/flgenre" in the first xml tag from your activity xml and fragment_mini_add.xml.
replace tools:context="com.nocrat.fanti.ProfileActivity" with tools:context="com.nocrat.fanti.GenresFragment" in fragment_mini_add.xml.
then, add this code to your activity xml to contain the fragment
<FrameLayout
android:id="#+id/flgenre"
android:layout_width="match_parent"
android:layout_height="match_parent" />

How to find the ID of a dynamic UI element in android

I can make the dynamic UI without problems, but I don't know how to access the view with findviewbyid (R.id._____) I don't know what to put in the blank.
.java code
public class newList extends AppCompatActivity {
Button btnAddItem;
RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_list);
// allows the creation of dynamic rows
btnAddItem = (Button) findViewById(R.id.addItem);
btnAddItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = (ViewGroup) findViewById(R.id.linearVert);
inflater.inflate(R.layout.rows, parent);
}
});
}
}
xml that is associated with the java 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.app.shoppingbuddy.shoppingbuddy.newList">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="300dp"
android:id="#+id/scrollView" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearVert">
</LinearLayout>
</ScrollView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Subtotal"
android:id="#+id/subtotal"
android:textColor="#ffc800"
android:layout_above="#+id/tax"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Tax"
android:id="#+id/tax"
android:textColor="#ee00ff"
android:layout_above="#+id/total"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total"
android:id="#+id/total"
android:textColor="#ff0000"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/subtotalAmount"
android:layout_alignBottom="#+id/subtotal"
android:layout_alignEnd="#+id/scrollView"
android:textColor="#ffc800" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/taxAmount"
android:layout_above="#+id/total"
android:layout_alignParentEnd="true"
android:textColor="#ee00ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/totalAmount"
android:layout_alignBottom="#+id/total"
android:layout_alignEnd="#+id/taxAmount"
android:textColor="#ff0000" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/addItem"
android:layout_above="#+id/subtotal"
android:layout_centerHorizontal="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>
xml used to create rows
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:id="#+id/linearHor">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/itemImage"
android:src="#drawable/box"
android:padding="5dp" />
<EditText
android:layout_width="109dp"
android:layout_height="wrap_content"
android:id="#+id/itemName"
android:hint="Name"
android:layout_weight="0.78" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/itemPrice"
android:hint="$ Price" />
</LinearLayout>
</LinearLayout>
There will be no unique R.id value at the activity level. For example, suppose the user taps the button 10 times, so you inflate the R.layout.rows layout 10 times. As a result, there will be 10 R.id.linearHor widgets, 10 R.id.itemPrice widgets, and so on.
Always call findViewById() on something that is guaranteed to give you a unique result. In this case — as with ListView, GridView, RecyclerView, etc. — you need to use other approaches to find the correct row View, then call findViewById() on that View to get at its child widgets.

Categories