We have different differnet activities of google map ( Some with tabs, Some simple, And some other activities), So we are are trying to implement navigation drawer in our app, For this we created a class which extend ActionBarActivity and has drawer, And we are extending this main activity in our classes for navigation drawer, But when we open our application its showing map is null.
Please help.
Here is our files
drawerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FA0"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
/>
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:id="#+id/shitstuff"
app:itemTextColor="#000"
app:menu="#layout/drawermenu"
android:layout_marginTop="-24dp"
/>
</android.support.v4.widget.DrawerLayout>
mapload.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffc17540"
android:id="#+id/maptab"
>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:id="#+id/slidingContainer"
android:layout_width="fill_parent"
android:gravity="bottom"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/t"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffff773c"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/rg_views"
android:orientation="horizontal"
android:gravity="bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
<RadioButton
android:id="#+id/rb_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_normal"
android:checked="true" />
<RadioButton
android:id="#+id/rb_satellite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/str_rb_satellite" />
<RadioButton
android:id="#+id/rb_terrain"
android:layout_width="161dp"
android:layout_height="wrap_content"
android:text="#string/str_rb_terrain" />
</RadioGroup>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/inhorizontalscrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:src="#android:drawable/ic_input_add" />
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<View
android:id="#+id/transparentView"
android:visibility="gone"
android:layout_width="fill_parent"
android:layout_height="#dimen/map_height"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>
BaseActivity.java
public class BaseActivity extends ActionBarActivity
{
public DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_list);
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
// You were missing this setHomeAsUpIndicator
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_navigation_drawer);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView n = (NavigationView) findViewById(R.id.shitstuff);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout , R.string.drawer_open, R.string.drawer_close)
{
/*
* Called when a drawer has settled in a completely closed state
*/
public void onDrawerClosed(View view)
{
Log.d("drawerToggle", "Drawer closed");
super.onDrawerClosed(view);
// getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu(); //Creates call to onPrepareOptionsMenu()
}
/*
* Called when a drawer has settled in a completely open state
*/
public void onDrawerOpened(View drawerView)
{
Log.d("drawerToggle", "Drawer opened");
super.onDrawerOpened(drawerView);
// getActionBar().setTitle("NavigationDrawer");
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
n.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
////.......
}
mDrawerLayout.closeDrawers(); // CLOSE DRAWER
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// THIS IS YOUR DRAWER/HAMBURGER BUTTON
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
return super.onOptionsItemSelected(item);
}
}
Mylocation.java
public class MyLocation extends BaseActivity implements View.OnClickListener,OnMapReadyCallback {
GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mapload, null, false); // line no-74
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
But everytime i run application i am getting this error
java.lang.RuntimeException: Unable to start activity ComponentInfo{MyLocation}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:551)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74) //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
at android.app.ActivityThread.access$1100(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7237)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:788)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:716)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:847)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:810)
at android.view.LayoutInflater.inflate(LayoutInflater.java:527)
at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
at com.myapp.gps.MyLocation.onCreate(MyLocation.java:74) //error here
at android.app.Activity.performCreate(Activity.java:6876)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
What I tried:-
inflater.from(getApplicationContext()).inflate(R.layout.mapload, pageHolder, true);
GoogleMap mapFragment =((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); //getting null pointer exception here
another thing which I tried
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit(); //same null pointer exception
The line with inflater.inflate is doing nothing useful there in that class. The XML layout isn't loaded and findFragmentById(R.id.map) is returning your null.
I think you wanted something like
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
setContentView(R.layout.mapload);
// Find fragment here
}
Or maybe you maybe you just wanted setContentView from the parent class?
Basically, the error is saying your <fragment> tag is missing a class.
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
<!-- Needs a class or name attribute -->
android:gravity="bottom"
android:layout_height="fill_parent" />
For example, from the documentation
<fragment
class="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
(Also worth mentioning that xmlns:android only is to be applied to the root xml element)
Though, I do not think you need a whole separate Activity or layout.
Load the map fragment into this FrameLayout
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
just like any regular Fragment...
SupportMapFragment mapFragment = new SupportMapFragment();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content, mapFragment)
.commit();
mapFragment.getMapAsync(this);
add this line to your xml:
android:name="com.google.android.gms.maps.SupportMapFragment"
like this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:gravity="bottom"
android:layout_height="fill_parent"/>
and the best way to implement google map with drawer is to use fragment, that's mean change your class "MyLocation" to extends from fragment
Related
This question already has answers here:
java.lang.IllegalStateException: Underflow in restore - more restores than saves
(4 answers)
Closed 5 years ago.
Image for Example
ChildFragment Exmple
OneFragment.java
public class OneFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Fragment Demo", "Fragment_One onCreate()");
}
public OneFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
return rootView;
}
#Override
public void onAttach(Context newBase) {
super.onAttach(CalligraphyContextWrapper.wrap(newBase));
}
}
fragment_one.xml
<RelativeLayout 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=".fragment.OneFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#color/grey_blue_100"
android:src="#android:drawable/ic_dialog_alert"/>
<TextView
android:gravity="center"
android:textSize="16sp"
android:textColor="#color/grey_blue_100"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="نقشه گوگل در حال حاضر در دسترس نمی باشد"/>
</LinearLayout>
</RelativeLayout>
MainFragment.java
public class RegisterFragment extends Fragment implements View.OnClickListener {
private FragmentActivity myContext;
private Fragment fragment;
private View rootView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Fragment Demo", "Fragment_One onCreate()");
}
public RegisterFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_register_6, container, false);
Button btnOne = (Button) rootView.findViewById(R.id.btn_one);
btnOne.setOnClickListener(this);
Button btnTwo = (Button) rootView.findViewById(R.id.btn_two);
btnTwo.setOnClickListener(this);
Button btnFinal = (Button) rootView.findViewById(R.id.btn_three);
btnFinal.setOnClickListener(this);
// This is default Fragment for RegisterFragment
setFragmentUp(new OneFragment());
return rootView;
}
#Override
public void onClick(View v) {
fragment = null;
switch (v.getId()) {
case R.id.btn_one:
fragment = new OneFragment();
break;
case R.id.btn_two:
fragment = new TwoFragment();
break;
case R.id.btn_three:
fragment = new FinalFragment();
break;
default:
break;
}
if (fragment != null) {
setFragmentUp(fragment);
}
}
#Override
public void onAttach(Activity activity) {
myContext = (FragmentActivity) activity;
super.onAttach(activity);
}
protected void setFragmentUp(Fragment fragment) {
myContext.getSupportFragmentManager().beginTransaction()
.replace(R.id.fragRegisterUp, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
#Override
public void onAttach(Context newBase) {
super.onAttach(CalligraphyContextWrapper.wrap(newBase));
}
}
fragment_main.xml
<LinearLayout
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"
android:background="#color/colorAccent"
android:orientation="vertical"
tools:context="com.fanavartech.android.ham_bar_v002.fragment.RegisterFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:background="#android:color/white"
android:orientation="horizontal">
<com.andexert.library.RippleView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:rv_rippleDuration="114"
app:rv_color="#color/colorPrimaryDark"
rv_centered="true">
<Button
android:id="#+id/btn_one"
android:layout_width="match_parent"
android:text="اطلاعات بار"
android:textSize="16sp"
android:textStyle="bold"
android:background="#null"
android:layout_height="match_parent"/>
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:rv_rippleDuration="114"
app:rv_color="#color/colorPrimaryDark"
rv_centered="true">
<Button
android:id="#+id/btn_two"
android:text="اطلاعات مسیر"
android:textSize="16sp"
android:textStyle="bold"
android:background="#null"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.andexert.library.RippleView>
<com.andexert.library.RippleView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
app:rv_rippleDuration="114"
app:rv_color="#color/colorPrimaryDark"
rv_centered="true">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_three"
android:text="پرداخت"
android:textSize="16sp"
android:textStyle="bold"
android:background="#null"/>
</com.andexert.library.RippleView>
</LinearLayout>
<FrameLayout
android:id="#+id/fragRegister"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="16dp"/>
</LinearLayout>
MainActivity.java
public class FirstActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.drawer_open , R.string.drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Toast.makeText(FirstActivity.this, "ثبت بار", Toast.LENGTH_SHORT).show();
toolbar.setTitle(R.string.nav_register);
Fragment fragment = new RegisterFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container_body, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
activity_main.xml
<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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"/>
</LinearLayout>
<android.support.percent.PercentFrameLayout
android:id="#+id/container_body"
android:layout_width="fill_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
Error Massage
06-05 13:16:55.049 4867-4867/com.fanavartech.android.ham_bar__v002 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.android.ham_v002, PID: 4867
java.lang.IllegalStateException: Underflow in restore - more restores than saves
at android.graphics.Canvas.native_restore(Native Method)
at android.graphics.Canvas.restore(Canvas.java:540)
at com.andexert.library.RippleView.draw(RippleView.java:166)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
at android.view.View.updateDisplayListIfDirty(View.java:15134)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
for Android version 6 error, but at lower Android working properly.
Where is the problem?
You can try two approaches:
"Just downgrade your - targetSdkVersion to 22 It works for all
versions" - This works fine if you have not launched your app with
targetSdkVersion 23 yet.
If you already have launched your app with targetSdkVersion 23 then,
instead of adding compile 'com.github.traex.rippleeffect:library:1.3'
in dependancies, Download the project by clicking on link -
https://codeload.github.com/traex/RippleEffect/zip/master
Get library from that and use that in your app. It's probably gonna work :)
How can I put my fab on my fragment? help please I'm getting errors such as.
android.view.InflateException: Binary XML file line #13: Error inflating class fragment
in my fragment acticity. Well here are my codes.
well the problem is in the getActivity().setContentView(R.layout.activity_ownerhome);
#ownerhome.java
public class ownerhome extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ownerhome);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
ownerhomeFragment.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_owner, container, false);
ImageLoader.getInstance().init(UILConfig.config(getActivity()));
lvCars = (ListView) v.findViewById(R.id.lvOnwer);
pref = getActivity().getSharedPreferences("Login.conf", Context.MODE_PRIVATE);
Log.d(TAG, pref.getString("username", ""));
Log.d(TAG, pref.getString("password", ""));
PostResponseAsyncTask taskRead = new PostResponseAsyncTask(getActivity(), this);
tvUser = (TextView) v.findViewById(R.id.tvUser);
tvUser.setText("Hello! "+pref.getString("username", ""));
taskRead.execute("http://carkila.esy.es/user.php?owner="+pref.getString("username", ""));
FloatingActionButton fab = (FloatingActionButton) v.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent in = new Intent(getActivity(), InsertActivity.class);
startActivity(in);
}
});
registerForContextMenu(lvCars);
return v;
}
content_ownerhome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kun.carkila.ownerFragment"
android:id="#+id/ownerhomeFragment"/>
</RelativeLayout>
activity_ownerhome.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.design.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"
android:fitsSystemWindows="true"
tools:context="com.example.kun.carkila.ownerhome">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_ownerhome" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/plus" />
</android.support.design.widget.CoordinatorLayout>
fragmentOwnerHome.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<ListView
android:layout_width="match_parent"
android:layout_height="450sp"
android:id="#+id/lvOnwer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/tvUser" />
</RelativeLayout>
STACK TRACE
FATAL EXCEPTION: main
Process: com.example.kun.carkila, PID: 9267
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kun.carkila/com.example.kun.carkila.ownerhome}: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.kun.carkila.ownerFragment.onCreateView(ownerFragment.java:127)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1230)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2288)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:839)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:745)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.kun.carkila.ownerhome.onCreate(ownerhome.java:12)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1090)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5021)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Binary XML file line #12: Duplicate id 0x7f0e00aa, tag null, or parent id 0xffffffff with another fragment for com.example.kun.carkila.ownerFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity
You have added the floating action button in your activity_ownerhome.xml and you are initializing it in ownerhomeFragment.java.
So add your FloatingActionButton in fragmentownerhome.xml itself like this
<RelativeLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.kun.carkila.ownerhome"
tools:showIn="#layout/activity_ownerhome">
<ListView
android:layout_width="match_parent"
android:layout_height="450sp"
android:id="#+id/lvOnwer"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="24dp"
android:textSize="30sp"
android:textColor="#color/colorPrimaryDark"
android:id="#+id/tvUser" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#drawable/plus" />
</RelativeLayout>
and remove the FloatingActionButton from your activity_ownerhome.xml file.
The cause of the crash is because your FAB xml code is in the "Activity layout", it is supposed to be in the "fragment layout" file
This is how i use FAB in a fragment
In my fragment layout file:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
app:srcCompat="#android:drawable/ic_dialog_email" />
In my fragment class file:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
return view;
}
And it works...
NOTE:
when i included android:backgroundTint="#android:color/transparent"
the application crashed and gave the error :
Binary XML file line #14: Error inflating class android.support.design.widget.FloatingActionButton
Hope this helps.
The problem isn't with the FAB, it's with how you inflate your view.
Replace this code in the Fragment:
View v = super.onCreateView(inflater, container, savedInstanceState);
getActivity().setContentView(R.layout.activity_ownerhome);
With this:
var v = inflater.inflate(R.layout.{your fragment xml}, container, false);
And remove this:
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.kun.carkila.ownerFragment"
android:id="#+id/ownerhomeFragment"/>
You should define how you want your fragment xml to look, and then inflate it using the code inflater.inflate... and put in your fragment xml there, like I wrote above.
Update:
Instead of <include layout="#layout/content_ownerhome" /> place in this:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/content_frame" />
Then in your Main Activity write this code:
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
And you're supposed to see your fragment, if in the fragment you inflated your fragment xml correctly, as stated above.
You must implement the fragment in your main class something like...
public class ownerhome extends AppCompatActivity implement ownerhomeFragment {...
I apologize in beforehand for my bad code and bad knowledge. I have just recently started coding in Android and Java
My problem is that I have Navigation Drawer and Tabs. Although if I choose something from the navigation drawer which loads up in a Fragment it doesn't show. Only the tabs are showing.
My plan is that one navigation drawer tab shall have 3 tabs inside and the rest just normal pages without tabs.
MainActivity.java
http://pastebin.com/TV7aWy9c
activity_main.xml
<?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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/fragment_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#000000" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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"
android:fitsSystemWindows="true"
tools:context="se.themeister.hello.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/AppTheme.AppBarOverlay"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#009900"
android:layout_below="#id/tab_layout"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
I don't know if that's enough or if more information or code is needed
The problem with your code is that your ViewPager with TabLayout should be part of the Fragment, not part of the Activity. And NavigationDrawer should just replace one fragment by another in the viewport (i.e. in the container). Now, in your code, you're trying to somehow insert your new Fragment into the ViewPager.
Here's a very basic app with Navigation Drawer, one Fragment with Tabs and rest fragments without tabs, which you can use as an example:
Activity.xml:
<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_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragmentContainer"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
The code in the activity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.fragment_1) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithTabs()).commit();
} else if (id == R.id.fragment_2) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new FragmentWithoutTabs()).commit();
} else if (id == R.id.fragment_3) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentContainer, new Fragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
I.e., as you see, I'm replacing fragments in the FrameLayout with id fragmentContainer.
All logic related to the fragment with tabs, incapsulated in FragmentWithTabs:
public class FragmentWithTabs extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_with_tabs, container, false);
ViewPager viewPager = (ViewPager)rootView.findViewById(R.id.viewPager);
TabLayout tabLayout = (TabLayout)rootView.findViewById(R.id.tabLayout);
viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
#Override
public Fragment getItem(int position) {
return new SubFragment(position == 0? Color.BLUE : position == 1? Color.WHITE : Color.RED);
}
#Override
public CharSequence getPageTitle(int position) {
return position+"";
}
#Override
public int getCount() {
return 3;
}
});
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
}
I hope, it helps
I have two navigation drawer, one with a ListView and the second with a Layout. The first Navigationdrawer works fine, but the second can't open it on a button click.
Here is the MainActivity xml code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_gravity="right"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:navigationIcon="#drawable/arrow"
app:title="MyGames"
>
<Button
android:id="#+id/rigthMenu"
android:background="#drawable/compose"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginRight="5dp"
android:layout_gravity="right" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollIndicators="bottom">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="2dp"
android:background="#ffffff"
android:scrollbars="vertical"/>
<include
android:id="#+id/layRigth"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
layout="#layout/rigth_menu" />
</android.support.v4.widget.DrawerLayout>
Here is the rigthMenu layout xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:gravity="left">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox2"
android:layout_gravity="center_horizontal" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox"
android:layout_gravity="center_horizontal" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New CheckBox"
android:id="#+id/checkBox4"
android:layout_gravity="center" />
and here is the MainActivity OnCreate code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// mDrawerLayout2 = (DrawerLayout)findViewById(R.id.layRigth);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onIcon = !onIcon;
if (onIcon == true) {
mDrawerLayout.openDrawer(mDrawerList);
} else {
mDrawerLayout.closeDrawer(mDrawerList);
}
}
});
Button rigthMenu = (Button)findViewById(R.id.rigthMenu);
rigthMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final LayoutInflater factory = getLayoutInflater();
final View text = factory.inflate(R.layout.rigth_menu, null);
mDrawerLayout2 = (DrawerLayout)text.findViewById(R.id.layRigth);
mDrawerLayout2.openDrawer(mDrawerLayout2);
}
});
Drawable x = toolbar.getNavigationIcon();
getDefaultSettings("Image", this);
// mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;
mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.content_frame,new TabFragment()).commit();
// Initializing
dataList = new ArrayList<DrawerItem>();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Add Drawer Item to dataList
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, GREEN, 1));
dataList.add(new DrawerItem(R.mipmap.ps4miniatura, "PC", 0));
dataList.add(new DrawerItem(0, " ", 2));
adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item, adapter,
dataList);
// addItem.giveAdapter(adapter);
mDrawerList.setAdapter(adapter);
mDrawerList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
if (savedInstanceState == null) {
SelectItem(1);
}
}
Logcat error:
12-08 08:40:52.936 15587-15587/com.tutecentral.navigationdrawer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tutecentral.navigationdrawer, PID: 15587
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(android.view.View)' on a null object reference
at com.tutecentral.navigationdrawer.MainActivity$2.onClick(MainActivity.java:75)
at android.view.View.performClick(View.java:4820)
at android.view.View$PerformClick.run(View.java:20157)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5578)
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:955)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
I have a nullPointException. How can I get the view of the layout rigthMenu?
There are a few errors in the rigthMenu's OnClickListener. Firstly, there's no need to inflate the rigth_menu layout there. You've included it in the main layout, so it will already be inflated. Also, rigth_menu does not contain any View with ID layRigth, so text.findViewById(R.id.layRigth) is returning null, and causing the NullPointerException. And, you've already got a reference to the DrawerLayout - mDrawerLayout - so you don't need to find it again.
rigthMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(GravityCompat.END)) {
mDrawerLayout.closeDrawer(GravityCompat.END);
}
else {
mDrawerLayout.openDrawer(GravityCompat.END);
}
}
});
Please use below code to open and close the drawer
public void toggleDrawer() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START))
mDrawerLayout.closeDrawer(GravityCompat.START);
else
mDrawerLayout.openDrawer(GravityCompat.START);
}
I am adding a drawer layout to my Activity which uses fragments. I've implemented this just as described here per the android documentation. And the drawer layout Adapter and View all work in that they show up, but the drawer extends "under" the fragment content. Here is a picture of what is happening:
Does anyone know why this might be? My code for the activity's xml, and the activity add fragment is below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_list); // for drawer navigation
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
fragmentTransaction.commit();
setUpDrawer(fragmentManager);
}
private void setUpDrawer(FragmentManager fm) {
String[] drawerItems = getResources().getStringArray(
R.array.drawer_array);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ListView drawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.item_nav_drawer, drawerItems));
// Set the list's click listener
drawerList.setOnItemClickListener(new
DrawerItemClickListener(drawer,drawerList,drawerItems,fm));
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/alarm_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
Do I have to set a z-index somewhere? I can provide any further info needed I'd just really like to figure this out!
I don't what produces this issue , but to solve it u have to add the fragment to the content of your FrameLayout which has the id = content_frame.
So edit this line
fragmentTransaction.replace(android.R.id.content,
new AlarmListFragment());
to be
fragmentTransaction.replace(R.id.content_frame,
new AlarmListFragment());
You can move the drawer layout below your Fragment container in your XML. this will render the DrawerLayout in the forward view of the app. just be sure not to cover the fragment container with the parent container of the drawer so that your fragment will be accessible when you need it.
<RelativeLayout
android:id="#+id/my_parent_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity"
xmlns:tools="http://schemas.android.com/tools">
<FrameLayout
android:id="#+id/fragment_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
<android.support.v4.widget.DrawerLayout
android:id="#+id/my_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:id=#+id/drawer_pane"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="end" />
<ListView xmlns="http://schemas.android.com/apk/res/android"
android:layout_width="280dp"
android:layout_height="match_parent"
android:choiceMode="singleChoice"
android:id="#+id/menu_list"
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>