i'm a noob developer...I hope that someone can help me with this problem.
I'm going to add a share button to my actionbar (ABS) but I've some problems
my actionbar menu XML
<?xml version="1.0" encoding="utf-8"?>
<item android:id="#+id/menu_item_share"
android:title="Share"
android:showAsAction="ifRoom"
android:actionProviderClass="com.actionbarsherlock.widget.ShareActionProvider" />
my activity
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.widget.ShareActionProvider;
public class DisPlayWebPageActivity extends SherlockActivity {
WebView webview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
...
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.actionmenu, menu);
ShareActionProvider myShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_SEND);
myIntent.putExtra(Intent.EXTRA_TEXT, "page_url");
myIntent.setType("text/plain");
myShareActionProvider.setShareIntent(myIntent);
return true;
}
relevant errors
Error:(52, 8) java: com.actionbarsherlock.internal.view.menu.MenuBuilder is not abstract and does not override abstract method getItem(int) in com.actionbarsherlock.view.Menu
Error:(663, 34) java: getItem(int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement getItem(int) in com.actionbarsherlock.view.Menu
return type android.view.MenuItem is not compatible with com.actionbarsherlock.view.MenuItem
Error:(605, 34) java: findItem(int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement findItem(int) in com.actionbarsherlock.view.Menu
return type android.view.MenuItem is not compatible with com.actionbarsherlock.view.MenuItem
Error:(443, 33) java: addSubMenu(int,int,int,int) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement addSubMenu(int,int,int,int) in com.actionbarsherlock.view.Menu
return type android.view.SubMenu is not compatible with com.actionbarsherlock.view.SubMenu
Error:(435, 33) java: addSubMenu(int,int,int,java.lang.CharSequence) in com.actionbarsherlock.internal.view.menu.MenuBuilder cannot implement addSubMenu(int,int,int,java.lang.CharSequence) in com.actionbarsherlock.view.Menu
return type android.view.SubMenu is not compatible with com.actionbarsherlock.view.SubMenu
...
ask me if you need full error log!
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_setting:
Intent next = new Intent(Home.this, Setting.class);
next.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivityForResult(next, 2);
return true;
}
return true;
}
Related
I am following a tutorial 11. Exercise: Using the contextual action mode
But I am having this error :
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
Error: The method startActionMode(ActionMode.Callback) in the type Activity is not applicable for the arguments (ActionMode.Callback)
I checked this stackoverflow answer
they said to add
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
I had this error
The method getActivity() is undefined for the type Display
what I am doing wrong ? the below is my code.
package com.example.sqlfirst;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Display extends ActionBarActivity {
private final static String TAG = "MainActivity";
protected Object mActionMode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_main);
//have to use getSupportActionBar from android.support.v7.app
// ActionBar actionBar = getSupportActionBar();
//getActionBar().setDisplayHomeAsUpEnabled(true);
ActionBarActivity activity=(ActionBarActivity)getActivity();
activity.startSupportActionMode(modeCallBack);
View view = findViewById(R.id.gridview);
view.setOnLongClickListener(new View.OnLongClickListener() {
// called when the user long-clicks on someView
public boolean onLongClick(View view) {
if (mActionMode != null) {
return false;
}
// start the CAB using the ActionMode.Callback defined above
mActionMode = Display.this.startActionMode(mActionModeCallback);
view.setSelected(true);
return true;
}
});
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Send intent to SingleViewActivity
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
// Pass image index
i.putExtra("id", position);
startActivity(i);
} });
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch (item.getItemId()){
case R.id.ic_action_person:
Toast.makeText(this, "Create a new account please", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(this, Register.class);
startActivity(intent);
return true;
case R.id.ic_action_search:
Toast.makeText(this, "Search for new images", Toast.LENGTH_SHORT).show();
Intent isearch= new Intent(this,Search.class);
startActivity(isearch);
return true;
case R.id.ic_action_picture:
Toast.makeText(this, "Search for new photos", Toast.LENGTH_SHORT).show();
Intent iphotos= new Intent(this,Display.class);
startActivity(iphotos);
return true;
}
return true;
}
private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {
// Called when the action mode is created; startActionMode() was called
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
// inflate a menu resource providing context menu items
MenuInflater inflater = mode.getMenuInflater();
// assumes that you have "contexual.xml" menu resources
inflater.inflate(R.menu.activity_main_actions, menu);
return true;
}
// called each time the action mode is shown. Always called after
// onCreateActionMode, but
// may be called multiple times if the mode is invalidated.
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return false; // Return false if nothing is done
}
// called when the user selects a contextual menu item
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_action_picture:
Toast.makeText(Display.this, "Selected menu",
Toast.LENGTH_LONG).show();
mode.finish(); // Action picked, so close the CAB
return true;
default:
return false;
}
}
// called when the user exits the action mode
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
}
};
}
Your Display class is extending ActionBarActivity, that means that it´s an Activity so there´s no need to use getActivity(), you can directly make use of the methods like this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* this method is available within your ActionBarActivity*/
startSupportActionMode(modeCallBack);
setContentView(R.layout.grid_main);
// The rest of your code comes here
}
I have two activities with two layouts and a action bar.
The minimum API I am using is 14.
I go from one activity to another with no problem using intent, and I want to make a home button in the action bar to return to the activity that I choose.
I implemented a menu and called it from xml using the guides on the android developers site, but when I click on the button/menu from the action bar, it crashes with uncaught exception.
The main activity class:
package com.example.quiz;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Button new_b;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ActionBar actionBar = getActionBar();
actionBar.show();
new_b=(Button)findViewById(R.id.new_button);
new_b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,HomeActivity.class);
startActivity(intent);
};
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.home_button:homeClick();
return true;
case R.id.info_button:homeClick();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void homeClick() {
Intent intent_home = new Intent(this,HomeActivity.class);
intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_home);
}
}
}
The home activity class:
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class HomeActivity extends Activity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
ActionBar actionBar = getActionBar();
actionBar.show();
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main_actions, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.home_button:homeClick();
return true;
case R.id.info_button:homeClick();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void homeClick() {
Intent intent_home = new Intent(this,HomeActivity.class);
intent_home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent_home);
}
}
}
Can I make it another way, or what can I do to make it work?
04-20 19:31:57.610: I/Adreno200-EGL(5708): <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build:
AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107_msm8625_JB_REL_2.0.3_CL3357771_release_AU (CL3357771)
04-20 19:31:57.610: I/Adreno200-EGL(5708): Build Date: 02/25/13 Mon
04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Branch:
04-20 19:31:57.610: I/Adreno200-EGL(5708): Remote Branch: quic/jb_rel_2.0.3
04-20 19:31:57.610: I/Adreno200-EGL(5708): Local Patches: NONE
04-20 19:31:57.610: I/Adreno200-EGL(5708): Reconstruct Branch AU_LINUX_ANDROID_JB_REL_2.0.3.04.01.02.21.107 + NOTHING
04-20 19:31:57.850: W/ResourceType(5708): getEntry failing because entryIndex 14 is beyond type entryCount 1
04-20 19:31:57.850: W/ResourceType(5708): Failure getting entry for 0x7f0b000e (t=10 e=14) in package 0 (error -2147483647)
04-20 19:32:09.390: E/dalvikvm(5708): GC_CONCURRENT freed 2965K, 22% free 12424K/15751K, paused 12ms+26ms, total 75ms
04-20 19:32:10.600: W/dalvikvm(5708): threadid=1: thread exiting with uncaught exception (group=0x41d02438)
You can use this code as an example:
MainActivity.java
package com.example.quiz;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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.activity_main_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.go_home:
Intent intentHome = new Intent(this, HomeActivity.class);
startActivity(intentHome);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
/res/menu/activity_main_actions.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/go_home"
android:showAsAction="ifRoom"
android:title="#string/go_home" />
</menu>
It turned out that I was trying to format my strings with html code.
Example:
I was using:
<string name="title"> <b>App title</b> </string>
When I was supposed to use:
<string name="title"> App title <string>
In my apps class ProjectFragActivtiy encountered exception like
java.lang.ClassCastException: com.actionbarsherlock.internal.view.menu.MenuBuilder
at com.oj.bs.ProjectFragActivity.onCreateOptionsMenu
(ProjectFragActivity.java:49).
in apps menu button is used for logout operation but encountered exception. Is there another way to implement the menu in actionbarsherlock.
Does anyone have a solution to this? Thanks in advance.
following is the ProjectFragActivtiy class
package com.oj.bs;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
public class ProjectFragActivity extends SherlockFragmentActivity {
SessionManager sessionMngr ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sessionMngr = new SessionManager(getApplicationContext());
Toast.makeText(getApplicationContext(), "User Login Status: " + sessionMngr .isLoggedIn(),
Toast.LENGTH_LONG).show();
sessionMngr.checkLogin();
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab().
setText("Residential").
setTabListener(new ResidentialFragActivity()).
setIcon(R.drawable.residential);
actionBar.addTab(tab);
tab = actionBar.newTab().
setText("Commercial").
setTabListener(new CommercialFragActivity()).
setIcon(R.drawable.commercial);
actionBar.addTab(tab);
}
public void Logout(View v) {
sessionMngr.logoutUser();
}
#Override
public boolean onCreateOptionsMenu(Menu m) {
getMenuInflater().inflate(R.menu.menu_logout, (android.view.Menu) m);
return true;
}
}
Change the onCreateOptionsMenu() as follows
#Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu_logout, (com.actionbarsherlock.view.Menu) menu);
return true;
}
NOTE :
In order to inflate your menu from XML you should call getSupportMenuInflater() in your activities. The fragment callbacks will already have an instance of the inflater as an argument.
I have made an app with a lot of buttons and activities. I'm having trouble though understanding how to start a new activity through a button that is in my menu (when the menu button is clicked on phone) (inflatable menu). This is my code for the menu connected to my activity:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1" android:title="#string/menu_home"></item>
</menu>
Here is my activities in Java:
package com.gmail.derekcraigsmith.nanaimobus;
import android.os.Bundle;
import android.app.Activity;
import android.content.ClipData.Item;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
public class Route1TimesCcMonfriAActivity extends Activity implements
OnMenuItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.route1_times_cc_monfri_a);
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.route1_times_cc_monfri_a, menu);
return true;
}
#Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
return false;
}
}
int id = item.getItemId();
switch (id) {
case R.id.your_menu_item_id : {
startActivity(new Intent(start_activity, next_activity));
}
where start activity is your main activity and next_activity is the activity you want to start.
Give a look on my blog here for more info.
activity_main.xml in menu folder
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/next"
android:title="Next" />
</menu>
In your activity inflate the menu
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) //get the id which is an int
{
case R.id.next: // check if its the menu item next selected
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Next Selected", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,secondAct.class));//start activity
break;
default:
return super.onOptionsItemSelected(item);
}
}
What code would i add so that when the menu button refresh is pressed it will refresh the webview that is in my application.?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView WV = (WebView) findViewById(R.id.webView1);
WV.getSettings().setJavaScriptEnabled(true);
WV.loadUrl("http://home.btconnect.com/MrJFisher/");
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
WebView.reload();
}
return super.onOptionsItemSelected(item);
}
};
beginner at this. so i do apologise.
EDIT:
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
public class Main extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://home.btconnect.com/MrJFisher/");
}
#Override
public boolean onCreateOptionsMenu (Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == R.id.item1){
mWebView.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== R.id.item1){
WV.reload();
return true;
}
return super.onOptionsItemSelected(item);
}
See the WebView docs