I've just done a fresh re-install/extract of Eclipse off Android's website, hardly added much code or XML to my project, yet nothing's being recognized outside of my MainActivity.java. And before you ask-yes, I've tried cleaning and restarting Eclipse.
(see comments for where the errors are) For example, in the first method (onCreate), the second like there's activity_main that Eclipse is saying it can't resolve, as if it doesn't exist. Consequently, my CreateLG button in the onCreateLGClick method isn't resolved/recognized either. Neither is the main (.xml) or createlg_menu (.xml) resolved..
MainActivity.java:
package com.example.groceryrunner;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.PopupMenu;
import android.R;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // not resolved or isn't a field
}
#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); // main isn't recognized
return true;
}
public void onCreateLGClick(View v) {
final int id = v.getId();
switch (id) {
case R.id.CreateLG: // CreateLG button from activity_main isn't recognized either
//findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGPopup(v);
break;
/*case R.id.ListsButton:
findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE);
createLGMenu(v);
break;*/
}
}
public void createLGPopup(View v) {
PopupMenu LGMenu = new PopupMenu(this, v); // createlg_menu isn't recognized as well
LGMenu.getMenuInflater().inflate(R.menu.createlg_menu, LGMenu.getMenu());
LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
String choice = new String((String) item.getTitle());
if (choice == "Create_List") {
//createListDialog();
}
else if (choice == "Create_Group") {
//createListDialog();
}
return false;
}
});
LGMenu.show();
}
}
Here's the other files:
activity_main.xml:
<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" >
<Button
android:id="#+id/CreateLG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="37dp"
android:layout_marginTop="21dp"
android:text="+"
android:textSize="40sp" />
</RelativeLayout>
createlg_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/createList" android:title="Create List"></item>
<item android:id="#+id/createGroup" android:title="Create Group"></item>
</menu>
main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
</menu>
When i encounter this issue, the first step is to remove the import android.R line. That is the wrong R.
Next, comment out any line referencing R and build the project. That will make new gen folder with the R values.
Most likely your issue is importing android.R so the app looks in there instead of in your R gen file. But Eclipse gets silly sometimes and won't build without commenting those out, so try both steps to be sure.
Related
I'd like to make a menu with a delete option. The actual delete functionality isn't made yet because at the moment I can't see the top bar in my app.
Main layout (activity_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<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="billy.cs436.placebadgesapp.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/newPlace"
android:text="#string/newPlaceButton"
/>
</RelativeLayout>
Menu layout (main.xml):
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/deleteMenu"
android:icon="#drawable/clear"
android:title="#string/deleteMenu"
app:showAsAction="ifRoom"
/>
</menu>
Main activity (MainActivity.java):
package billy.cs436.placebadgesapp;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
Button newPlace;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newPlace = (Button) findViewById(R.id.newPlace);
newPlace.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), setLocation.class);
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);//Menu Resource, Menu
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.deleteMenu) {
// if there are no badges, toast message saying so (needs implementing)
Toast.makeText(this, "There are no badges to delete!", Toast.LENGTH_SHORT).show();
//else clear all badges
} else {
Toast.makeText(this, "Badges cleared!", Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Can anyone tell my why the New Place button is the only thing that shows up when this is run?
EDIT:
#T.S has the correct answer. Thank you.
Change your class to extend AppCompatActivity instead of Activity.
I'm trying to add button to the Action Bar with no luck. There are no errors. Button is in the #drawable. But it doesn't show up.
What am I doing wrong?
(It asks me to add more details, but I have nothing more to add)
test activity :
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
public class test extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
setupCREATEbut();
}
private void setupCREATEbut() {
Button CREATEbut = (Button) findViewById (R.id.appenter2);
CREATEbut.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent,0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topmenu, menu);
return super.onCreateOptionsMenu(menu);
}
}
test xml :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="rs.test">
<LinearLayout
android:id="#+id/centerblock"
android:layout_width="400dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relcenterblock"
android:layout_width="fill_parent"
android:layout_height="415dp" >
<Button
android:id="#+id/appenter2"
android:layout_width="160dip"
android:layout_height="160dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/bigroundbutton"
android:gravity="center"
android:text="#string/request"
android:textColor="#ffffff"
android:textSize="30sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
topmenu xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"/>
</menu>
Try to use showAsAction=always or showAsAction=ifRoom.
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_toprightmenu"
android:title="#string/toprightmenu"
yourapp:showAsAction="always"/>
</menu>
I am getting this error. It says "edit_message can not be ressolved or is not a field" Can somebody tell me why I am getting this error is how can I fix it.
LAYOUT OF MAIN ACTIVITY: -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/edit_message" />
</LinearLayout>
CODE OF MAIN ACTIVITY
package com.practice.firstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE= "com.practice.fristapp1.MESSAGE";
#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;
}
/** Called when the user clicks the Send button */
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message); ///********ERRORR***********
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
LAYOUT OF SECOND ACTIVITY: -
<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=".DisplayMessageActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.firstapp1;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
public class DisplayMessageActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
STRING RESOURCES:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="title_activity_display_message">My Message</string>
<string name="hello_world">Hello world!</string>
</resources>
It looks like your code is right. Check the following things:
1) Save the layouts in xml, one reason could be its not saved.
2) Check there is no error in your xmls and project is build.
3) If you are using eclipse, go to Projects->Clean All Projects and then projects->Build Automatically.
4) Open your activity and press "cmd + shift + o" or ctrl+ shift+o (in windows). It shall import the R.java, select the com..R in the options.
I hope it shall solve the error.
You're missing import com.practice.firstapp1.R in MainActivity.java.
See the docs on Accessing Resources for more information.
**I want to click on a button in my fragment 2 , and replace fragment 1 with fragment 3 as shown in the code.
But the findViewById(r.id.mybutton) is returing 'null' .
I tried debugging the code but its not able to use the created 'buttontoggle'
Hope you can help me out with the above problem
Thanks in advance :)
package com.vivekmishra1991.testfrag;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(findViewById(R.id.fragment_container)!=null){
if(savedInstanceState!=null){
return;
}
//fragment 1
f1 F1= new f1();
F1.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, F1).commit();
//fragment2(the fragment with the button)
f2 F2=new f2();
F2.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container1, F2).addToBackStack(null).commit();
}
//code for buttton onclick function
Button toggleButton = (Button)findViewById(R.id.mybutton);
toggleButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{ // fragment 3 that has to be replaced with fragment 1
f3 F3=new f3();
F3.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,F3).addToBackStack(null).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;
}
}
EDIT
Here is my activityMain.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="#+id/fragment_container"
android:layout_height="0dp"
android:layout_weight="6"
android:layout_width="match_parent" />
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_width="match_parent"/>
</LinearLayout>
f2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Next!"
android:id="#+id/mybutton"
android:layout_gravity="left|center_vertical"/>
</LinearLayout>
You can check your .xml file to make sure that "mybutton" does exist.
If so, try Project > Clean. Occasionally, eclipse doesn't add the button to the R.java file, and cleaning the project tends to fix that. If its not that either, you can always try to recreate the button in your xml file.
I've looked at the other threads on this topic, and none of them have helped. I'm using an appropriate minSdkVersion, my menu xml appears setup correctly, my Manifest is calling Them.Holo.Light, and all of the other "typically overlooked" problems (that I'm aware of from other threads, anyway).
I'm using Eclipse with the latest SDK and following the basic tutorial from the Android dev wiki. Eclipse isn't showing any errors.
As you'll see, I'm referencing main.xml and main_activity.xml. main.xml is showing the default view when the app loads, which is a simple header bar and a picture of two ponies. The idea is that the user can use the context menu to swap between the pony picture and one of Lionel Richie (hey, it's a test app).
I've tried running it on the emulator, as well as my Xoom tablet and a phone, with no luck.
Code:
/***** Contents of testIceCreamS Manifest *****/
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.icecreams"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="11" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestIceCreamSActivity"
android:theme="#android:style/Theme.Holo.Light"
android:label="#string/app_name"
android:uiOptions="splitActionBarWhenNarrow" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
/***** Contents of TestIceCreamSActivity.java *****/
package test.icecreams;
import android.app.Activity;
import android.os.Bundle;
import test.icecreams.R;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
/***** Contents of ActionBarUsage.java *****/
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ActionBarUsage extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
/***** Contents of main_activity.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/buttonone"
android:title="#string/showimage1"
android:icon="#drawable/helloicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttontwo"
android:title="#string/showimage2"
android:icon="#drawable/poniesicon"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonthree"
android:title="#string/showwhite"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfour"
android:title="#string/showblack"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/buttonfive"
android:title="#string/showalert"
android:showAsAction="ifRoom|withText"/>
</menu>
/***** Contents of main.xml: *****/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00000000"
android:orientation="vertical" >
<TextView
android:id="#+id/hellotext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:debuggable="true"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/cd"
android:src="#drawable/ponies" />
</LinearLayout>
I also have the content for strings.xml, but I figured it was probably the least important of all of these.
Any suggestions?
In fact, I understand that you want to create an application running on ICS with the layout main.xml and a contextual menu like *main_activity.xml*. You do not have to create two activities, only the main one which will be launched at application will be started.
Please try to make all in one Activity in the same file TestIceCreamSActivity.java :
package test.icecreams;
import test.icecreams.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class TestIceCreamSActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
LinearLayout bkgr = (LinearLayout)findViewById(R.id.LinearLayout1);
ImageView image = (ImageView)findViewById(R.id.imageView1);
switch (item.getItemId()) {
case R.id.buttonone:
image.setImageResource(R.drawable.ponies);
return true;
case R.id.buttontwo:
image.setImageResource(R.drawable.hello);
return true;
case R.id.buttonthree:
bkgr.setBackgroundResource(R.color.backgroundwhite);
return true;
case R.id.buttonfour:
bkgr.setBackgroundResource(R.color.background);
return true;
case R.id.buttonfive:
//The alert code goes here!
return true;
default :
return super.onOptionsItemSelected(item);
}
}
}
Please also change your target SDK version to level 14 (ICS 4.0 -> 4.0.2) or 15 (4.0.3) in your manifest :
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="15" />