EditTextPreference crashes on requestFocus() - java

I am migrating from a PreferenceActivity to a PreferenceFragment (v14) and when I try to tap on my EditTextPreference the app crashes.
The code worked with the PreferenceActivity, which is why I don't really understand why it throws a NullPointerException.
Here is the important part of the code:
private Preference websitePref;
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.preferences, rootKey);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.white));
websitePref = findPreference(getString(R.string.preference_key));
websitePref.setIconSpaceReserved(false);
final SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
final String savedWebsite = sharedPrefs.getString(websitePref.getKey(), "");
websitePref.setSummary(savedWebsite);
...
I tried changing the Preference to EditTextPreference and casting it, but I still get the same error:
10-05 13:55:36.694 22397-22397/de.test.browserelement E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.test.browserelement, PID: 22397
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.widget.EditText.requestFocus()' on a null object reference
at android.support.v14.preference.EditTextPreferenceDialogFragment.onBindDialogView(EditTextPreferenceDialogFragment.java:66)
at android.support.v14.preference.PreferenceDialogFragment.onCreateDialog(PreferenceDialogFragment.java:149)
at android.app.DialogFragment.onGetLayoutInflater(DialogFragment.java:410)
at android.app.Fragment.performGetLayoutInflater(Fragment.java:1435)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2422)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2201)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2155)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2056)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:719)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Here is my EditTextPreference in my preferences.xml:
<EditTextPreference
android:defaultValue="#string/preference_default"
android:key="#string/preference_key"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="#string/preference_title"
android:dependency="#string/sperr_key"
android:inputType="textVisiblePassword" />

I had the same problem. For some reason it does not set the dialog layout by default.
Try casting it to EditTextPreference and add this line of code:
websitePref.setDialogLayoutResource(R.layout.preference_dialog_edittext);

Related

Nested Fragments with MapView

I have a fragment that contains a MapView and it works by itself. When I launch that same fragment inside another fragment the app crashes.
My app crashes after mapView.onCreate(null) with the following stack. However it only crashes when it is nested in another fragment.
java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.view.ViewGroup
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1282)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
at android.app.FragmentManagerImpl.dispatchMoveToState(FragmentManager.java:3051)
at android.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2998)
at android.app.Fragment.performActivityCreated(Fragment.java:2537)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1318)
at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2426)
at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2205)
at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2161)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2062)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:738)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6649)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
This is my fragment which works by itself
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_map_view, container, false);
setup();
return view;
}
private void setup() {
mapView = view.findViewById(R.id.mapViewFragment);
mapView.onCreate(null);
mapView.onResume();
MapsInitializer.initialize(this.getActivity().getApplicationContext());
}
and this is how I am nesting the fragments
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.frameLayoutBuffer, new MapViewFragment()).commit();
The id is from a framelayout.
If I change to another Fragment, the other fragment displays just fine, so it something to do with the MapView.
Both fragments also do not contain any ImageView, any help about this problem will be appreciated.
I fixed by calling the setup method inside the onResume() and changing view.findViewById() to getView().findViewbyId()
Edit: Actually that did not fix the problem. I added a delay before creating the map in another thread, and that seemed to fix.

Cannot setText() of an EditText field (Android) - NullPointerException [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
textView setText() NullPointerException
(3 answers)
Closed 5 years ago.
When I try to setText() on an EditText I keep getting this error, I can't see where I'm going wrong. I'd like to understand the logic behind this error. I'm sure it's something small but it's really bugging me now.
01-25 10:20:44.267
14325-14325/com.example.gregsquibbs.greg_squibbs_todoapp3
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.gregsquibbs.greg_squibbs_todoapp3, PID: 14325
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.gregsquibbs.greg_squibbs_todoapp3/com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.EditText.setText(java.lang.CharSequence)' on a null
object reference
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.EditText.setText(java.lang.CharSequence)'
on a null object reference
at
com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoFragment.setMessage(EditTodoFragment.java:56)
at
com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity.onCreate(EditTodoActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6975)
at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6541) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This is where I create the etMessage and use setText()
public class EditTodoFragment extends Fragment implements EditTodoContract.View {
private EditTodoContract.Presenter presenter;
private EditText etMessage;
public EditTodoFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.edit_todo_fragment, container, false);
System.out.println("Step 2 - Fragment check");
etMessage = (EditText) view.findViewById(R.id.etMessage);
return view;
}
public void setPresenter(EditTodoContract.Presenter todoPresenter) {
this.presenter = todoPresenter;
}
public String getMessage() {
String message = etMessage.getText().toString();
return message;
}
public void setMessage(String message) {
etMessage.setText(message);
}
}
Here is the XML file
<FrameLayout 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="com.example.gregsquibbs.greg_squibbs_todoapp3.EditTodo.EditTodoActivity">
<EditText
android:id="#+id/etMessage"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/rect"
android:ems="10"
android:gravity="top"
android:inputType="textShortMessage" />
<Button
android:id="#+id/bSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:onClick="saveButtonClicked"
android:layout_gravity="center_horizontal|bottom"
/>
Here is where setMessage() gets called in the activity
public class EditTodoActivity extends AppCompatActivity {
private String message;
private int position;
private EditTodoFragment editTodoFragment;
private EditTodoPresenter editTodoPresenter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_todo_activity);
System.out.println("Step 1");
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.contentFrameContainer);
if (fragment == null){
editTodoFragment = new EditTodoFragment();
fm.beginTransaction()
.add(R.id.contentFrameContainer, editTodoFragment)
.commit();
editTodoPresenter = new EditTodoPresenter(editTodoFragment);
}
message = getIntent().getStringExtra(IntentConstants.INTENT_MESSAGE_DATA);
position = getIntent().getIntExtra(IntentConstants.INTENT_ITEM_POSITION, -1);
editTodoFragment.setMessage(message);
}
public void saveButtonClicked(View v) {
if(editTodoFragment.getMessage().equals("")) {
} else {
String changedMessage = (editTodoFragment.getMessage());
Intent intent = new Intent();
intent.putExtra(IntentConstants.INTENT_CHANGED_MESSAGE, changedMessage);
intent.putExtra(IntentConstants.INTENT_ITEM_POSITION, position);
finish();
}
}
}
Edit: On this line in the fragment
etMessage = (EditText) view.findViewById(R.id.messageField);
When I hover over the casting (EditText) it says
Casting 'view.findViewById(R.id.messageField)' to 'EditText' is redundant less... (⌘F1)
This inspection reports unnecessary cast expressions.
I don't know whether that has anything to do with it or not.
There is no garrenty that editTodoFragment.setMessage(message); will be called after your EditText initialized by this line
etMessage = (EditText) view.findViewById(R.id.etMessage);
In your case your code is setting text to EditText before it's initialized.
Best practice is send data via Bundle to Fragment and set Text after EditText is initialized.
Check this answer
Sample Code:
In EditTodoActivity
message = getIntent().getStringExtra(IntentConstants.INTENT_MESSAGE_DATA);
if (fragment == null){
editTodoFragment = new EditTodoFragment();
//
Bundle bundle = new Bundle();
bundle.putInt(key, message);
fragment.setArguments(bundle);
fm.beginTransaction()
.add(R.id.contentFrameContainer, editTodoFragment)
.commit();
editTodoPresenter = new EditTodoPresenter(editTodoFragment);
}
In EditTodoFragment onCreate
Bundle bundle = this.getArguments();
if (bundle != null) {
message = bundle.getString(key, defaultValue);
}
In EditTodoFragment onCreateView
etMessage = (EditText) view.findViewById(R.id.etMessage);
etMessage.setText(message);
Probably the error is that you have defined etMessage in other file than edit_todo_fragment. That's why you are not getting a compilation error, since the reference to etMessage exists, but it does not exists in your inflated view so thus you got the NPE.
As I understood from logs you're calling setMessage method from onCreate method of your activity. But the problem is Activity's onCreate is called earlier than Fragment's onCreateView, so your EditText is not initialized at that moment.
first you have to define this EditText belong to witch view
then, after you inflate your view you should do this.
View view = inflater.inflate(R.layout.edit_todo_fragment, container, false);
etMessage = view.findViewById(R.id.etMessage);
etMessage.setText("your message...")
if you call set text before it, you see this error
or
this view (etMessage) is not in this layout (edit_todo_fragment)

Exception when referencing MapView in a fragment - OSMdroid

I have written an app with OSMdroid using activities, but I am now trying to port it over to fragments instead (I'm new to fragments though). I am getting the error:
"java.lang.NullPointerException: Attempt to invoke virtual method 'void org.osmdroid.views.MapView.setBuiltInZoomControls(boolean)' on a null object reference"
It seems that the MapView has not yet been initialised, am I initialising in the wrong place (OnCreateView)? According to the activity lifecycle, OnCreate is called before OnCreateView, so it would make sense that it is not recognised, but I am confused as to where then to put my code.
Code for my implementation of the fragment:
//inflating fragment layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_map, container, false);
map = (MapView) view.findViewById(R.id.map);
return view;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));
setupMap();
}
//initializing map
private void setupMap() {
//adding zoom and touch controls
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
//getting current location using coarse/fine location so we can set centerpoint
currentLocation = getCurrentLocation();
... code continues ...
Error stack trace:
java.lang.NullPointerException: Attempt to invoke virtual method 'void org.osmdroid.views.MapView.setBuiltInZoomControls(boolean)' on a null object reference
at skicompanion.skicompanion.MapFragment.setupMap(MapFragment.java:101)
at skicompanion.skicompanion.MapFragment.onCreate(MapFragment.java:86)
at android.app.Fragment.performCreate(Fragment.java:2214)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1153)
at android.app.BackStackRecord.run(BackStackRecord.java:800)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:487)
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:5765)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
In your posted code it doesn't look like you need to override onCreate method in your case. just move this code:
context = getActivity();
Configuration.getInstance().load(context, PreferenceManager.getDefaultSharedPreferences(context));
setupMap();
into the onCreateView method before the return call and should be ok.

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));

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.

Categories