I'm trying to create a fragment class following from this:
And they give you the code for a fragment class, but I'm getting
ExampleFragments.java
package com.example.learn.fragments;
public static class ExampleFragments extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.blue_pill_frag, container, false);
}
}
But I'm getting an error on line 9, where I declare the name of the class.
Illegal modifier for the class ExampleFragments; only public, abstract & final are permitted
I'm sure it's something basic that I'm not understanding, thanks.
You can't have a static top-level class. Change
public static class ExampleFragments extends Fragment {
to
public class ExampleFragments extends Fragment {
In the example you're following, the static modifier is used because the Fragment is a nested class.
To get rid of the error you can include that fragment into an existing class, or remove the static modifier.
Related
Here i implemented a emoji-keyboard.
For the use of this library , activity extended to EmojiCompatActivity.
prepareKeyboard(EmojiCompatActivity activity, EmojiEditText input
This is working well in activity. While using in fragment i tried this one
prepareKeyboard((ActivityName)getActivity,input)
Here is my MainActivity
public class MainActivity extends EmojiCompatActivity {}
And fregment class is
public class PagerEmojKeyboard extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.emoji_keyboard, container, false);
EmojiEditText userMessageInput = getActivity().findViewById(R.id.input_message);
EmojiKeyboardLayout emojiKeyboardLayout=(EmojiKeyboardLayout) v.findViewById(R.id.keyboard_emoj);
emojiKeyboardLayout.prepareKeyboard((MainActivity) this.getActivity(),userMessageInput);
return v;
}
}
You can refer to the base activity as this.activity.
Sometimes, while using fragments, the above method also works.
If you are working in the method onCreateView you shouldn't have problems but if you have other method there is a View variable that you must have as a global for use throughout the class.
I'm new here so please bear with me.
I am trying to set text in a textview from the onClick method of a fragment but I keep getting this error:
Non-static field 'textView' cannot be referenced from a static context
Everything I've tried hasn't worked so I'm asking for help.
I have this fragment that has a button that I want to set the text of a textView:
PullFragment.java
public class PullFragment extends Fragment implements View.OnClickListener {
public PullFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_pull, container, false);
//The button
ImageButton toolbarButtonPull;
toolbarButtonPull = view.findViewById(R.id.toolbarButtonPull);
toolbarButtonPull.setOnClickListener(this);
return view;
}
#Override
public void onClick (View v) {
switch (v.getId()) {
case R.id.toolbarButtonPull:
// Here is the button onClick event
break;
}
}
}
This is the other fragment that has the textView:
PlaceholderFragment.java
public class PlaceholderFragment extends Fragment {
public TextView textView;
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_pull, container, false);
// Here is the textView I want to change
textView = rootView.findViewById(R.id.textView);
return rootView;
}
}
I have tried the following in the onClick event:
PlaceholderFragment.textView.setText("Test");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment.Test();
Non-static field 'textView' cannot be referenced from a static context
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.Test();
Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
Maybe I'm missing something simple but I haven't made any progress on this.
Google suggests using interfaces for communication between fragments, see documentation: https://developer.android.com/training/basics/fragments/communicating
Also, I think this is a good tutorial, using activity's shared ViewModel for transferring data between fragments.
The first problem:
I have tried the following in the onClick event:
PlaceholderFragment.textView.setText("Test");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment.Test();
Non-static field 'textView' cannot be referenced from a static context
The error Non-static field 'textView' cannot be referenced from a static context is to be expected because you can't access a variable or a method of a class without instantiating the object. Except if you make it a static variable or a static method. So, you need to make them static like this:
public class PlaceholderFragment extends Fragment {
public static TextView textView;
public PlaceholderFragment() {}
...
public static void test() {
...
}
}
The second problem:
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");
//Or trying to call public void Test which just does "textView.setText("Text");"
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.Test();
Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null
object reference
The error:
Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference is also to be expected. It is because when you're calling the following:
PlaceholderFragment placeholderFragment = new PlaceholderFragment();
placeholderFragment.textView.setText("Text");
the placeholderFragment is not yet fully instantiated. It's because the Fragment creation is not a synchronous process. So, when you're calling the placeholderFragment.textView.setText("Text"); the textView is not yet created and binded to the fragment. You need to set the text via arguments or wait until the fragment is fully created.
I am trying to develop an android app in android studio and I keep writing more and more fragments in the mainActivity class. My question is how to separate these to another file? Probably I am doing it in a wrong way and if so could somebody show me how should I do it?
My code:
public class MainActivity extends ActionBarActivity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
.
.
.
public static class PlaceholderFragment1 extends Fragment {...}
public static class PlaceholderFragment2 extends Fragment {...}
public static class PlaceholderFragment3 extends Fragment {...}
}
Since they are static inner classes, AndroidStudio can easily refactor these for you. Select PlaceholderFragment1 (just put the text cursor on it) and press F6 (or right click the fragment name->refactor->move) and select `Move inner class [fragment name] to upper level', change the name and package if you want and hit refactor.
Having a static inner class for a Fragment is fine (will work technically), but if you want to reuse the fragment in another activity, best to refactor it out. Also, most people like to keep classes as small as possible, and if the function of the fragment is logically separate from the activity there is little reason to keep it as an inner class.
You could without problem write them as separate classes in the same package and then just use them. Otherwise write them in a separate package and import them as any other class.
First you will need to create a new class (new class file)
NOTE: You will ned to create a class like this for every fragment you have to define the fragment logic.
public class MyFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.article_view, container, false);
}
//logic here
}
Then you can use it in your MainActivity which extends FragmentActivity like:
MyFragment fragment= new MyFragment();
or if exists
fragment= (MyFragment)getSupportFragmentManager().findFragmentByTag(FRAGMENT_TAG);
You can set your fragments to view with:
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frameLayout, fragment, FRAGMENT_TAG);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
I'm within a fragment calling a subroutine from another class which contains getActionBar.removeTab() but as I'm within a fragment the method I'm calling needs to be static but getActionBar cannot be contained within a static method. Which leaves me a little out of my depth. Any ideas?
Purpose
The point of the below code is so within the fragment I can remove tabs when neccersy from the class OtherClass
Code:
public class Example extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
OtherClass.SubWhichWontWork(); }
public class OtherClass extends FragmentActivity implements
ActionBar.TabListener {
public void SubWhichWontWork() throws ClientProtocolException, IOException {
if (blabla > other){
getActionBar().removeTabAt(numtabbar);}
}
Since OtherClass extends Activity and with assumption that Example fragment is used inside OtherClass you can use ((OtherClass)getActivity()).SubWhichWontWork(); inside onCreateView of Example class
I'm having a very weird problem. I have asked my course mates about it, but they said that they do not know why this is happening.
This is my main class with a method that returns type Fragment:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
//....
public Fragment getItem(int position) {
switch(position){
case 0:
return new AccelerometerMonitorFragment();
}
return null;
}
//...
}
Now, if I put class "AccelerometerMonitorFragment" inside my main class as an inner class:
public static class AccelerometerMonitorFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//do something
return null;
}
}
This will work fine, no errors. But I would like to have it in separate file, for cleanness purposes.
So I create a separate class file, delete the inner class, and try to use it.
package com.example.actrecognition;
//imports...
public class AccelerometerMonitorFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// do something
return null;
}
}
But then I get this error:
Type mismatch: cannot convert from AccelerometerMonitorFragment to Fragment
I do not understand it, because the class in the separate file extends Fragment, so what is the problem? Why having the class in external file and not static breaks the code?