I’m trying to use a tabbed activity within a fragment, so a fragment with two other fragments. I keep getting the NullPointerException for the viewPager object, which has already been mapped by the right id. Whats gone wrong ?
public class BookFragment extends Fragment {
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_books, container, false);
PageAdapter pageAdapter = new PageAdapter(getChildFragmentManager(), 1);
pageAdapter.addFragment("Pending", new PendingFragment());
pageAdapter.addFragment("Scheduled", new ScheduledFragment());
ViewPager viewPager = view.findViewById(R.id.pager_books);
viewPager.setAdapter(pageAdapter);
TabLayout tabs = view.findViewById(R.id.book_tabs);
tabs.setupWithViewPager(viewPager);
return view;
}
}
The fragment Layout (fragment_books.xml) being inflated;
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".views.bookhub_navigation.ui_fragments.books.BookFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/book_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:id="#+id/pending_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pending" />
<com.google.android.material.tabs.TabItem
android:id="#+id/scheduled_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scheduled" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/pager_books"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
It keeps giving a NullPointerException when the id pager_books is in the right view heirarchy which is the fragment_books.xml file.
The error log
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager.widget.ViewPager.setAdapter(androidx.viewpager.widget.PagerAdapter)' on a null object reference at com.immers_ltd.views.bookhub_navigation.ui_fragments.books.BookFragment.onCreateView(DateFragment.java:58) at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.addAddedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:237) at android.app.ActivityThread.main(ActivityThread.java:7948) 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:1075)
Related
I have created multi tabs app in android , But I can't refer to my Xml elements in the class in picture , I have tried solution here findViewById in Fragment but it didn't work for me
here is my xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CountDown">
<EditText
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="101dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="101dp"
android:layout_marginBottom="450dp"
android:autofillHints="time will be here"
android:inputType="time"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.45"
tools:ignore="Autofill,LabelFor"
tools:text="time" />
</androidx.constraintlayout.widget.ConstraintLayout>
here is my main activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
here is my main activity xml
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="#dimen/appbar_padding"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Try like below code.
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View frag = inflater.inflate(R.layout.count_down, container, false);
EditText time = frag.findViewById(R.id.time);
return frag;
}
I was trying to add a collapsing Toolbar to a Fragment, but the app crashed as soon as I opened the Fragment:
This is the error I get:
2018-10-19 18:05:38.950 13301-13301/io.github.davidwickerhf.diceroller E/AndroidRuntime: FATAL EXCEPTION: main
Process: io.github.davidwickerhf.diceroller, PID: 13301
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at io.github.davidwickerhf.diceroller.ProfileFragment.onCreateView(ProfileFragment.java:36)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:809)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7436)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
Fragment Java:
public class ProfileFragment extends Fragment {
private MainActivity mMainActivity;
private SettingsViewModel settingsViewModel;
private SettingAdapter adapter;
private CoordinatorLayout coordinatorLayout;
private View fragmentView;
private TabLayout mTabLayout;
private ViewPager mViewPager;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fragmentView = inflater.inflate(R.layout.fragment_profile, container, false);
mMainActivity = new MainActivity();
androidx.appcompat.widget.Toolbar mToolbar = fragmentView.findViewById(R.id.profile_toolbar);
mMainActivity.setSupportActionBar(mToolbar);
mViewPager = (ViewPager) fragmentView.findViewById(R.id.viewpager);
ViewPagerAdapter adapter = new ViewPagerAdapter(mMainActivity.getSupportFragmentManager());
adapter.AddFragment(new ProfileInfoFragment(), "Profile");
adapter.AddFragment(new ProfileMessagingFragment(), "Messages");
mViewPager.setAdapter(adapter);
mTabLayout = (TabLayout) fragmentView.findViewById(R.id.tab_layout_profile);
mTabLayout.setupWithViewPager(mViewPager);
return fragmentView;
}
}
The Fragment XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ProfileFragment"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme"
app:toolbarId="#id/profile_toolbar">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:titleEnabled="false">
<ImageView
android:id="#+id/background_image_Toolbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
<androidx.appcompat.widget.Toolbar
android:layout_marginTop="24dp"
android:id="#+id/profile_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/AppTheme" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
android:id="#+id/tab_layout_profile"
app:tabTextColor="#color/greyText"
app:tabIndicatorColor="#color/greyText"
android:background="#fafafa"
android:elevation="0dp">
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewpager">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.viewpager.widget.ViewPager>
Please help?
I guess you are getting that error because you are instantiating a new MainActivity object:
mMainActivity = new MainActivity();
In case of Fragments, you can call getActivity() to get the reference of the activity that is hosting your Fragment. Something like:
private MainActivity mMainActivity;
....
mMainActivity = (MainActivity) getActivity();
Or, you don't even need to cast to MainActivity since you are invoking a common method (available on Activity)
private Activity mMainActivity;
....
mMainActivity = getActivity();
I am trying to replace the content of frame_layout with a view of fragment.
I used to do that by just using transaction.replace but it just put the view on the top of the main content.
Here is the code:
That's where I replace the frame_layout whose ID is contain:
VideosFragment videosFragment = new VideosFragment();
Bundle bundle = new Bundle();
bundle.putCharSequence("Character", view.getContentDescription());
videosFragment.setArguments(bundle);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.contain , videosFragment).addToBackStack(null).commit();
here is the xml of main_Activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.shobakyDC.muchannel.MainActivity">
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:paddingStart="50dp"
android:paddingTop="10dp"
android:verticalSpacing="10dp"
android:paddingRight="0dp"
android:paddingLeft="50dp"
android:paddingEnd="0dp">
</GridView>
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/Navigator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/header"
android:layout_gravity="start"
android:background="#android:color/white"
app:itemTextColor="#color/navcolor"
app:itemIconTint="#color/navcolor"
app:menu="#menu/navigationmenu">
</android.support.design.widget.NavigationView>
the video_fragment:
public class VideosFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.videofragment_layout , container , false);
Bundle args = getArguments();
TextView textView = view.findViewById(R.id.textView);
textView.setText(args.getString("Character"));
return view;
}
}
In xml of Main Activity, you have to make an empty FrameLayout with an ID like this:
<FrameLayout
android:id ="#+id/contain"
android:layout_width="match_parent"
android:background="#drawable/islamic_green"
android:layout_height="match_parent">
</FrameLayout>
and then make a separate xml layout for this fragment. Now you have to replace this fragment layout with frame layout in Main Activity.
I am trying to use recycler view but eclipse giving error:...my support libraries are up-to-date :
This is Error:
here is my layout that adding recycler-view :.
//part of xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:widget="http://schemas.android.com/apk/res-auto"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:id="#+id/playeramainlayout"
android:background="#color/black">
<LinearLayout
android:id="#+id/hf "
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:background="#color/black"
android:gravity="center" >
<!-- Player Buttons -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:background="#layout/rounded_corner"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<!-- Previous Button -->
<ImageButton
android:id="#+id/btnPrevious"
android:src="#drawable/btn_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"/>
<!-- Play Button -->
<ImageButton
android:id="#+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/btn_play" />
android:background="#null"/>
<!-- Next Button -->
<ImageButton
android:id="#+id/btnNext"
android:src="#drawable/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"/>
</LinearLayout>
</LinearLayout>
<!-- Progress Bar/Seek bar -->
<!-- Timer Display -->
<FrameLayout
android:id="#+id/imageviewkalayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/player_header_bg"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp" >
<android.support.v7.widget.RecyclerView
android:id="#+id/HSVfoalbumarinplayeractivity"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
<ImageButton
android:id="#+id/btnRepeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:background="#null"
android:src="#drawable/btn_repeat" />
<ImageButton
android:id="#+id/btnShuffle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:background="#null"
android:src="#drawable/btn_shuffle" />
<ImageButton
android:id="#+id/equaliser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:background="#null"
android:src="#drawable/btn_repeat" />
</FrameLayout>
<LinearLayout
android:id="#+id/timerDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/songProgressBar"
android:layout_centerHorizontal="true"
android:background="#color/black" >
<TextView
android:id="#+id/songCurrentDurationLabel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:background="#color/black"
android:textColor="#eeeeee"
android:textStyle="bold" />
<TextView
android:id="#+id/songTotalDurationLabel"
android:layout_width="155dp"
android:layout_height="match_parent"
android:background="#color/black"
android:gravity="right"
android:textColor="#eeeeee"
android:textStyle="bold" />
</LinearLayout>
<SeekBar
android:id="#+id/songProgressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ hf "
android:layout_alignParentLeft="true"
android:background="#color/black"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:progressDrawable="#drawable/seekbar_progress"
android:thumb="#drawable/seek_handler" />
<RelativeLayout
android:id="#+id/player_header_bg"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:layout_above="#+id/timerDisplay"
android:layout_alignParentLeft="true"
android:background="#color/black"
android:paddingLeft="5dp"
android:paddingRight="5dp" >
<TextView
android:id="#+id/songTitle"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_weight="1"
android:gravity="center"
android:singleLine="true" />
<TextView
android:id="#+id/songartist"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:gravity="center"
android:singleLine="true" />
</RelativeLayout>
</RelativeLayout>
Here is the code oof initialising view that is adding recycler view to activity :
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
HSVforimageGallery=(RecyclerView) findViewById(R.id.HSVfoalbumarinplayeractivity);
HSVforimageGallery.setLayoutManager(new LinearLayoutManager(this));
HSVforimageGallery.setHasFixedSize(true);
}
Here is logcat :
12-28 21:40:32.055: E/AndroidRuntime(25525): FATAL EXCEPTION: main
12-28 21:40:32.055: E/AndroidRuntime(25525): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.musicplayer/com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity}: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: android.view.InflateException: Binary XML file line #113: Error inflating class android.support.v7.widget.RecyclerView
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.app.Activity.setContentView(Activity.java:1881)
12-28 21:40:32.055: E/AndroidRuntime(25525): at com.androidhive.musicplayer.AndroidBuildingMusicPlayerActivity.onCreate(AndroidBuildingMusicPlayerActivity.java:114)
12-28 21:40:32.055: E/AndroidRuntime(25525): Caused by: java.lang.NoClassDefFoundError: android.support.v7.recyclerview.R$styleable
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:481)
12-28 21:40:32.055: E/AndroidRuntime(25525): at android.support.v7.widget.RecyclerView.<init>(RecyclerView.java:450)
12-28 21:40:32.055: E/AndroidRuntime(25525): ... 26 more
I tried lots of things like adding support project of recycler-view and referencing to main project etc .
But none of the thing worked .
Any help will be appreciated :).
In recent versions of the support library (22.x), the CardView and RecyclerView libraries were provided with only Android Studio (Gradle) integration, so you needed to manually create Eclipse library projects in order to use them in Eclipse.
As of v23.01 of the Support v7 library, however, there are now library projects included in the sdk.
First, make sure that you have at least v23.01 of the Support Library:
Then, navigate to the location of the Support Library in the sdk:
Copy the recyclerview folder to your project, I created a folder called "dependency" and placed all library projects in there.
Then, import your library project into Eclipse, along with your app project:
For each library project, ensure that Is Library is checked:
Then ensure that all library projects are added to your app project:
And, ensure that all library projects are included in the build path of your app project:
Now, your Eclipse project should be configured correctly.
In this simple example, I used a RecyclerView and CardViews.
Here is the Fragment code:
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"testone", "testtwo", "testthree", "testfour"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
}
The Adapter for the RecyclerView:
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private String[] mDataset;
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class MyViewHolder extends RecyclerView.ViewHolder {
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v) {
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(String[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_item, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() {
return mDataset.length;
}
}
fragment_blank.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
card_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp" >
<android.support.v7.widget.CardView
android:id="#+id/card_view"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
</TextView>
</android.support.v7.widget.CardView>
</RelativeLayout>
Result:
I'm implementing fragments in my activity ,sometimes it shows the following error.when i trying to load the appActivity fragment. please help me get rid out of this error
Java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.rff/com.example.rff.Parent}:
android.view.InflateException: Binary XML file line #68: Error inflating class fragment
here my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/frag_parent"
>
<TableLayout
android:id="#+id/tablenew"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#FFF"
android:paddingTop="1dp"
android:stretchColumns="0"
>
<TableRow
android:background="#000000"
android:paddingBottom="1dp"
android:paddingRight="1dp"
android:tag="new">
<TextView
android:id="#+id/newactivity"
android:text="#string/newact"
android:gravity="center"
android:layout_marginLeft="1dp"
android:textColor="#000000"
android:background="#FFF"
android:padding="10dp"
android:textSize="50sp"
/>
</TableRow>
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="1000dp"
android:layout_height="50dp">
<Button
android:id="#+id/app_new"
android:layout_width="420dp"
android:layout_height="match_parent"
android:text="Apps"/>
<Button
android:id="#+id/doc_new"
android:layout_width="400dp"
android:layout_height="match_parent"
android:text="Docs"/>
</LinearLayout>
<fragment
class="com.example.rff.AppActivity"
android:id="#+id/lm_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<fragment
class="com.example.rff.DocActivity"
android:id="#+id/doc_fragment"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Here is my parent fragment activity
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AppActivity appActivity = new AppActivity();
fragmentTransaction.replace(android.R.id.content, appActivity);
fragmentTransaction.commit();
AppActivity
public class AppActivity extends Fragment{
Context context;
ArrayList<ViewGroup> viewsList;
UpdateAppThread UpdateThread;
View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.app_new_layout,container, false);
context = view.getContext();
...
....
return view ;
}
This is my best guess, since your layout files and rest of the code seem fine.
Depending on your minimum SDK declared in your Manifest, if you have it lower than 11 make sure you have the following imports:
In your fragment class: import android.support.v4.app.Fragment;
In your parent Activity: import android.support.v4.app.FragmentActivity;
Make your parent Activity extend FragmentActivity instead of Activity