I am doing a project on android studio. The problem is I cannot add a fragment class to my project. I was able to add two fragment classes when I started doing this project. But, after completing about half of my project, now I cannot add a third fragment class. When I add a third fragment class and run my project, this exception pops up:
java.lang.NoSuchMethodError: No static method zzb(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzaa; or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' appears in /data/data/com.hashcoder.eegoomain/files/instant-run/dex/slice-com.google.android.gms-play-services-basement-10.0.1_d1c25087bea669c827e484b09b1949315ba5fc5f-classes.dex)
at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5172)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4767)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4707)
at android.app.ActivityThread.access$1600(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
And I tried both android.app.Fragment or android.support.v4.app.Fragment. Both doesn't work.....
Try this.Make a java class and extends it from Fragment.
This is the basic structure of a fragment class.
public class MyFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.<your xml layout>, container, false);
//Do your work here
return view;
}
}
and make sure you extends it from android.support.v4.app.Fragment
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 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.
As the question reads, Fragments in android are View or ViewGroup. Can anyone explain
Here is the onCreateView method of Fragment from docs
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
The container parameter passed to onCreateView() is the parent
ViewGroup (from the activity's layout) in which your fragment layout
will be inserted
And
To draw a UI for your fragment, you must return a View from this
method that is the root of your fragment's layout.
You cannot define fragments as views or viewgroups.Fragments are more than that.The easiest way to visualize fragment is to think of them as chunks of an activity having own xml appearance,own behaviour with own life cycle callbacks.They always run on top of an activity allowing you to perform operations such as add,replace etc on them at run time.This way you can switch between your layouts effectively and efficiently.
To understand onCreateView method,consider the following explanation:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view =inflater.inflate(R.layout.example_fragment, container, false);
return view;
}
this returns a single View object, always a ViewGroup, with the set of View object that defines the Fragment's UI. The Activity calls this event handler when it is time for the Fragment to provide its UI for display.
Neither. Fragment is a base class.
From https://developer.android.com/guide/components/fragments.html
A Fragment represents a behavior or a portion of user interface in
an Activity
Fragment contains a field:
// The View generated for this fragment.
View mView;
that is generated in onCreateView which has the implementation:
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
Bundle savedInstanceState) {
return null;
}
So if you want a Fragment with a View then #Override that method. And then the Fragment can be shown to the user if you use the appropriate fragment transaction from an Activity or nested Fragment.
fragment is a wrapper over a custom view thats tied to host activity lifecycle.
Fragments are a kind of Activity that don't take full screen,it can just split the screen into many fragments,so it is well used for phone and tablet .
i think is no,activity(fragment) control what the view shows,viewGroup extends view and implements ViewManger.Only Activity loads related Views can the phone show the layout.Activity is a Group,view can work normally in this container
my name is Augusto, I'm developing a Messaging/Social app for Android on Android Studio and I implemented SlidingTabLayout (com.google.samples.apps.iosched.ui.widget) with 2 tabs (Chats and Friends ).
I wish to know how to access the views inside the Tabs' xml files on my activity.
I have:
MainActivity.class with activity_main.xml as contentview and the tabs's layouts(friendsview.xml and chatsview.xml) for the tabs.
I want to access tabs' layouts view (e.g: EditText, ImageView) from MainActivity.class, How do I "import" them to the activity?
Thanks!
SlidingTabLayout it's only a layout. To manage content use fragments with FragmentPagerAdapter for your tabs.
Here (link) is nice tutorial for you.
If you follow this example tutorial, you will have
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
and to access EditText and other widget inside tab, you need to have reference from view, so change above code to:
public class Tab1 extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.chatsview,container,false);
editText = v.findViewById(R.id.YOUR_EDIT_TEXT_ID);
return v;
}
}
Below code shows null pointer exception..
public class MyFragment extends Fragment {
private String[] eArray = getActivity()
.getResources()
.getStringArray(R.array.English);
What are the other options, searching in stackoverflow did not give me success..
I am using FragmentPagerAdapter also.. which is not related to this eArray.. but it contains data for images.
getActivity() is returning null because you are using it before fragment get attached to Activity.
You need to initialize this at onCreateView
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
eArray = getActivity().getResources().getStringArray(R.array.English);
}