问题
I have an activity with multiple tabs and each tab consists of a fragment with some text inside. I would like to have a text with "Read More" which is a link to an URL. Without having the link, everything works fine, but when I try to implement it, I get
E/UncaughtException: java.lang.NullPointerException
So I assume it is the way I implement it. Right now, the fragment has this:
public class About_us extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_us, container, false);
//The part below is For Read More
TextView t2 = (TextView) getView().findViewById(R.id.read_more);
if (t2 != null) {
t2.setMovementMethod(LinkMovementMethod.getInstance());
}
return rootView;
}
}
"read_more" layout has this for the TextView:
< TextView
android: id = "@+id/read_more"
android: layout_width = "match_parent"
android: layout_height = "wrap_content"
android: clickable = "true"
android: text = "@string/link_to_the_website"
android: textColor = "@color/buttonColorPressed" / >
And link_to_website is given in the strings:
< string name = "link_to_the_website" > < a href = "www.google.com/" > Read More Here < /a></string >
Can anyone help me to figure out what is it that I wrote wrong?
回答1:
Ether use your inflated view as
TextView t2 = (TextView) rootView.findViewById(R.id.read_more);
or override onViewCreated and then you can use getView() or directed passed view
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView t2 = (TextView) view.findViewById(R.id.read_more);
// or TextView t2 = (TextView) getView().findViewById(R.id.read_more);
}
Because getview will only return the actual view which was previously created and returned by onCreateView otherwise it will return null, hence the issue
getView()
Get the root viewfor the fragment's layout (theone returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.
so you cannot use getView before the completion of onCreateView.
Then with this approach, you split the task into two parts
onCreateView : for view inflation
onViewCreated: for view and listeners initialization
update : to add links
t2.setMovementMethod(LinkMovementMethod.getInstance());
t2.setText(Html.fromHtml("< string name = 'link_to_the_website' > < a href = 'https://www.google.com/' > Read More Here < /a></string >"));
回答2:
try to change the line in your code
TextView t2 = (TextView) rootView.findViewById(R.id.read_more);
and add the property in your xml in textview
android:autoLink="web"
回答3:
// declare interface in fragment
public interface OnTextSelectedListener
{
void ontextSelected(String url);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (OnTextSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnTextSelectedListener");
}
}
// Implement interface in activity
public void ontextSelected(String url) {
// load your webview
}
Check this link to understand fragment communication better.
来源:https://stackoverflow.com/questions/48264229/how-to-open-url-from-fragment-textview