I'm newbie in Android programming.
I've read many tutorial in the Internet about Sliding Menu on https://github.com/jfeinstein10/SlidingMenu.
1. Import library in my project.
2. Create an .xml in layout folder.
3. Modify MainActivity like this:
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends SlidingActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.copyactivity_main);
getSlidingMenu().setBehindOffset(100);
}
#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;
}
It have no error. But when I debug it in emulator. It stopped and have the error: Source not found.
I don't know what it is.
Any help, thanks.
Related
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.
package rms.example.com.rms;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class CaddActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cadd);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cadd, 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);
}
}
please help me in my code as i receive error in cadd (cannot resolve symbol cadd) .
my other classes also have the same problem.
this is my uni project and i am new to android programming
thank you for your support
In Android Studio, from the top toolbar select Build => Rebuild Project. When it ends, the error should be fixed.
If it is not, have a look which word in the code is highlighted in red. If it is in this line:
getMenuInflater().inflate(R.menu.cadd, menu);
Make sure you have a resource in the menu directory called 'cadd'. If not and you do not need it, just remove the line.
Check if you have cadd named menu_layout in res/menu folder.
If yes then try clean and Rebuild your project.
From the following example create cadd file for your menu.
As you define here..
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cadd, menu);
return true;
}
cadd
<menu 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"
tools:context=".CaddActivity">
</menu>
is cadd resource file have in your menu directory?i think the file is not there so try to add cadd menu resource file other wile use default menu_main file in place of cadd
it was solved,
it was that i forgot to declare in the menu
I'm an Android newbie working with the Eclipse IDE and the Android SDK. Anyway, I've watched a few tutorials and I can't seem to be able to call the findViewById() function.
I would appreciate if you could instruct me how to use it, if it's a method of a static class or something else.
Thanks, I'm sure this won't be a problem for the more advanced users!
Well the in response to the comments I'm trying to use it in an Activity.
Here's the code in there:
package tk.quiero.test1;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
#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.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);
}
}
Where exactly am I supposed to use it?
Thanks
findViewById is defined in the Activity class, so it sounds like you're trying to call this from outside of the Activity. If so, you should post what exactly you're trying to do because there are ways of doing this, but there's also a high likelihood that there's a cleaner or easier way than resolving UI elements outside of the Activity
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.xxxxx)
}
replace xxxxx with the id that you've defined in your xml...which should look like #+id/xxxxx
This is weird. I had recently gotten into Android programming. I made a fresh install but when I create a new project there is this new option called Fragment Layout Name at the very end of the page where you specify your Activity name. I haven't had this happen before but when I open my main activity this is what I get. Also half of it is filled with errors according to Android. Is there anyway I can go back and avoid this?
package com.example.quizactivity;
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;
}
}
}
Do as follows, this works for me..
Step-1:
Right Click on your Project -> Properties -> Android -> In Library panel, remove appcompat_v7 library, Apply and Ok
Step-2:
In Project goto res -> values -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> change parent value from Theme.AppCompat.Light to android:Theme.Light
Step-3:
In Project goto res -> values-v11 -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> change parent value from Theme.AppCompat.Light to android:Theme.Holo.Light
Step-4:
In Project goto res -> values-v14 -> style.xml
In line <style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar"> change parent value from Theme.AppCompat.Light.DarkActionBar to android:Theme.Holo.Light.DarkActionBar
Step-5:
In Project goto menu -> main.xml remove these lines in menu tag:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.test.MainActivity"
and in item tag change this line from app:showAsAction="never" to android:showAsAction="never"
In project, goto res -> layout -> delete fragment.xml
Step-6:
In MainActivity extends Activity not ActionBarActivity and finally your MainActivity.java after remove unnecessary code, looks like this:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#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.main, menu);
return true;
}
}
Enjoy:)
When creating a new project Select Minimum SDK version as upper or higher as u can . and then u will get rid of app compact .
I have chosen Miminum SDK 4.0 and get rid of it .
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