Recently i added the "Sign in with google" button and worked fine, but now when i try to run the app this error pops up, and i don't know why.
Here's the full exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.miapp, PID: 26644
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.miapp/com.example.loginapp.HomeScreen}: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to androidx.drawerlayout.widget.DrawerLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
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:965)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to androidx.drawerlayout.widget.DrawerLayout
at com.example.loginapp.HomeScreen.onCreate(HomeScreen.java:31)
at android.app.Activity.performCreate(Activity.java:7327)
at android.app.Activity.performCreate(Activity.java:7318)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3094)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3257)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7050)
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:965)
The error is on this line it seems like:
dl = findViewById(R.id.dl);
Here's the MainActivity (HomeScreen Activity)
package com.example.loginapp;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import com.google.firebase.auth.FirebaseAuth;
public class HomeScreen extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private DrawerLayout dl;
private ActionBarDrawerToggle abdt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
NavigationView nav_view = findViewById(R.id.nav_view);
dl = findViewById(R.id.dl);
abdt = new ActionBarDrawerToggle(this, dl,R.string.Open, R.string.Close);
abdt.setDrawerIndicatorEnabled(true);
firebaseAuth = FirebaseAuth.getInstance();
dl.addDrawerListener(abdt);
abdt.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
nav_view.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener(){
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.miPerfil){
startActivity(new Intent(HomeScreen.this, MiPerfil.class));
}
if (id == R.id.ayudaPerfil){
Toast.makeText(HomeScreen.this, "Ayuda", Toast.LENGTH_SHORT).show();
}
if (id == R.id.agregarLista){
startActivity(new Intent(HomeScreen.this, HomeActivity.class));
Toast.makeText(HomeScreen.this, "Añadir actividad", Toast.LENGTH_SHORT).show();
}
if (id == R.id.logoutMenu){
Toast.makeText(HomeScreen.this, "Cerrando sesión... ", Toast.LENGTH_SHORT).show();
Logout();
}
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.navigation_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
return super.onOptionsItemSelected(item) || abdt.onOptionsItemSelected(item);
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(HomeScreen.this, MainActivityy.class));
}
}
And here's the XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical"
android:id="#+id/dl">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
android:layout_gravity="start"
android:background="#FFF"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</RelativeLayout>
Please, if you can help I appreciate it.
Stack trace clearly indicate the problem. You are using RelativeLayout in your xml and try to cast it as DrawerLayout in your Activity code. Use androidx.drawerlayout.widget.DrawerLayout instead of RelativeLayout in your layout xml.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:id="#+id/dl">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000">
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/nav_view"
android:layout_gravity="start"
android:background="#FFF"
app:headerLayout="#layout/navigation_header"
app:menu="#menu/navigation_menu">
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Related
I got an error but i don't know what wrong because a lot of my code is create auto after i choose Navigation Drawer Activity.
The error have been solve
Caused by: android.view.InflateException: Binary XML file line #12 in com.example.orderfood:layout/activity_home: Binary XML file line #16 in com.example.orderfood:layout/content_home: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #16 in com.example.orderfood:layout/content_home: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: navigation destination com.example.orderfood:id/nav_home is not a direct child of this NavGraph
This is activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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"
android:background="#drawable/background"
tools:openDrawer="start">
//line 12 here
<include
android:id="#+id/app_bar_home"
layout="#layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="#color/gray"
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
This is app_bar_home.xml
<?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=".Home">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.OrderFood.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.OrderFood.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_home" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/white"
app:srcCompat="#drawable/ic_baseline_shopping_cart_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
This is content_home.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_home">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycler_menu"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
//line 16 here
<fragment
android:id="#+id/nav_host_fragment_content_home"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is Home.java
package com.example.orderfood;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import com.example.orderfood.Common.Using;
import com.example.orderfood.Interface.ItemClickListener;
import com.example.orderfood.Model.Category;
import com.example.orderfood.ViewHolder.MenuViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.orderfood.databinding.ActivityHomeBinding;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class Home extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
private ActivityHomeBinding binding;
FirebaseDatabase database;
DatabaseReference category;
TextView txtName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityHomeBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.appBarHome.toolbar.setTitle("Menu");
setSupportActionBar(binding.appBarHome.toolbar);
//Init Firebase
database = FirebaseDatabase.getInstance("https://androidorderfood-df945-default-rtdb.asia-southeast1.firebasedatabase.app");
category = database.getReference("Category");
binding.appBarHome.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();
}
});
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_menu, R.id.nav_cart, R.id.nav_order)
.setOpenableLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
//Set user name
View headerView = navigationView.getHeaderView(0);
txtName = (TextView) findViewById(R.id.txtName);
txtName.setText(Using.currentUser.getName());
//Load menu
recycler_menu = (RecyclerView) findViewById(R.id.recycler_menu);
recycler_menu.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recycler_menu.setLayoutManager(layoutManager);
loadMenu();
}
private void loadMenu() {
FirebaseRecyclerAdapter<Category, MenuViewHolder> adapter = new FirebaseRecyclerAdapter<Category, MenuViewHolder>(Category.class, R.layout.menu_item, MenuViewHolder.class, category) {
#Override
protected void populateViewHolder(MenuViewHolder menuViewHolder, Category category, int i) {
menuViewHolder.txtMenuName.setText(category.getName());
Picasso.with(getBaseContext()).load(category.getImage()).into(menuViewHolder.imageView);
final Category clickItem = category;
menuViewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, int position, boolean isLongClick) {
Toast.makeText(Home.this, ""+clickItem.getName(), Toast.LENGTH_SHORT).show();
}
});
}
};
recycler_menu.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_home);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
This is mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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/mobile_navigation"
app:startDestination="#+id/nav_home">
<fragment
android:id="#+id/nav_menu"
android:name="com.example.orderfood.ui.home.HomeFragment"
android:label="#string/menu_menu"
tools:layout="#layout/fragment_home" />
<fragment
android:id="#+id/nav_cart"
android:name="com.example.orderfood.ui.gallery.GalleryFragment"
android:label="#string/menu_cart"
tools:layout="#layout/fragment_gallery" />
<fragment
android:id="#+id/nav_order"
android:name="com.example.orderfood.ui.slideshow.SlideshowFragment"
android:label="#string/menu_order"
tools:layout="#layout/fragment_slideshow" />
</navigation>
I am trying to add a player using Sliding Up Panel but the app crashes on startup. If you want to see my full code;
https://github.com/Kailash8460/poptune.git
MainActivity.java
package com.example.poptunemusicplayerapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import com.example.poptunemusicplayerapplication.fragments.MainFragment;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
public class MainActivity extends AppCompatActivity {
private static final int KEY_PER = 123;
private SlidingUpPanelLayout panelLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]
{Manifest.permission.READ_EXTERNAL_STORAGE}, KEY_PER);
return;
}
else {
UiInitialize();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
switch (requestCode){
case KEY_PER:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
UiInitialize();
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void UiInitialize() {
ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(this));
panelLayout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
Fragment fragment = new MainFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_container, fragment);
transaction.commit();
/*List<Song> songList = new SongLoader().getAllSongs(this);
for (Song song : songList){
Log.i("DATA","Title:" + song.title);
}*/
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout 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/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
app:umanoPanelHeight="68dp"
app:umanoShadowHeight="4dp"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/control_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
MainFragment.java
Here there is no issue on SongsFragment, ArtistFragment and AlbumFragment files, because I have run this application before adding the sliding up panel and that was successfully run but after adding this Sliding Up Panel application crashes on startup.
package com.example.poptunemusicplayerapplication.fragments;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.poptunemusicplayerapplication.R;
import com.google.android.material.tabs.TabLayout;
import java.util.ArrayList;
import java.util.List;
public class MainFragment extends Fragment {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
public MainFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
tabLayout = (TabLayout) rootView.findViewById(R.id.tabs);
viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
setUpViewPager(viewPager);
tabLayout.setupWithViewPager(viewPager);
return rootView;
}
private void setUpViewPager(ViewPager viewPager) {
FragmentAdapter adapter = new FragmentAdapter(getActivity().getSupportFragmentManager());
adapter.AddFragments(new SongsFragment(), "Songs");
adapter.AddFragments(new ArtistFragment(), "Artist");
adapter.AddFragments(new AlbumFragment(), "Album");
viewPager.setAdapter(adapter);
}
private class FragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> titleList = new ArrayList<>();
public FragmentAdapter(#NonNull FragmentManager fm) {
super(fm);
}
#NonNull
#Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
#Override
public int getCount() {
return fragmentList.size();
}
public void AddFragments(Fragment fragment, String title) {
fragmentList.add(fragment);
titleList.add(title);
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
}
}
fragment_main.xml
<?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:id="#+id/fragmentmainlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragments.MainFragment">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/appbar"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="#+id/toolbar"
app:layout_scrollFlags="scroll|enterAlways"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.AppCompat.Light"/>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabs"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/view_pager"
app:layout_behavior="#string/appbar_scrolling_view_behavior"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Errors
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.poptunemusicplayerapplication, PID: 28426
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.poptunemusicplayerapplication/com.example.poptunemusicplayerapplication.MainActivity}: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: android.view.InflateException: Binary XML file line #14: Binary XML file line #14: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class com.sothree.slidinguppanel.SlidingUpPanelLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.poptunemusicplayerapplication.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2941)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
Caused by: java.lang.IllegalArgumentException: gravity must be set to either top or bottom
at com.sothree.slidinguppanel.SlidingUpPanelLayout.setGravity(SlidingUpPanelLayout.java:366)
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:288)
at com.sothree.slidinguppanel.SlidingUpPanelLayout.<init>(SlidingUpPanelLayout.java:270)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
at com.example.poptunemusicplayerapplication.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:7258)
at android.app.Activity.performCreate(Activity.java:7249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1222)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2941)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3073)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:7025)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:441)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408)
getting error at MainActivity.java:39
Line 39 of MainActivity.java:-
setContentView(R.layout.activity_main);
After looking at the layout in the demo app for SlidingUpPanelLayout, I see it is expecting the android:gravity attribute to be set, not android:layout_gravity.
Try this instead for your layout:
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout 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/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
app:umanoPanelHeight="68dp"
app:umanoShadowHeight="4dp"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/control_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
When i try to run my app i get this error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.example.mysolver/com.example.mysolver.MainActivity}:
java.lang.ClassCastException:
androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to
android.widget.Button
at com.example.mysolver.MainActivity.onCreate(MainActivity.java:46)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Other people have had this problem but none of the solutions worked for me.
Here is my "content_main.xml"
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="#layout/activity_main">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:onClick="onClick"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:ems="10"
android:hint="Enter letters"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
<ScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="25dp"
android:layout_marginEnd="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/output"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Output will appear here..."
android:textSize="20sp" />
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Here's my "activity_main.xml":
<?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=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<androidx.appcompat.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" />
</com.google.android.material.appbar.AppBarLayout>
<include
android:id="#+id/button"
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Here's my "MainActivity.java":
package com.example.mysolver;
import android.content.res.AssetManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
public class MainActivity extends AppCompatActivity {
Button butt;
EditText text;
String letters;
TextView mOutput;
processData procDat;
public static final String TAG = MainActivity.class.getName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
Log.d(TAG, "Set up tool bar");
setSupportActionBar(toolbar);
text = findViewById(R.id.editText);
Log.d(TAG, "Set up text");
mOutput = findViewById(R.id.output);
butt = findViewById(R.id.button);
Log.d(TAG, "Set up button");
AssetManager assetManager = getAssets();
InputStream inputStream;
try {
inputStream = assetManager.open("dictionary.txt");
} catch (IOException e) {
inputStream = null;
}
Reader reader = new InputStreamReader(inputStream);
BufferedReader is = new BufferedReader(reader);
procDat = new processData("", is);
Log.d(TAG, "Initialized procDat");
}
public void onClick(View v)
{
Log.d(TAG, "Button clicked");
String output;
letters = text.getText().toString();
procDat.setLetters(letters);
try {
output = procDat.doProcessing();
} catch (IOException e) {
output = "Error while opening file";
}
mOutput.setText(output);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Simply rename the id with other id instead of button. You can't use the same id in two layout since content_main.xml is included in activity_main.xml.
<include
android:id="#+id/button"
layout="#layout/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
the main problem is in your "AndroidManidfest.xml" file and it is corrected by changing the theme of your application or theme of your activity. here are 3 solutions for that...
Changing the theme of all activity by
"android:theme="#style/Theme.AppCompat"" or by
"android:theme="#style/Theme.MaterialComponents.Light.NoActionBar""
Changing the theme of your application by
"android:theme="#style/Theme.AppCompat"" or by
"android:theme="#style/Theme.MaterialComponents.Light.NoActionBar""
Or by changing your
"androidx.constraintlayout.widget.ConstraintLayout" by
"RelativeLayout" in your "content_main.xml" file
I'm sure that it works...
I want to be able to swap between two fragments in my main activity class. But when inflating the fragment my application crashes. Below is my code:
This is the XML code where my fragment will be placed
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#drawable/rounded_dialog"
android:orientation="vertical"
app:behavior_hideable="false"
app:behavior_peekHeight="56dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<fragment
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"/>
</LinearLayout>
This is the code for my Fragment
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.user.swim.AsyncTasks.GeoCodingTask;
public class SearchLocation extends Fragment {
private EditText destination;
private Button search;
private RecyclerView recyclerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_search_locatiom, null);
search = view.findViewById(R.id.search_destination);
destination = view.findViewById(R.id.destination);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
searchLocation(view);
return view;
}
Below is the code in my main activity to inflate the layout
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_place)!=null){
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
I get the following errors in my logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.swim, PID: 20462
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.swim/com.example.user.swim.MainActivity}: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: android.view.InflateException: Binary XML file line #29: Binary XML file line #14: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
Caused by: java.lang.NullPointerException
at java.lang.VMClassLoader.findLoadedClass(Native Method)
at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:738)
at java.lang.ClassLoader.loadClass(ClassLoader.java:363)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at android.app.Fragment.instantiate(Fragment.java:617)
at android.app.FragmentContainer.instantiate(FragmentContainer.java:49)
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3598)
at android.app.FragmentController.onCreateView(FragmentController.java:98)
at android.app.Activity.onCreateView(Activity.java:6182)
at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:338)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:783)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
at android.view.LayoutInflater.parseInclude(LayoutInflater.java:995)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:862)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:827)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.user.swim.MainActivity.onCreate(MainActivity.java:87)
at android.app.Activity.performCreate(Activity.java:6980)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
This is the XML for fragment_search_locatiom
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"
android:layout_margin="10dp"
android:text="#string/where_would_you_like_to_go"
android:textAlignment="center" />
<EditText
android:id="#+id/destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="10dp"
android:drawablePadding="5dp"
android:drawableStart="#drawable/ic_search_black_24dp"
android:drawableLeft="#drawable/ic_search_black_24dp"
android:textSize="18sp"
android:inputType="text"
android:background="#drawable/rectangle"
android:hint="Search for destination"
android:maxLines="1"
android:imeOptions="actionDone"
android:textAlignment="textStart" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/search_destination"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/search_destination" />
</LinearLayout>
I don't want to add the name/class attribute in the XML for the fragment because when you look at the official documentation adding those attributes won't make it possible to change fragments at runtime.
Also i've tried changing the MainActivity class to extend ActivityFragment but after more research i've discovered that ActivityFragment is a subclass of AppCompatActivity.
i have copied your code and try to use it,when i use fragment in the SearchLocation it makes error so i changed it to Framlayout.
Can't realy tell what is wrong in your code but this worked after i made changes
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_place) != null) {
if (savedInstanceState != null) {
return;
}
SearchLocation fragment = new SearchLocation();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_place, fragment).commit();
}
}
SearchLocation java
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class SearchLocation extends Fragment {
private EditText mSearch;
private Button mDestination;
private RecyclerView mRecyclerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search_location, null);
mSearch = view.findViewById(R.id.search_destination);
mDestination = view.findViewById(R.id.destination);
mSearch.setText("Hello world");
RecyclerView mRecyclerView = view.findViewById(R.id.recyclerView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
/*searchLocation(view);*/
return view;
}
search_location xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SearchLocation">
<EditText
android:id="#+id/search_destination"
android:layout_width="match_parent"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/destination"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/search_destination" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/destination" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can try with Framelayout instead of fragment like the following
<!-- Framelayout to display Fragments -->
<FrameLayout
android:id="#+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" />
I'm new to material design and every time i try to use RecyclerView every thing gose wrong..
can any one know what is the problem ?
Main Activity class
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toool);
setSupportActionBar(toolbar);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Main Activity layout
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<include
android:id="#+id/toool"
layout="#layout/app_tool" />
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
tools:layout="#layout/rere" />
</RelativeLayout>
Recycler fragment
<?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/recye"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
Recycler class
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class Recycler extends Fragment {
private RecyclerView recyclerView;
private MyRecyeAdapter adapter;
private View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.rere, container, false);
recyclerView = (RecyclerView) getActivity().findViewById(R.id.recye);
adapter = new MyRecyeAdapter(getActivity(), getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
public static List<info> getData() {
List<info> data = new ArrayList<>();
int photos[] = { R.drawable.images, R.drawable.dreamsmal, R.drawable.images, R.drawable.dreamsmal };
String users[] = { "Hothyfa", "Marwan", "Jamal", "Alaa" };
for (int i = 0; i < photos.length && i < users.length; i++) {
info current = new info();
current.iconId = photos[i];
current.Names = users[i];
data.add(current);
}
return data;
}
}
RecyclerView Adapter class
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;
public class MyRecyeAdapter extends RecyclerView.Adapter<MyRecyeAdapter.myholder> {
List<info> data = Collections.emptyList();
private LayoutInflater inflater;
public MyRecyeAdapter(Context context, List<info> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
#Override
public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom, parent, false);
myholder holder = new myholder(view);
return holder;
}
#Override
public void onBindViewHolder(myholder holder, int position) {
info current = data.get(position);
holder.imageProfile.setImageResource(current.iconId);
holder.UsersName.setText(current.Names);
}
#Override
public int getItemCount() {
return data.size();
}
public class myholder extends RecyclerView.ViewHolder {
ImageView imageProfile;
TextView UsersName;
public myholder(View itemView) {
super(itemView);
imageProfile = (ImageView) itemView.findViewById(R.id.imageView);
UsersName = (TextView) itemView.findViewById(R.id.textView);
}
}
}
custom row layout (for the RecyclerView)
<?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="horizontal" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:src="#drawable/images" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:padding="8dp"
android:text="Large Text" />
</LinearLayout>
and the data class for the Recycler
public class info {
int iconId;
String Names;
}
Toolbar layout
<?xml version="1.0" encoding="utf-8"?>
<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/primarycolor" >
</android.support.v7.widget.Toolbar>
Note: I have put this in the build.gradle
compile 'com.android.support:recyclerview-v7:22.2.1'
and rebuild the app.
Error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hothyfa.recyclerandmore/com.example.hothyfa.recyclerandmore.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at
app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at
\\\\\\\android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
at com.example.hothyfa.recyclerandmore.MainActivity.onCreate(MainActivity.java:19)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
Ok, I found the issue, in your onCreateView of Recycler
recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye);
change it to:
recyclerView= (RecyclerView) view.findViewById(R.id.recye);
try to edit this
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
/>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:background="#2196F3"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<fragment
android:id="#+id/fragment"
android:name="com.example.hothyfa.recyclerandmore.Recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/toool"
android:layout_centerHorizontal="true"
tools:layout="#layout/rere" />
</RelativeLayout>
Try This:
<FrameLayout 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/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
android:clipToPadding="false"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"/>
</FrameLayout>
You can see these two links How to hide/show toolbar in response to scrolling on Android and Recyclerview with Cards example in Android with AppCompat (V7)
Do lemme knw in any concern