Reputation: 808
I want to set SettingsDrawer as a Fragment in multiple activities (The settingsDrawer will display after click on specific button). The background of the settings drawer should be z current activity unfortunatly i tried to set fragment static (in activity xml) and also dynamicly. For first try i got inflating class fragment error and for dynamicly ive obtain a null pointer. Here is my dynamicly version:
SettingsDrawer Class fragment
public class SettingsDrawer extends Fragment{
Context mContext;
ArrayList<SettingsDrawerItem> items;
ListView listView;
DrawerLayout mDrawerLayout;
RelativeLayout content;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.settings_drawer_layout, null);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mContext=getContext();
mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawerLayout);
listView = (ListView) view.findViewById(R.id.settingsList);
DrawListAdapter adapter = new DrawListAdapter(mContext, items);
listView.setAdapter(adapter);
content = (RelativeLayout)view.findViewById(R.id.drawerPane);
openAndCollapseDrawerList();
}
public SettingsDrawer() {
mContext=getContext();
items = new ArrayList<>();}
public void openAndCollapseDrawerList()
{
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT))
mDrawerLayout.closeDrawer(content);
else
mDrawerLayout.openDrawer(content);
}
Settings Drawer layout
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="280dp"
android:layout_height="match_parent"
android:id="@+id/drawerPane"
android:layout_gravity="start">
<ListView
android:id="@+id/settingsList"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_below="@+id/logoBox"
android:choiceMode="singleChoice"
android:background="#ffffffff" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Activity Part where I initialize fragment
private void initSettingsDrawer() {
Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer);
settingsDrawerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = SettingsDrawer.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.activity_main, fragment).commit();
}
});
}
That gives me a nullPointer Exception.
Upvotes: 0
Views: 224
Reputation: 363
add this code in your SettingsDrawer
static SettingsDrawer fragment;
static SettingsDrawer getInstance(){
if(fragment==null){
return new SettingsDrawer();
}else{
return fragment;
}
and in initSettingsDrawer :
fragment = SettingsDrawer.getInstance();
Upvotes: 1