Issue with Setting Text on TextView outside the OnCreate Method [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
i have been creating an App that uses Fragments Mostly
here is how it is supposed to work, on the Home Fragment i click a button that takes me to another Fragment for filling information
on this fragment when I click a button a Dialog Fragment opens and I select a City Name then Submit, it dismisses the Dialog and is supposed to SetText on a TextView.
I use an interface that calls a method on the City Selection fragment in order to set the Text. here is some Code
Declaration for the EditText
EditText editText_From;
Setting finding the View
OnCreate
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_trip_date, container, false);
editText_From = view.findViewById(R.id.editText_From);
return view;
}
public void setSelectedCity(String city)
{
Log.i("<<269>>", "Setting Text on Edit Text <<269>>:" + city);
editText_From.setText(city);
}
This Method below is supposed to set text for the my EditText or TextView
public void setSelectedCity(String city)
{
Log.i("<<269>>", "Setting Text on Edit Text <<269>>:" + city);
selectedCityConfirmed = city;
editText_From.setText(city);
}
the App Crashes on EditText with the Following Error Message,
02-16 19:37:43.928 26582-26582/com.example.bob2609.busticketingapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bob2609.busticketingapp, PID: 26582
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' on a null object reference
at com.example.bob2609.busticketingapp.TripDateFragment.setSelectedCity(TripDateFragment.java:188)
at com.example.bob2609.busticketingapp.MainActivity.selectedCity(MainActivity.java:176)
at com.example.bob2609.busticketingapp.LocationSelector$1.onItemClick(LocationSelector.java:69)
at android.widget.AdapterView.performItemClick(AdapterView.java:313)
at android.widget.AbsListView.performItemClick(AbsListView.java:1201)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3195)
at android.widget.AbsListView$3.run(AbsListView.java:4138)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
The Interface Method on MainActivity
#Override
public void selectedCity(String city)
{
tripDateFragment.setSelectedCity(city);
}
Anyone knows a way around this?

Why are you declaring again Textview again in onCreate Method?
Declare outside the onCreate and Find the View using "findViewByID" inside on create.

Related

Not able to pass string value from one fragment to another [duplicate]

This question already has answers here:
How to pass values between Fragments
(18 answers)
Closed 5 years ago.
I know there are so many qustions regarding this , but none of them are solving my problem
I want to pass a string variable from one fragment to other, here is the code I did. but I am getting error (I will show the error)
from first fragment I did
Desc_1 ldf = new Desc_1 ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);
getFragmentManager().beginTransaction().add(R.id.frame, ldf).commit();
and incoming second fragment I did like
public class Desc_1 extends Fragment {
public Desc_1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview=inflater.inflate(R.layout.fragment_desc_1,container,false);
String value = getArguments().getString("YourKey");
Toast.makeText(getActivity(), value,
Toast.LENGTH_LONG).show();
return rootview;
}
}
I am getting error like
07-27 20:31:06.386 26816-26816/com.example.jaison.newsclient E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.jaison.newsclient, PID: 26816
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
at layout.Desc_1.onCreateView(Desc_1.java:34)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2239)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1574)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1641)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2415)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2200)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2153)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2063)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:725)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5769)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:751)
try this :
Bundle bundle = getArguments();
if(bundle != null)
String value = bundle.getString("YourKey");
Just check if getArguments() is not null inside your fragment, and then inside the curly braces add your code
Example:
Bundle bundle_arguments = getArguments();
if(bundle_arguments != null) {
// your code
}
Please check this answer for passing values between fragments
https://stackoverflow.com/a/27626004/5065318

Unexpected NullpointExpection inside a setter [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 6 years ago.
I'm trying to do a simple operation: click a button and show a custom DialogFragment, but I'm getting a NullPointExpection and I can't figure out why.
mAlertDialog.java:
public class mAlertDialog extends DialogFragment {
TextView title;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_invalid_time, container, false);
title = (TextView)view.findViewById(R.id.titleText);
return view;
}
public void setTheTitle(String title) {
this.title.setText(title);
}
}
Showing mAlertDialog:
mAlertDialog dialog = new mAlertDialog();
dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
dialog.show(fm, "InvalidTime");
Error message:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at m.inschool8.Objects.mAlertDialog.setTheTitle(mAlertDialog.java:20)
at m.inschool8.bSubjects.Fragment_Subjects$55.onClick(Fragment_Subjects.java:2654)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
The title is still null when you are calling
mAlertDialog dialog = new mAlertDialog();
dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
so what you can do is
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog_invalid_time, container, false);
title = (TextView)view.findViewById(R.id.titleText);
setTheTitle(getActivity().getString(R.string.invalidTimeTitle));
return view;
}
// call it
mAlertDialog dialog = new mAlertDialog();
dialog.show(fm, "InvalidTime");
Your title is null until the DialogFragment is shown causing onCreateView to kick-in. Hence change the order as below:
mAlertDialog dialog = new mAlertDialog();
dialog.show(fm, "InvalidTime");
dialog.setTheTitle(getActivity().getString(R.string.invalidTimeTitle));

How should I make sure that the onCreate(View/Dialog) has finished running, so everything is properly initialised?

Answer
The answer / an example can be found at the bottom of this post. The way this answer has been reached, can be found in this answer and its comments.
Original question
I've got a dialogFragment, which has two editText input fields. On a specific buttonClick, I want this dialogFragment to be created, and the two editText fields to be filled with text. For this purpose, I created a simple method inside the dialogFragment's class.
public void presetFields(String nameField, String tagField) {
nameInputField.setText(nameField);
tagInputField.setText(tagField);
}
Problem is, that both nameInputField and tagInputField get initialised inside the onCreateDialog method.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View v_iew = inflater.inflate(R.layout.fragment_inputdialog, null);
nameInputField = (EditText) v_iew.findViewById(R.id.inputdialogname);
tagInputField = (EditText) v_iew.findViewById(R.id.inputdialogtag);
}
I thought that would be no problem at all, since I have a similar construction running for a fragment. Only difference is that the editText's get initialised in an onCreateView instead of an onCreateDialog.
This is the onClick code that shows the dialogFragment and calls the method to set the nameInputField and tagInputField fields.
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stuffManagerInputDialogFragment.show(getFragmentManager(), "TEST");
stuffManagerInputDialogFragment.presetFields(nameTextViewContent, tagTextViewContent);
}
});
When I click the editButton, this is the log I get with the NPE
03-02 16:04:25.120 509-509/com.example.tim.timapp I/art: Not late-enabling -Xcheck:jni (already on)
03-02 16:04:25.190 509-509/com.example.tim.timapp W/System: ClassLoader referenced unknown path: /data/app/com.example.tim.timapp-2/lib/x86_64
03-02 16:04:25.400 509-524/com.example.tim.timapp D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
03-02 16:04:25.520 509-524/com.example.tim.timapp I/OpenGLRenderer: Initialized EGL, version 1.4
03-02 16:04:25.570 509-524/com.example.tim.timapp W/EGL_emulation: eglSurfaceAttrib not implemented
03-02 16:04:25.570 509-524/com.example.tim.timapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7fceb5b27dc0, error=EGL_SUCCESS
03-02 16:04:28.660 509-509/com.example.tim.timapp D/TEST: 2131493026
03-02 16:04:30.310 509-509/com.example.tim.timapp D/AndroidRuntime: Shutting down VM
03-02 16:04:30.310 509-509/com.example.tim.timapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tim.timapp, PID: 509
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
at com.example.fragments.MainFragments.DialogFragments.StuffManagerInputDialogFragment.presetFields(StuffManagerInputDialogFragment.java:149)
at com.example.fragments.MainFragments.VariableFragments.StuffManagerVariableFragment$2.onClick(StuffManagerVariableFragment.java:87)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-02 16:04:33.140 509-509/com.example.tim.timapp I/Process: Sending signal. PID: 509 SIG: 9
I'm not an expert by far, but what I make of this error, is that nameTextView and tagTextView are null at the moment that I'm trying to set text to them, which makes me think the method gets called before the fragment is created.
How can I make sure that both editText's are properly initialised before I try to call something on it?
I have removed some bits of code for easy reading. If something seems to be missing, it probably is. Please let me know, so I can add it.
If you require more code, please also let me know.
Complete answer
All credits go to George Mulligan for this one.
So some stuff has changed from the original approach. First of all, the presetFields method has been removed. The text that should populate the editText fields is now passed to the fragment as an argument.
The onClick method now looks like this:
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nameTextViewContent = nameTextView.getText().toString();
tagTextViewContent = tagTextView.getText().toString();
Bundle args = new Bundle();
args.putString("name", nameTextViewContent);
args.putString("tag", tagTextViewContent);
stuffManagerInputDialogFragment.setArguments(args);
stuffManagerInputDialogFragment.show(getFragmentManager(), "TEST");
}
});
So without the presetFields method, we have no way to set the editText fields to the text we want. This has been added to the onCreateDialog method, as can be seen below.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View v_iew = inflater.inflate(R.layout.fragment_inputdialog, null);
nameInputField = (EditText) v_iew.findViewById(R.id.inputdialogname);
tagInputField = (EditText) v_iew.findViewById(R.id.inputdialogtag);
if (getArguments() != null) {
nameInputField.setText(getArguments().getString("name"));
tagInputField.setText(getArguments().getString("tag"));
}
}
The null-check for getArguments() has been added because I sometimes need to call this dialogFragment without filling the editText fields. When this happens, it means there are no arguments set to the fragment, which would result in a NPE when calling getArguments().getString("String");
That should be it. I do however have a reputation for being stupid, so it might very well be possible I overlooked something, that should be included in this explanation. If so, please let me know, so I can add it.
What I make of this error, is that nameTextView and tagTextView are
null at the moment that I'm trying to set text to them, which makes me
think the method gets called before the fragment is created.
That is almost correct. The Fragment is created but onCreateDialog() has not been called yet because showing the dialog happens asynchronously after the call to show(...).
You can fix this by adding the two preset Strings as arguments to the DialogFragment.
You can then assign the EditText fields the correct value directly in the onCreateDialog method by using getArguments() on the DialogFragment.
Here is what this looks like:
public MyDialogFragment extends DialogFragment {
private static final String ARG_NAME = "name";
private static final String ARG_TAG = "tag";
public MyDialogFragment newInstance(String name, String tag) {
Bundle args = new Bundle();
args.putString(ARG_NAME, name);
args.putString(ARG_TAG, tag);
MyDialogFragment frag = new MyDialogFragment();
frag.setArguments(args);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View v_iew = inflater.inflate(R.layout.fragment_inputdialog, null);
nameInputField = (EditText) v_iew.findViewById(R.id.inputdialogname);
tagInputField = (EditText) v_iew.findViewById(R.id.inputdialogtag);
nameInputField.setText(getArguments().getString(ARG_NAME, ""));
tagInputField.setText(getArguments().getString(ARG_TAG, ""));
}
}
Then the new click event handler.
editButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
stuffManagerInputDialogFragment = MyDialogFragment
.newInstance(nameTextViewContent, tagTextViewContent);
stuffManagerInputDialogFragment.show(getFragmentManager(), "TEST");
}
});
Don't try to manipulate your dialog immediately after calling show.
If you want to set default values for fields within your dialog, pass them to the dialog as arguments and initialise them during the dialog creation.
e.g
public class MyFragment extends Fragment {
/**
* Static factory method that takes an int parameter,
* initializes the fragment's arguments, and returns the
* new fragment to the client.
*/
public static MyFragment newInstance(int index) {
MyFragment f = new MyFragment();
Bundle args = new Bundle();
args.putInt("index", index);
f.setArguments(args);
return f;
}
}
from: http://www.androiddesignpatterns.com/2012/05/using-newinstance-to-instantiate.html

Android: Can't access static elements outside Fragment class

I am trying to change elements such TextViews etc. that are parts of the Fragment (which is used for SlidingTabLayout). I can access TextView from the Tab1 class:
public class Tab1 extends Fragment {
public static TextView serverName;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_1,container,false);
serverName = (TextView) view.findViewById(R.id.serverName);
serverName.setText("This works, but I can't change text from outside the Tab1 class");
return view;
}
But when I want access the serverName TextView from anywhere I am always getting null value. Here I am trying to change the text from the activity which contains Sliding Tabs (Tab1 is a part of it):
public class Dashboard2 extends AppCompatActivity {
Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence tabsTitles[] = {"Info", "Options"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard2);
InitializeToolbarAndTabs();
Tab1.serverName.setText("This doesn't work");
}
private void InitializeToolbarAndTabs()
{
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabsTitles, tabsTitles.length);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
tabs = (SlidingTabLayout) findViewById(R.id.tabs);
tabs.setDistributeEvenly(true);
tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return getResources().getColor(R.color.tabsScrollColor);
}
});
tabs.setViewPager(pager);
}
}
Logs from the Android Studio:
java.lang.RuntimeException: Unable to start activity ComponentInfo{ASD.Dashboard2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3119)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
at android.app.ActivityThread.access$1000(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at ASD.Dashboard2.onCreate(Dashboard2.java:54)
at android.app.Activity.performCreate(Activity.java:6500)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1120)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3072)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3218)
at android.app.ActivityThread.access$1000(ActivityThread.java:198)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1676)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6837)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference - how to solve this problem?
Not sure what you are doing but you can always find your fragment by FragmentManage.findFragmentByTag() or FragmentManager.findFragmentById().
Once found just access your field.
I don't think you have initialized the Tab1 fragment, at least I can't see it there.
Accessing fragment variables from an Activity through static declarations is a horrible idea, use voids in the fragment class.
I am sorry, the error you are getting is not related with accessing or not a static object of the Fragment. The error you are receiving is because at the moment you call Tab1.serverName.setText("This doesn't work"); your fragment still didnt inflate the view or still didn't charge the TextView (didn't arrive yet to serverName = (TextView) view.findViewById(R.id.serverName);). The fragments are charged into the view in a asynchrounous way, so even if you declare it in your layout as a tag, it may be possible that the Fragment's view is still not fully loaded.
If you absolutely want to be sure the fragment's view is fully loaded, use the protected void onResumeFragments() method:
#Override
protected void onResumeFragments() {
super.onResumeFragments();
Tab1.serverName.setText("This doesn't work");
}
Anyway, I strongly recommend you NOT to access fragment objects statically, but to use the findFragmentById() or findFragmentByTag() methods and then to access a public method inside the given fragment.
Hope it helps.

Adding methods after fragments

I have a fragment that is a "timer" that I can add anywhere. In the fragment I change a textView programatically, and it runs beautifully. My problem is when it comes to using a view from the layout inflated by the constructor(? Not sure if that's the right terminology) in another method below it.
public class Timer_fragment extends android.support.v4.app.Fragment {
int testpins;
String testedpin;
TextView text;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.timer_frag, container, false);
TextView text = (TextView) v.findViewById(R.id.pwd_status);
text.setText("Setting text in fragment not main");
/* set the TextView's text, click listeners, etc. */
updateStatus();
return v;
}
All of that code works with no errors but when I try to add this method:
private void updateStatus() {
TextView text = (TextView) findViewById(R.id.pwd_status);
testPin();
text.setText(testedpin);
}
I get a red line under findViewById saying The method findViewById(int) is undefined for the type Timer_fragment.
I thought about inflating the view in all of my methods and not returning them, but surely that would affect performance somehow right?
Just tried inflating the layout before using the view but I get an error on the word inflater and container saying that they can't be resolved.
Am I going about this correctly?
You already have a member variable in the scope of your Fragment called text. Don't re-declare it in your methods, just assign it.
text = (TextView) v.findViewById(R.id.pwd_status);
and
private void upateStatus() {
testPin();
text.setText(testedpin);
}
The method 'findViewById' is provided by the activity. While this class extends Fragment, you will not have access to activity related method calls unless you provide the activity to the fragment. Check out: http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
Basically, either pass in the instance of the activity to the Timer_fragment:
private final Activity _activity;
Timer_fragment(Activity activity)
{
_activity = activity;
}
...
private void updateStatus()
{
TextView text = (TextView) _activity.findViewById(R.id.pwd_status);
testPin();
text.setText(testedpin);
}
Or set the text of the view from within whichever activity is being used, and not from within the timer class.
Just replace findViewById with getActivity().findViewById.
findViewById method is defined inside the Activity class. Fragments aren’t activites. But the fragment can get a reference to the Activity that added it to a screen using the method getActivity.

Categories