I'm simply trying to change the text of a textView that is located within the actual fragment. It isn't throwing any error but it simply isn't updating. Aside from this the fragment successfully attaches to the activity via a viewpager.
The fragment:
public class FragmentPlotData extends Fragment {
public static TextView textViewPlotData;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_plot_data, container, false);
textViewPlotData = (TextView) view.findViewById(R.id.textViewPlotData);
textViewPlotData.setText("Changed text ");
return inflater.inflate(R.layout.fragment_plot_data, container, false);
}
}
The fragment's layout:
<TextView
android:id="#+id/textViewPlotData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textColor="#color/black_process_2"
android:text="#string/original text"
android:gravity="center"/>
</RelativeLayout>
You should return view in onCreateView, don't inflate a new layout like that.
Just replace:
return inflater.inflate(R.layout.fragment_plot_data, container, false);
to:
return view;
Related
I'm doing a recycler method in my android studio project but i have a problem.
Everytime i'm trying to findViewById my application is crashing.
Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
And i don't understand why because i'm creating my view in the good way.
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carrito, container, false);
return view;
}
And i'm trying to continue with that
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
...
Log.e("test : ",Integer.toString(R.id.recyclerBuy));
recyView = view.findViewById(R.id.recyclerBuy);
/* recyView.setLayoutManager(new GridLayoutManager(getContext(),RecyclerView.VERTICAL));
recyView.setAdapter(buyAdapter);
*/
}
But i'm crashing at the line recyView = view.findViewById(R.id.recyclerBuy);. My R.id.recyclerBuy is not empty.
And here is my RecyclerView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ui.carrito.CarritoFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerBuy"
android:layout_width="match_parent"
android:layout_height="550dp"
android:layout_marginBottom="88dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent" />
...
</androidx.constraintlayout.widget.ConstraintLayout>
Anyone have a clue why i'm crashing everytime ?
This happens because onAttach() callback is called before onCreateView().
The easiest way to fix the issue is placing the code where you finding your RecyclerView in onCreateView(). Something like:
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carrito, container, false);
recyView = view.findViewById(R.id.recyclerBuy);
// here you recyView won't be null
return view;
}
A good article about fragments lifecycle: https://medium.com/androiddevelopers/the-android-lifecycle-cheat-sheet-part-iii-fragments-afc87d4f37fd
I added the custom toolbar in xml and then added it as Actionbar in fragment.
But it's not showing anything from Title to Menus. The only actionbar is visible
Here is my xml code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Must be last for right layering display -->
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary" />
</FrameLayout>
</FrameLayout>
And here is my java code
public class searchView extends Fragment{
Toolbar search;
MaterialSearchView search_view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_view,container,false);
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.search_view, container, false);
search = (Toolbar)layout.findViewById(R.id.toolbar2);
((AppCompatActivity) getActivity()).setSupportActionBar(search);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
setHasOptionsMenu(true);
return v;
}
}
In these code I called the fragment in MainActivity On Bottombar click
else if (position==1){
searchView f = new searchView();
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,f).commit();
}
return true;
Toolbar search;
MaterialSearchView search_view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_view,container,false);
setHasOptionsMenu(true);
search = (Toolbar)v.findViewById(R.id.toolbar2);
((AppCompatActivity) getActivity()).setSupportActionBar(search);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
return v;
}
}
to show options menu override
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//add your menu here;
}
Im trying to get the TextViews shown in my faq_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:text="Title 1"/>
<TextView
android:id="#+id/answer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAlignment="center"
android:text="Description 1"
android:textColor="#33000000"
android:layout_marginTop="5dp"/>
</LinearLayout>
In my Fragment class
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
((TextView)container.findViewById(R.id.question_text)).setText(title);
((TextView)container.findViewById(R.id.answer_text)).setText(description);
return inflater.inflate(R.layout.faq_fragment, container, false);
}
App is crashing and Im getting error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
You need to inflate the view before using it.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.faq_fragment, container, false);
((TextView)view .findViewById(R.id.question_text)).setText(title);
((TextView)view .findViewById(R.id.answer_text)).setText(description);
return view;
}
You should use the inflated view:
Change it to:
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.faq_fragment, container, false);
((TextView)view.findViewById(R.id.question_text)).setText(title);
((TextView)view.findViewById(R.id.answer_text)).setText(description);
return view;
}
In my Android app I've got an EditText to which I want to do a setError() when it is empty. The EditText is located in a file called fragment_amount.
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_amount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
/>
and I load this in a Fragment like so:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_amount, null);
}
From within the parent fragment of the fragment in which this EditText is initially loaded (using the snippet above) I now want to use setError like so:
EditText amountField = (EditText) getView().findViewById(R.id.fragment_amount);
amountField.setError("ERRROROROROR");
Unfortunately, I get a ClassCastException on the first line, saying: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.EditText. The strange thing it that in the fragment in which I initially load this EditText (AmountFragment.java), it is loaded in exactly the same way, without any errors.
Does anybody know how I can load this EditText in the parent fragment as an EditText so that I can use the setError() method? All tips are welcome!
Try this...
You have given the name fragment_amount for your layout and also you have given the same name as id for your edittext.
So, try changing the id for your edittext in both xml and java.(For example, here i have given as fragment_amount_edit)
XML :
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_amount_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
/>
JAVA :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_amount, null);
EditText amountField = (EditText) view.findViewById(R.id.fragment_amount_edit);
amountField.setError("ERRROROROROR");
return view;
}
Some times the same name of layout or any other widget can be conflicted.
In your code there is same id of EditText is fragment_amount and same layout name as fragment_amount. So it is being conflicted.
Change from here:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_amount, null);
Now you are inflating your layout to View. So object of that View must be referenced to find the id of EditText. So
EditText edt= (EditText) view.findViewById(R.id.youreditId);
edt.setError("ERRROROROROR");
return view;
}
i'm trying to create a fragment dialog and need the full space to display information. so i want to disable the titlebar. But after i disabled it the isn't maximized in width anymore.
public class ArticleSearchFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, getTheme());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_search, container, false);
...
}
And the XML:
<?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:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/searchField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ean / Name / Produkt Nummer"
android:completionThreshold="1" />
<Button
android:id="#+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cancel" />
</LinearLayout>
put this code in your onCreateView() of dialog fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_image, null);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
It will remove title bar from Dialog Fragment