Below are my both activities which i am calling .
I know this is answered before but my case seems to be different.
Can somebody look into this?
import android.os.Bundle;
import android.view.View;
import com.example.mediapipeposetracking.databinding.ActivityDashboardBinding;
public class DashboardActivity extends DrawerBaseActivity{
ActivityDashboardBinding activityDashboardBinding;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityDashboardBinding = ActivityDashboardBinding.inflate(getLayoutInflater());
setContentView(activityDashboardBinding.getRoot());
}
}
Second Activity
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.google.android.material.navigation.NavigationView;
public class DrawerBaseActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawerLayout;
#Override
public void setContentView(View view) {
drawerLayout=(DrawerLayout) getLayoutInflater().inflate(R.layout.activity_drawer_base,null);
FrameLayout container=drawerLayout.findViewById(R.id.activityContainer);
container.addView(view);
super.setContentView(view);
Toolbar toolbar =drawerLayout.findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
NavigationView navigationView=drawerLayout.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.menu_drawer_open, R.string.menu_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
return false;
}
protected void allocateActivityTitle(String titleString){
if(getSupportActionBar()!=null){
getSupportActionBar().setTitle(titleString);
}
}
}
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5235)
at android.view.ViewGroup.addView(ViewGroup.java:5064)
at android.view.ViewGroup.addView(ViewGroup.java:5004)
at android.view.ViewGroup.addView(ViewGroup.java:4976)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:687)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:175)
at com.example.mediapipeposetracking.DrawerBaseActivity.setContentView(DrawerBaseActivity.java:27)
at com.example.mediapipeposetracking.DashboardActivity.onCreate(DashboardActivity.java:17)
Related
I deleted a UI package which contained my fragment classes.
The UI package I deleted
I then created new classes of the same name in the bustracker package
What my project set up looks like now
When I run the app I get the following error.
caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.bustracker.ui.nearbyStops.nearbyStopsFragment" on path: DexPathList[[zip file "/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.bustracker-cuWeKLTM_oUFXduWOdcaDA==/lib/x86, /system/lib, /vendor/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 55 more
My question is, how do I get it to no longer look for com.example.bustracker.ui.nearbyStops.nearbyStopsFragment, but instead look for com.example.bustracker.nearbyStopsFragment?
Project has 4 classes, all of which you can find below.
MainActivity.java
import android.os.Bundle;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.annotation.NonNull;
import androidx.core.view.GravityCompat;
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.appcompat.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/*FloatingActionButton fab = 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();
}
});*/
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// 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_nearby_stops, R.id.nav_saved, R.id.nav_search)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_nearby_stops:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new nearbyStopsFragment()).commit();
break;
case R.id.nav_saved:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new savedFragment()).commit();
break;
case R.id.nav_search:
getSupportFragmentManager().beginTransaction().replace(R.id.nav_view, new searchFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
nearbyStopsFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class nearbyStopsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_nearby_stops, container, false);
}
}
savedFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class savedFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_saved, container, false);
}
}
searchFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
public class searchFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_search, container, false);
}
}
It's hard to know without seeing the code, but it looks like something in BaseDexClassLoader is trying to call something in the fragment file on line 125.
Inside your activity/fragment you will have, on top of the class, a lot of imports of packages you are using. There should be an import that says something like this:
import com.example.bustracker.ui.nearbyStops.nearbyStopsFragment;
Just change that into this new one:
import com.example.bustracker.nearbyStopsFragment;
If this doesn't work please provide your JAVA code with imports.
I faced this problem when I converted some Java classes to Kotlin, So I find out that I need to add id "kotlin-android" plugin and implementation "androidx.core:core-ktx:1.7.0" in Module gradle and classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10" in project gradle
Nothing happens when I click the "start" button, the app suddenly closes. I think the View.OnClickListener doesn't work, but I can't find how to fix it.
package com.example.myquiz;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView title;
private Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
title = findViewById(R.id.main_title);
start = findViewById(R.id.ma_startB);
Typeface typeface = ResourcesCompat.getFont(this,R.font.blacklist);
title.setTypeface(typeface);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,CategoryActivity.class);
startActivity(intent);
}
});
}
}
I got this error in Logcat:
error screenshot
This is the code for the CategoryActivity class, this is where I get the error from, but I really don't see where the problem is.
package com.example.myquiz;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.GridView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class CategoryActivity extends AppCompatActivity {
private GridView catGrid;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Categories");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
catGrid = findViewById(R.id.catGridView);
List<String> catList = new ArrayList<>();
catList.add("Cat 1");
catList.add("Cat 2");
catList.add("Cat 3");
catList.add("Cat 4");
catList.add("Cat 5");
catList.add("Cat 6");
CatGridAdapter adapter = new CatGridAdapter(catList);
catGrid.setAdapter(adapter);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if(item.getItemId() == android.R.id.home)
{ CategoryActivity.this.finish();}
return super.onOptionsItemSelected(item);
}
}
The click listener is working correct, looking at the error log the launching of the CategoryActivity class is causing the app crash:
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor...
This tells the ActionBar / ToolBar inside the CategoryActivity is setup twice. Probably (if not, please share the class code) you can either remove setSupportActionBar() or change the theme of the CategoryActivity to something like: Theme.MaterialComponents.Light.NoActionBar to see if you get the Activity running after the button click.
I'm creating a Material design tab view. I'm getting an error after addFragment method on my Main Activity. This is the error that I'm getting.
Wrong 1st argument type. Found: 'com.example.sa.tabproject.LatestPromoFragment, ' android.supportv4.app.Fragment'
My MainActivity.java
import android.app.Activity;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.widget.Toolbar;
import android.app.Fragment;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
tabLayout=(TabLayout)findViewById(R.id.tab_layout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter= new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(new LatestPromoFragment(),"Latest Promos"); // new LatestPromoFragment() should be in getItem() otherwise it will cause crashes
viewPagerAdapter.addFragment(new MapViewFragment(),"Map View"); // new MapViewFragment() should be in getItem() otherwise it will cause crashes
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}}
ViewPagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.ArrayList;
import java.util.List;
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragment = new ArrayList<>(); // this line can cause crashes
ArrayList<String> tabTitles= new ArrayList<>();
public void addFragment(Fragment fragment,String titles){
this.fragment.add(fragment); // this line can cause crashes
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int position) {
return fragment.get(position); // this should create the Fragment instances
}
#Override
public int getCount() {
return fragment.size();
}
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}}
I imported several fragment classes to the MainActivity.java. But it doesn't fix the error.
This is because in adapter class you are using support fragment i.e import android.support.v4.app.Fragment; and in Activity class you are using app fragment import android.app.Fragment;
use same type of fragment every where it will work.
If you use android.support.v4.app.FragmentPagerAdapter, you must also use android.support.v4.app.Fragment for all Fragments.
If you want to use android.app.Fragment native Fragments with a FragmentPagerAdapter or FragmentStatePagerAdapter, you need to use the v13 support library versions of the adapter, i.e. android.support.v13.app.FragmentPagerAdapter.
You can see this in the documentation if you look at the getItem() for each:
V4:
http://developer.android.com/intl/en/reference/android/support/v4/app/FragmentPagerAdapter.html#getItem(int)
V13:
http://developer.android.com/intl/en/reference/android/support/v13/app/FragmentPagerAdapter.html#getItem(int)
Replace your import android.app.Fragment; code
with
import android.support.v4.app.Fragment;
Below is my code, and whenever i run it, it would crash........
I'm trying to put a list of Strings into a ListView, but whenever i do i get the error message:
Fatal EXCEPTION: Main
Unable to start activity ComponentInfo{com.ss.website/com.ss.website.MainActivity}: java.lang.ClassCastException: android.widget.SlidingDrawer cannot be cast to android.support.v4.widget.DrawerLayout at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2338)
Thanks in advance.
package com.ss.website;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
private DrawerLayout drawer;
private ListView listview;
private String[] planets;
private LinearLayout linearLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.slidingDrawer1);
planets=getResources().getStringArray(R.array.planets);
linearLayout = (LinearLayout)findViewById(R.id.content);
listview= (ListView) findViewById(R.id.listView1);
listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planets));
}
}
Simple as it is: DrawerLayout is no subclass nor superclass of SlidingDrawer.
I tried to have a view of an ActionBarActivity opening after hitting a spinner button.
There are two items on my spinner, the second one runs fine, but when I tried to access the Categorias item the app throws me a NullPointerException inside the DrawerActivity class. I don't actually know where the problem is. Another ActionBarActivity extension class that I have runs perfectly.
I'm new to Android/Java development.
The Spinner Fragment
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerFragment extends Fragment {
private static Spinner spinner;
private int selected;
private View mView;
static void setSpinnerContent( View view ){
spinner = (Spinner) view.findViewById(R.id.spinner1);
return ;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_spinner, container, false);
// Now use the above view to populate the spinner.
setSpinnerContent( view );
/**
* Maneja las acciones seleccionadas del Spinner
*/
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
selected = spinner.getSelectedItemPosition();
switch(selected){
case 1:
Intent categorias = new Intent( );
categorias.setClass( getActivity() , NavigationDrawerActivity.class );
startActivity(categorias);
break;
case 2:
Intent convenios = new Intent();
convenios.setClass(getActivity(), ConveniosFragment.class);
startActivity(convenios);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
return view;
}
}
The Navigation Drawer Activity extends a ActionBarActivity
package inmostla.ligatangamanga.pruebaintegrar.navigationdrawer;
import inmostla.ligatangamanga.pruebaintegrar.navigationdrawer.NavigationDrawerFragment;
import inmostla.ligatangamanga.pruebaintegrar.R;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class NavigationDrawerActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigation_drawer);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}...
Maybe that's because getActivity() returns null. Try overriding onAttach() and keep Activity reference as a field in your class. Use this reference instead of getActivity() when you needed a reference to context or activity.