Cannot resolve symbol 'activity_main' in android studio [duplicate] - android

This question already has answers here:
Android - activity_main cannot be resolved or is not a field
(11 answers)
Closed 6 years ago.
I'm facing a problem. I am getting the error "Cannot resolve symbol 'activity_main'. I read the other posts about it, and could not figure out how to fix it. Here is my code :
package com.grasland.musicplayer.Activities;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import com.grasland.musicplayer.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
}
}
There is a screen of my project's tree :
Tree project
Thanks !

Make sure that the name of the main activity's layout file is "activity_main". If its not, rename it to "activity_main". If the name is already correct, try Build → Clean Project.

Related

error: cannot find symbol variable viewpager ---Androidx

I have the following code, the project is migrated to andoidx, R.id.viewpager returns can not find symbol variable viewpager SimpleFragmentPagerAdapter is a custom(an extention of)
FragmentPagerAdapter and is working well
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the content of the activity to use the activity_main.xml layout file
setContentView(R.layout.activity_catagory);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
SimpleFragmentPagerAdapter adapter = new SimpleFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
}
}

FrameLayout.LayoutParams doesn't work

I am trying to avoid the following situation in the activity( screen shot of the activity). When I add the margins using setMargins it does not work. I have tried adding margins via xml code and the end result is achieved but it very time consuming and it can't be guaranteed that it will work for all devices. How can i do it in code?
Here is the code.
package com.example.android.tourguide;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class TouristAttractions extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private NavigationView mNavigationView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_item);
/*
Finding the Toolbar that is defined in activity_main.xml via the id toolbar.
Note:The following three lines should be repeated for all the activities that are opened from the MainActivity. Also the toolbar should have an orientation
which is not zero.
*/
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar_for_menu_items);
/*
Setting the action bar as the toolbar defined in the above code line
*/
setSupportActionBar(toolbar);
/*
Setting the title text color of the app bar.
*/
toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.color_of_text_of_app_bar));
mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
/*
Finding the drawerLayout so that when the user clicks on the menu item of the navigation drawer it should close as we invoke the method closeDrawers()
*/
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
if (menuItem.getItemId() == R.id.home_menu) {
/*
Opening the home class that is the MainActivity when the Tourist Home menu button is clicked.
*/
Intent intentToOpenHomeClass = new Intent(TouristAttractions.this, MainActivity.class);
startActivity(intentToOpenHomeClass);
} else if (menuItem.getItemId() == R.id.entertainment_menu) {
Intent intentToOpenEntertainmentClass = new Intent(TouristAttractions.this, Entertainment.class);
startActivity(intentToOpenEntertainmentClass);
} else if (menuItem.getItemId() == R.id.gardens_menu) {
Intent intentToOpenGardenClass = new Intent(TouristAttractions.this, Garden.class);
startActivity(intentToOpenGardenClass);
} else {
mDrawerLayout.closeDrawers();
}
return true;
}
});
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout_for_menu_items);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager_for_menu_items);
AdapterForFragmentOfTouristAttraction adapterForFragmentOfTouristAttraction = new AdapterForFragmentOfTouristAttraction(this, getSupportFragmentManager());
viewPager.setAdapter(adapterForFragmentOfTouristAttraction);
tabLayout.setupWithViewPager(viewPager);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) toolbar.getLayoutParams();
params.setMargins(0, toolbar.getHeight(), 0, 0);
ActionBar actionbar = getSupportActionBar();
/*
actionBar.setDisplayHomeAsUpEnabled(true) will make the icon clickable and add the < at the left of the icon.
*/
actionbar.setDisplayHomeAsUpEnabled(true);
/*
Enabling the menu icon of the navigation.However note we are simply adding the menu icon however clicking on the icon does absolutely nothing.
*/
actionbar.setHomeAsUpIndicator(R.drawable.baseline_menu_white_24);
}
}
First of all, it might be a better idea to use a LinearLayout with a vertical orientation, a RelativeLayout, or a ConstraintLayout to position views below one another. In more exotic use cases even a CoordinatorLayout might be better suited.
FrameLayouts are mostly used to simply overlay views on one another, without depdendencies between them.
This said, the issue you are facing occurs because you use toolbar.getHeight() right after adding the view, which will always be 0. Try attaching a debugger and see for yourself!
The reason is that Android needs a bit of time to measure -> layout -> draw its views, and if you call getHeight right after adding the layout it will not have done either of those steps, leaving the values uninitialized, at 0.
There are ways around that, but again, you would be better off using a different layout alltogether. If you insist on using a FrameLayout the cleanest approach would be to extend it and create your own, where you can measure and layout the view yourself. The hacky, hard to maintain, and confusing approach would be to use ViewTreeObserver to listen for the changes and react to them. This is bad because you have to wait for a full layout pass before you trigger yet another one.
Don't use a FrameLayout here.
You havn't Done
yourView.setLayoutParams(params);

cannot resolve symbol fragmentcontainer

So I've decide to go back into android developing after dropping it for a bit. I Restarted making an old project in android studio I ran into a issue where I'm getting "cannot resolve symbol fragmentcontainer" and I'm sure it was working last time.
import android.support.v4.app.FragmentManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// add fragment to the activity
FragmentManager fm = getSupportFragmentManager();
// give fragment to manage
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if (fragment == null) {
fragment = new HomeFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment)
.commit();
}
}
}
It looks like your Activity layout R.layout.activity_main does not contain a view with id fragmentContainer. If that's not the issue, check this related question: Android Studio cannot resolve symbol but code executes correctly.
just using "fragment_container" not "android.R.id.fragment_container" works for me...here is the detail
getFragmentManager().beginTransaction()
.replace(fragment_container, new SettingsFragment())
.commit();
I think you can use
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commitNow(); instead of using R.id.fragmentContainer to create fragment in an activity.

Exception NoClassDef founderror android

I have impelemented the actionbar using fragementactivity for view pager action bar using swipe working fne in api leve1 17 or above but coming to the lower version 2.2 to 4.0.3 it showing noclassdef error it is tourching so much please help me to how to retify this code pls help....
target version is 17 The person who solves answer i wll give party dnt miss my party get ready for answer
this is my code
public class Cbntabs extends FragmentActivity implements ActionBar.TabListener{
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
ActionBar actionBar;
String result;
// Tab titles
private String[] tabs = { "CBN Voice", "CBN Message" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.tabs);
Bundle bundle = getIntent().getExtras();
if(bundle!=null)
{
result=bundle.getString("results");
}
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager(),result);
/**/
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.header_bg));
actionBar.setTitle("TELUGU DESAM PARTY ELITE CLUB");
View customNav = LayoutInflater.from(this).inflate(R.layout.custom_show_action_bar, null);
viewPager.setAdapter(mAdapter);
you should use getSupportActionBar.
just import the library project into your workspace
Path to import project is : Android SDK -> Extras -> android ->support - > v7 -> appcompat and the after importing this project right click on your project -> select properties -> android -> add library and he just add the imported library here

PreferenceFragment can't load from XML (ClassCastException)

I'm having some difficulties getting a PreferenceFragment to load using XML.
It keeps throwing a ClassCastException "PreferenceFragmentClass cannot be cast to android.support.v4.app.Fragment".
The code is intended to run on API14 and higher.
Here's my code:
import android.os.Bundle;
import android.preference.PreferenceFragment;
public class SettingsFragment extends PreferenceFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Here's the code where SettingsFragment is being used in:
import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class FragmentsSetup extends FragmentActivity {
private ViewPager viewPager;
private TabsAdapter tabsAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
viewPager = new ViewPager(this);
viewPager.setId(R.id.pager);
setContentView(viewPager);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
tabsAdapter = new TabsAdapter(this, viewPager);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.forwarding_tab).toUpperCase()), ForwardingFragment.class, null);
tabsAdapter.addTab(actionBar.newTab().setText(getString(R.string.settings_tab).toUpperCase()), SettingsFragment.class, null);
}
}
Any tips would be greatly appreciated.
SettingsFragment inherits from PreferenceFragment. PreferenceFragment is from the native API Level 11 implementation of fragments. However, FragmentsSetup is inheriting from FragmentActivity, from the Android Support package's backport of fragments.
That combination will not work.
If you intend to support devices older than API Level 11, you cannot use PreferenceFragment. Also, I am uncertain if PreferenceFragment works outside of a PreferenceActivity (it might, but I have never tried that).
If you're developing for Android 3.0 (API level 11) and higher, you should use a PreferenceFragment to display your list of Preference objects.
Also you can add a PreferenceFragment to any activity—you don't need to use PreferenceActivity.
See documentation here:
https://developer.android.com/guide/topics/ui/settings.html?

Categories

Resources