I have tried everything I can possibly think of, I have tried many solutions in Stack as well. None seem to resolve my issue. I am trying to pass a string that I passed from an earlier intent (parsed JSON data). Everytime I call set text I get a Null Pointer Exception, I have tried using a method to set the text as well as just setting it. Any assistance would be greatly appreciated.
This is the fragment I am trying to update.
public class HomeFragment extends Fragment{
TextView tv;
public HomeFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater lf = getActivity().getLayoutInflater();
View rootView = lf.inflate(R.layout.fragment_home, container, false);
tv = (TextView) rootView.findViewById(R.id.accountName);
tv.setText("test");
return rootView;
}
}
I just put a regular string in to test. Here is the 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/lblCompany"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
</RelativeLayout>
Thanks In advance for absolutely any help.
This is the id set on your TextView in xml:
android:id="#+id/lblCompany"
This is the id you are searching for in onCreateView:
tv = (TextView) rootView.findViewById(R.id.accountName);
The ids need to match for you to update the text in that particular TextView. Try
tv = (TextView) rootView.findViewById(R.id.lblCompany);
instead.
Related
I am using fonttype defined this way, and android studio's layout design view is showing the string/textview in the weathericon font.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimaryLight">
<TextView
android:id="#+id/tvFragThird"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="#font/weathericons"
android:text="#string/textview"
android:textSize="30sp" />
</RelativeLayout>
But,when I am running this piece of code:
public class ThirdFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_third, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragThird);
// Typeface typeface = ResourcesCompat.getFont(getContext(), R.font.weathericons);
// tv.setTypeface(typeface);
tv.setText("");//getArguments().getString("msg"));
return v;
}
I am just getting the text in normal font. I have tried both defining the fontfamily in layout and typeface in java, and none of them are working.
what I am missing?
As it's mentioned here https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml, custom font using fontFamily, needs API >= 26.
So change TextView tag to android.support.v7.widget.AppCompatTextView tag (if you use API < 26).
I had this problem and i came across above link and did above change and my problem is solved and i hope, it solves yours (if it has not been solved yet) and others.
Update:
As RobCo commented, if you can change to AppCompatActivity, you don't need above change. It's mentioned here : https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView
I have the following fragment working in view pager:
<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:background="#color/window_background"
android:id="#+id/font"
tools:context="com.cuentos.lib.cuentosmusicales.Libro">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:gravity="center"
android:id="#+id/titleBook"
android:textSize="25sp"
android:textColor="#2211CC"
android:text="#string/hello_blank_fragment" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/titleBook"
android:id="#+id/textBook"
android:textSize="15sp"
android:textColor="#44BB11"
android:text="#string/hello_blank_fragment" />
</RelativeLayout>
and i want to modify textview values from fragment class
public class Libro extends Fragment {
String titleLibro;
String text;
TextView title, text1;
public Libro() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_libro, container, false);
title = (TextView) view.findViewById(R.id.titleBook);
text1 = (TextView) view.findViewById(R.id.textBook);
title.setText("hi friends");
text1.setText("text");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_libro, container, false);
}
}
So, I detect im not able to change data because there is no change, so how I can instanciate textview in fragment?
First, you're inflating your view two times. There's one on the first line of the onCreateView, which is the one that you modify the TextViews, but in the return statement you create and return another, you're not returning the one you modified, but a new one instead, so the view that will be presented in the UI is the not modified.
Also, you're calling the findViewById in the wrong lifecycle stage. Notice that onCreateView returns the view that will be present in your fragment. It's not possible to retrieve a view component like yours TextViews that way, because the view itself doesn't exists yet.
The right method to modify your view components is in the onViewCreated:
#Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
title = (TextView) view.findViewById(R.id.titleBook);
text1 = (TextView) view.findViewById(R.id.textBook);
title.setText("hi friends");
text1.setText(text);
}
Try setting the text when overriding your Fragments onActivityCreated or onResume methods.
I have a RelativeLayout containing a TextView.
I want to append a new TextView under the existing TextView programmatically in the onCreateView method, but what happens when I try to do so, is that the TextView I add programmatically gets overlapped with the existing one.
More details below:
public static class ContentTours extends Fragment{
public static String TOUR_NAME= "tour_info";
View view = getView();
TextView contentTour;RelativeLayout toursView;
public ContentTours(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tours_view, container, false);
toursView = (RelativeLayout) view.findViewById(R.id.rel_layout);
contentTour = (TextView) view.findViewById(R.id.tours);
contentTour.setText(getArguments().getString("2"));
ViewGroup.LayoutParams stt = contentTour.getLayoutParams();
TextView nt = new TextView(getActivity().getBaseContext());
nt.setText(getArguments().getString("1"));
toursView.addView(nt, new ViewGroup.MarginLayoutParams(stt));
return view;
}
}
I have a RelativeLayout containing the TextView. tours_view.xml:
<RelativeLayout android:id="#+id/rel_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_marginBottom="16dp">
<TextView android:id="#+id/tours" android:clickable="true"
android:background="?android:selectableItemBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:text="Lorem ipsum"/>
</RelativeLayout>
the reason why I have a Fragment class is that tours_view.xml is a layout that I am applying to another FrameLayout.
By default in a RelativeLayout, the views overlap if there is no relation defined.
What you should do for your requirement is create a relation(rule) between the TextView to be added and the already present TextView.
RelativeLayout.LayoutParams stt = (RelativeLayout.LayoutParams) contentTour.getLayoutParams();
Now, add a rule
stt.addRule(RelativeLayout.BELOW, R.id.tours);
RelativeLayout.BELOW, so that the new TextView is below your old one. Hope this helps.
I've defined the following TextView:
<TextView
android:id="#+id/card12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_margin="15dp"
android:padding="15dp"
android:background="#drawable/selector_card_background"
android:text="Go to Google"/>
And I'd like to open a webpage when clicking the TextView (which shows a text instead of the full url)
I've read a lot about this, but all the thing I've tried doesn't work for me. I've tried writing android:autoLink="web" and view.setMovementMethod(LinkMovementMethod.getInstance()); but this doesn't make the TextView clickable. I also tried via intent but for this I'd have to use Button instead of TextView.
Also say that I'm not using Activity, I'm using Fragment:
public class InformationFragment extends Fragment {
public InformationFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.information_layout, container, false);
return rootView;
}
}
Any ideas about how to do this? Thank you so much.
Try this:
TextView link = (TextView)findViewById(R.id.card12);
link.setText(Html.fromHtml("Go to Google"));
link.setMovementMethod(LinkMovementMethod.getInstance());
This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
I have a problem with my Android application. If I use setText() method for a textView I have problem like: The Application has stopped unexpectedly. Please try again.
Code of the application:
public class TC1 extends ActionBarActivity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tc1);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
textView = (TextView)findViewById(R.id.textView);
textView.setText("Hi"); // wrong line
}
...
}
XML fragment:
<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.example.tc.TC1$PlaceholderFragment" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="35dp"
android:layout_marginTop="20dp"
android:text="Text" />
</RelativeLayout>
Your TextView is part of the Fragment xml but you try to get it from the Activity layout. Move the findViewById() call to the onCreateView() method of your Fragment.
For future questions:
Please do not quote "The Application has stopped unexpectedly" instead check the LogCat output for error messages and stack traces. The error message you quoted is the default crashed information for the user, nothing that provides any detailed informations for us developers.
add this to your code
View view = inflater.inflate(R.layout.fragment_blank, R.id.container,false);
and change
textView = (TextView)findViewById(R.id.textView);
to this
textView = (TextView)view.findViewById(R.id.textView);
Your code is currently trying to access your main activities view which does not contain your textview. Another option is to access it through your fragment in a similar fashion.
It seems that textview is part of fragment not activity_tc1.
So either use textview in activity_tc1 layout. or use textview in onCreateView method of fragment. where fragment_tc1 will be your fragment name.
For example
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tc1, container,
false);
TextView textView = (TextView) rootView.findViewById(R.id.textView);
textView.setText("Hi");
return rootView;
}