R.layout.activity_starting_point can not be resolved [duplicate] - android

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 8 years ago.
I just started coding in android and i am stuck at this point my code is simple its showing error in only these lines.
in the line setContentView(R.layout.activity_starting_point); its showing error that activity_starting_point can not be resolved or is not a field. why so please help!
package com.thenewboston.abhi;
import android.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class StartingPoint extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter=0;
add = (Button)findViewById(R.id.button1);
sub = (Button)findViewById(R.id.button2);
display = (TextView)findViewById(R.id.text1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_starting_point, container, false);
return rootView;
}
}
}

check your build target, delete import Android.R, clear your project, and check the error log for more information about what is causing your app to not generate the R class

Delete import android.R; as that is the Android API's R and not your project's one and is masking yours. You need your one as it contains your resources.
Try and see if auto-complete can find the target layout too. Type setContentView(R.layout. and see what options you are given. (You may need to invoke the auto-complete. It's Ctrl + Space on Eclipse. I don't know the shortcuts for Studio.)
Whenever you want to access the Android API's R.java you should fully qualify it with android.R.layout.whatever so it doesn't get auto-imported again and mess with your local resources.
If this doesn't work can you confirm that you have an R.java under the /gen/your/package/path folder? Perhaps try refreshing and rebuilding the project too.
If there are errors in your project R may not be able to successfully build. You should check your lint errors or other error logs and again refresh, rebuild, restart after sorting those.
Note that errors in your XML may not be picked up by eclipse so you should pay careful attention to those files. Just because the XML syntax is ok does not mean it is valid Android XML.

Related

Cannot Resolve R.menu and other R. stuff in Android Studio

I have been practicing in Android Studio as per the android app development free course in UDACITY. And wherever there is a R.menu or R. stuff it cannot be resolved
package com.example.android.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.R;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
}
Plz help how to solve this Issue.
Clean and rebuild your project by going to Build>Clean Project.
Go to File>Invalidate Caches/Restart.
Make sure you built your project, and make sure there aren't any errors (red lines) in the res directory.
remove import android.R; clean and rebuild it.
if it still doesn't work try this
import com.example.android.courtcounter.R;
Check you AndroidManifest.xml, check the package name there.
use that as import .R; in code.

Android: going back to first fragment by pressing home button

I am working on a simple android application that has MainActivity with FragmentStatePagerAdapter and some number of fragments side by side. I want to be able to go to the first fragment by pressing home button.
I know my code is pretty bad. Still I hope just a few lines will solve my problem.
Here is my MainActivity
package com.freestylers.druskischool;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
ViewPager pager;
FragmentStatePagerAdapter adapter;
* Each page of our pager will display one fragment from this array
* Swiping, to the right will take you to the next page
*/
String[] fragments={
"start",
"mes",
"whatever",
"next",
"name",
"of",
"a",
"fragment",
"more fragments",
"and",
"more",
"fragments"
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager=(ViewPager)findViewById(R.id.my_pager);
adapter=new FragmentStatePagerAdapter(
//maybe should use normal getFragmentManager()
getSupportFragmentManager()
){
#Override
public int getCount() {
// This makes sure getItem doesn't use a position
// that is out of bounds of our array of fragments
return fragments.length;
}
#Override
public Fragment getItem(int position) {
// Here is where all the magic of the adapter happens
// As you can see, this is really simple.
//(fragments[position]);
if(position==0){
return StartFragment.newInstance();
}
else if(position==1){
return MesFragment.newInstance();
}
else if(position==2){
returnNextFragment.newInstance();
}
//this goes on until position==11
else{return StartFragment.newInstance();}
}
};
//Let the pager know which adapter it is supposed to use
pager.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.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
And this is one of my fragments, MesFragment. On the line 19 (commented out by double slash)I just don`t know which command to use to get to my first fragment:
package com.freestylers.druskischool;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class MesFragment extends Fragment {
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case android.R.id.home:
//(getActivity()).getSupportFragmentManager().??? I don`t know how
to go back to first fragment
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
(getActivity()).getActionBar().setDisplayHomeAsUpEnabled(true);
View view=inflater.inflate(
R.layout.fragment_mes,
container,
false);
return view;
}
// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(){
MesFragment f=new MesFragment();
return f;
}
}
Thanks
You can set the currently selected item using ViewPager, which is in your activity:
public void switchToMesFragment() {
pager.setCurrentItem(1);
}
To switch from within a fragment, you need to get a reference to your activity and call the switching function, like so:
((MainActivity)getActivity()).switchToMesFragment();
You cannot completely control the Home button key since it is a key that is broadcasted to all active apps on the device. If you want to process the Home button for your app, you may overrride the Activity.onPause() method. Documentation # Pausing an Activity. But still you cannot prevent the user from seeing the Home screen, that's not fair to other apps perhaps. Try this out first...

New Android Project with Eclipse (Explanation of the Code)

I am new to programming Android. When I start a new Android Application Project in Eclipse it automatically opens 2 files for me:
MainActivity.java and fragment_main.xml
package com.example.testprogram;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
and
<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="com.example.testprogram.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I wonder what all this stuff is. The Tutorials I watch always have a different code when they start a new project. For example the .xml file that Eclipse opens for them is named activity_main.xml (unlike mine which is named fragment_main.xml). Also their MainActivity.java has less methods integrated then mine.
Can Someone tell me wether that makes a huge difference and how i can maybe change that.
You are using latest one so it get like this. Don't worry about it.
public class MainActivity extends ActionBarActivity
instead of
public class MainActivity extends Activity
Remove all code in your MainActivity except OnCreate(). Then you follow your tutorial.
You onCreate should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Those are probably old tutorials (on Android platform even after couple of months an tutorial can become old), Android ADT is constantly changing. It now automatically adds an Activity and a Fragment to your project. Instead of just an Activity. You can still use knowledge from those tutorials and fit it into Fragments. Or delete Fragments and use only Activities. But Android is promoting the use of Fragments with Activities, not just mere Activities so you should learn how to use them.
Create New Project Like This:
Start `File->New->Android Application Project->Next->Next->Next->Select-> Empty Activity->Next->Finish

I don't know what is appcompat v_7 and why is it being add in every time when i create a new project ?

this code is about the default code of MainActivity.java
having problem to understand and also there is a another thing that is the fragmentaion_main.xml file which is likely another buden instead of activity_main.xml why it so ?
package com.example.onehello;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
What is appcompat_v7?
It is the support library added for backwards compatibility. Android has many versions and each new version brings many changes to the API. So, for the older versions of Android to be compatible with the code written for the newer versions, the appcompat_v7 library is helpful.
Why is it being added every time I create a new project?
I assume that you are using Eclipse IDE for the development. Ideally, it would suffice to create one appcompat_v7 folder and make all the projects link to that folder for support library. But, there seems to be some bug in Eclipse. It creates a new appcompat_v7 folder each time a new project is created.
'fragment_main.xml' is like another burden?
Fragment class was introduced in Android API 11. It is useful in creating different layouts for tablets and phones. It is more of a good support to take advantage of the bigger screens on tablets. If you want to develop your application for a smart phone only, you need not even worry about it. So, it's more of an advantage than a burden.
For more reading on fragments, go to http://developer.android.com/reference/android/app/Fragment.html
This appears after installation "Android Support Library":
https://developer.android.com/tools/support-library/features.html

Fragments and Activities -- where do I put my application logic?

I have created my view how I want it to look. It has 1 images, an input box, and a button. I will want to load another activity when the button is clicked. I am confused why there are fragments and activities. I am new to the Android world (coming from iOS).
My understanding is that Activities are similar to ViewControllers, but I am not sure I understand what a fragment is.
Where do I put the event handling?
package com.phppointofsale.phppointofsale;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class StoreUrlActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_url);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StoreUrlFragement()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.store_url, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class StoreUrlFragement extends Fragment {
public StoreUrlFragement() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_store_url,
container, false);
return rootView;
}
}
}
Firstly I would recommend reading this Fragments . Pay particular attention to the created fragment section, which includes the fragment life-cycle diagram. Second download and compile this Sample App,the effective navigation app will help you understand how different fragments work in tandem, and even implements a action bar.
To answer your question more or less a fragment can be thought of as a separate class. Once you call upon that particular fragment you can call functions from within that class.
Fragment Case-Switch
This is some sample code to show you what I mean.
public Fragment getItem(int i){
switch (i) {
case 0:
// The first section of the app is the most interesting -- it offers
// a launchpad into the other demonstrations in this example application.
return new LaunchpadSectionFragment();
case 1:
return new BluetoothClass();
default:
// The GPS section of the app .
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
fragment.setArguments(args);
return fragment;
}
}
In this case each fragment for me represented a class, which was implemented in a separate tab and each tab had a separate functionality. One of the key advantages of fragments is you can run separate activities without first letting one activity complete.
Furthermore each fragment is an extension of the java.lang.Object library. So it has all those functions + additional ones. I would read this as well. Lastly it would be a good idea to have separate xml files for each fragment then you can display that separately when a fragment is invoked.
Some more code
Each fragment will/could have this
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
// Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners
View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do something
}
});
}
public void onStart(){
super.onStart();
Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
}
public void onResume(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();
}
public void onStop(){
super.onStart();
Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
disableBT();
}
Remember these functions are from the fragment life-cycle I mentioned earlier.
Hopefully that gave you some idea on fragments. Also remember to read this as a lot of functionality uses the v7 app compat library. Including the fragment manager.

Categories

Resources