Android: Adding menu resource in apk crashes it - android

I am trying to learn android, I create, deploy app using ant based CLI.
My manifest file is as follows
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.frag"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher" android:theme="#android:style/Theme.Holo">
<activity android:name="MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I added a menu xml file in by creating res\menu\items.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_share"
android:title="Share it"
>
</item>
</menu>
If I now compile using ant debug, then run the app, the app crashes. Any ideas?
Following is the activity code, it does not uses Menu as of now.
package com.example.frag;
import android.app.Activity;
import android.app.ActionBar;
import android.os.Bundle;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
//import android.support.v4.view.Menu;
//import android.support.v4.view.MenuInflater;
public class MainActivity extends FragmentActivity implements ActionBar.TabListener
{
private ActionBar actionBar;
private ViewPager viewPager;
private TabPagerAdapter tabPagerAdapter;
private String[] tabs = { "Mouse", "Keyboard"};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) findViewById (R.id.pager);
tabPagerAdapter = new TabPagerAdapter (getSupportFragmentManager());
viewPager.setAdapter (tabPagerAdapter);
actionBar = getActionBar();
//actionBar.setHasOptionsMenu (true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
/**
* on swipe select the respective tab
* */
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) { }
#Override
public void onPageScrollStateChanged(int arg0) { }
});
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) { }
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
/*#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate (R.menu.items, menu);
return super.onCreateOptionsMenu (menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
return true;
}*/
}
UPDATE
I removed menu resources, then tried adding styles.xml in values/, recompiled, launched and it crashed again.

Related

Android Horizontal Scroll View for showing youtube vidoes

am creating android application which will displays youtube videos on horizontal(Landscape) list view similar to application in screen-shot. And when i click on one of the video then it should play, which is what currently happening, am able to load all the youtube videos along with there thumbnails in simple list view(Vertical) in android, but i couldn't found out a way so far, using which I can show all videos in the youtube playlist in the manner as shown in the following screen shot layout view.
If any one knows how I could show my video list in horizontal(Landscape) layout as depicted in the attached screen-shot, it would be great help for me.
Please visit link to see the screenshot: https://sc-cdn.scaleengine.net/i/954e89ab4d5fef83bb19009107d71c60.png
Thank You.
Here Sample code
Sorry for the Logs, as am running it on device.
MainActivity
package in.wptrafficanalyzer.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Getting a reference to the ViewPager defined the layout file */
ViewPager pager = (ViewPager) findViewById(R.id.pager);
/** Getting fragment manager */
FragmentManager fm = getSupportFragmentManager();
/** Instantiating FragmentPagerAdapter */
MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
/** Setting the pagerAdapter to the pager object */
pager.setAdapter(pagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
MyFragment.java
package in.wptrafficanalyzer.viewpagerdemo;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.ErrorReason;
import com.google.android.youtube.player.YouTubePlayer.PlaybackEventListener;
import com.google.android.youtube.player.YouTubePlayer.PlayerStateChangeListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerView;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MyFragment extends Fragment implements YouTubePlayer.OnInitializedListener{
int mCurrentPage;
public static final String API_KEY = "AIzaSyBaoObw6vr4uCJnCXXXXXXXX";
//http://youtu.be/<VIDEO_ID>
public static final String VIDEO_ID = "dKLftgvYsVU";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Getting the arguments to the Bundle object */
Bundle data = getArguments();
/** Getting integer data of the key current_page from the bundle */
mCurrentPage = data.getInt("current_page", 0);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.myfragment_layout, container,false);
YouTubePlayerView tv = (YouTubePlayerView ) v.findViewById(R.id.youtube_player);
tv.initialize(API_KEY, this);
return v;
}
#Override
public void onInitializationFailure(Provider provider, YouTubeInitializationResult result) {
//Toast.makeText(this, "Failured to Initialize!", Toast.LENGTH_LONG).show();
}
#Override
public void onInitializationSuccess(Provider provider, YouTubePlayer player, boolean wasRestored) {
/** add listeners to YouTubePlayer instance **/
player.setPlayerStateChangeListener(playerStateChangeListener);
player.setPlaybackEventListener(playbackEventListener);
/** Start buffering **/
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
private PlaybackEventListener playbackEventListener = new PlaybackEventListener() {
#Override
public void onBuffering(boolean arg0) {
}
#Override
public void onPaused() {
}
#Override
public void onPlaying() {
}
#Override
public void onSeekTo(int arg0) {
}
#Override
public void onStopped() {
}
};
private PlayerStateChangeListener playerStateChangeListener = new PlayerStateChangeListener() {
#Override
public void onAdStarted() {
}
#Override
public void onError(ErrorReason arg0) {
}
#Override
public void onLoaded(String arg0) {
}
#Override
public void onLoading() {
}
#Override
public void onVideoEnded() {
}
#Override
public void onVideoStarted() {
}
};
}
MyFragmentPagerAdapter.java
package in.wptrafficanalyzer.viewpagerdemo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
final int PAGE_COUNT = 5;
/** Constructor of the class */
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
/** This method will be invoked when a page is requested to create */
#Override
public Fragment getItem(int arg0) {
MyFragment myFragment = new MyFragment();
Bundle data = new Bundle();
data.putInt("current_page", arg0+1);
myFragment.setArguments(data);
return myFragment;
}
/** Returns the number of pages */
#Override
public int getCount() {
return PAGE_COUNT;
}
#Override
public CharSequence getPageTitle(int position) {
return "Page #" + ( position + 1 );
}
}
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.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:textColor="#fff"
android:paddingTop="4dp"
android:paddingBottom="4dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
myfragment_layout.xml
<?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="vertical" >
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="5dp"
/>
</LinearLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="in.wptrafficanalyzer.viewpagerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="17" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter
android:label="#string/app_name"
>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
You are doing it wrong. Your my Fragment Xml should look like this:
<?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="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtube_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:padding="5dp"
/>
</android.support.v4.view.ViewPager>
</LinearLayout>
Now code accordingly and Post your logcat for crash issue resolution.

What is ActionBar.setHomeButtonEnabled(boolean)

I want to create a viewpager which contains 2 tabs, I have 3 layout:
This is activity_chart.xml that contains paper:
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
This is AccountsActivityChart.java:
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.rastari.salar.mymetarialbank.R;
import com.rastari.salar.mymetarialbank.adapter.TabsPagerAdapter;
/**
* Created by Salar on 9/5/2015.
*/
public class AccountsActivityChart extends FragmentActivity implements ActionBar.TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Accounts Activity", "Chart Activity" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chart);
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
#Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// on tab selected
// show respected fragment view
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
}
}
this is TabsPagerAdapter.java class:
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
/**
* Created by Salar on 26/4/2015.
*/
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Accounts Activity fragment activity
return new AccountsActivityFragment();
case 1:
// Chart Activity activity
return new ChartActivityFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 2;
}
}
this is AccountsActivityFragment.java:
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rastari.salar.mymetarialbank.R;
/**
* Created by Salar on 25/4/2015.
*/
public class AccountsActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_accounts_activity, container, false);
return rootView;
}
}
this is ChartActivityFragment.java:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.rastari.salar.mymetarialbank.R;
/**
* Created by Salar on 9/5/2015.
*/
public class ChartActivityFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_chart_activity, container, false);
return rootView;
}
}
this is manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rastari.salar.mymetarialbank" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/MyMetarialBank">
<activity
android:name=".activity.Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activity.MainActivity"/>
<activity android:name=".activity.AccountsActivityChart"/>
</application>
</manifest>
When I run the app I get this Error:
Attempt to invoke virtual method 'void
android.app.ActionBar.setHomeButtonEnabled(boolean)' on a null object
reference
How can I fix this?
I think you have applied the theme which is having no action bar. This is why when you are getting the actionbar instance, it will be null and you are calling the setHomeButtonEnabled on on the null actionbar instance. That is why you are getting the error.
try actionBar = getSupportActionBar();
instead of actionBar = getActionBar();

Nothing happens when pressing Enter while searching using Android Search widget

I am trying to implement the search widget in android but unsuccessful. It lets me type the query text in the seacrh textbox but nothing happens when I press Enter. I followed the tutorial very closely http://developer.android.com/training/search/setup.html. But I can't fix the problem. I've been trying everything for 6 hours now. Please help!
Here is my Android manifest file:
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock">
<meta-data android:name="android.app.default_searchable"
android:value="com.example.pt_labguide.SearchableActivity" />
<activity
android:name="com.example.pt_labguide.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".BasicMetabolicPanelActivity"
android:label="#string/basic_metab_panel" >
<intent-filter>
<action android:name="com.example.pt_labguide.BasicMetabolicPanelActivity"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ArterialBloodGasActivity"
android:label="#string/arterial_blood_gases" >
<intent-filter>
<action android:name="com.example.pt_labguide.ArterialBloodGasActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".DisplayLabValueActivity">
<intent-filter>
<action android:name="com.example.pt_labguide.DisplayLabValueActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SearchableActivity">
<intent-filter>
<action android:name="com.example.pt_labguide.SearchableActivity" />
<category android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
</application>
</manifest>
****Here is the Searchable activity:****
package com.example.pt_labguide;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.widget.SearchView;
public class SearchableActivity extends SherlockFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
handleIntent(getIntent());
}
#Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
Log.d("SearchResultsActivity", "onNewIntent called");
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
doMySearch(query);
Log.d("SearchResultsActivity", query);
}
}
private void doMySearch(String query){
Intent intent = new Intent("com.example.pt_labguide.DisplayLabValueActivity");
intent.putExtra("labValue", query);
startActivity(intent);
}
}
**Here is the Main activity:**
package com.example.pt_labguide;
import android.app.Fragment;
import android.app.SearchManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.widget.Button;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.widget.SearchView;
import android.view.View;
import android.content.Intent;
public class MainActivity extends SherlockFragmentActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notice that setContentView() is not used, because we use the root
// android.R.id.content as the container for each fragment
// setup action bar for tabs
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
Tab tab = actionBar.newTab()
.setText(R.string.list)
.setTabListener(new TabListener<ListFragment>(
this, "list", ListFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.categories)
.setTabListener(new TabListener<CategoryFragment>(
this, "category", CategoryFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.favorites)
.setTabListener(new TabListener<FavoriteFragment>(
this, "favorite", FavoriteFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.about)
.setTabListener(new TabListener<AboutFragment>(
this, "about", AboutFragment.class));
actionBar.addTab(tab);
}
static class TabListener<T extends SherlockFragment> implements ActionBar.TabListener {
private android.support.v4.app.Fragment mFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* #param activity The host Activity, used to instantiate the fragment
* #param tag The identifier tag for the fragment
* #param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}//end inner class
#Override
public boolean onSearchRequested() {
return super.onSearchRequested();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.search, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.menu_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(false);
return true;
}
public void display_BMP (View view) {
startActivity(new Intent("com.example.pt_labguide.BasicMetabolicPanelActivity"));
}
public void display_ABG (View view) {
startActivity(new Intent("com.example.pt_labguide.ArterialBloodGasActivity"));
}
public void display_LabValue (View view) {
String name = ((Button) view).getText().toString();
Intent intent = new Intent("com.example.pt_labguide.DisplayLabValueActivity");
intent.putExtra("labValue", name);
startActivity(intent);
}
}//end class TabDemo
**Here is the menu/search.xml:**
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.example.pt_labguide="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_search"
android:title="#string/search_hint"
android:icon="#drawable/ic_search_lens"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>
**Here is the raw/xml/searchable.xml:**
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>

Action items systematically stacked in the action overflow, in actionbar

i'm struggling with the ActionBar in android.
heres my problem:
my Action items are not shown in the ActionBar, instead they are stacked in the action overflow, no matter what i do ..
i have spent a hole day looking for a solution to this but i can't seem to find whats missing .. :-s
i tried setting android:showAsAction to "Always" : it didnt work.
when i change ifRoom to Always, the logcat says :
testmenu.xml:5: error: Error: String types not allowed (at 'showAsAction' with value 'Always'). + it doesnt campile at all.
i've tried a lots of differents utorials and posts here , but unfortunately i cant find a solution..
heres my code:
Mainactivity.java
package com.locklock;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
//import android.support.v7.app.ActionBar.Tab;
//import android.support.v7.app.ActionBarActivity;
//import android.support.v7.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements TabListener {
#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();
}
// Set up the action bar to show tabs.
final ActionBar actionBar= getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// actionBar.setDisplayShowTitleEnabled(false);
// adding tabs
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("TEST");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("EVENTS");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("USERS");
tab3.setTabListener(this);
ActionBar.Tab tab4=actionBar.newTab();
tab4.setText("SETTINGS");
tab4.setTabListener(this);
actionBar.addTab(tab1);
actionBar.addTab(tab2);
actionBar.addTab(tab3);
actionBar.addTab(tab4);
// tab = getActionBar.newTab();
// tab.setText(tabText);
// tab.setIcon(R.drawable.tab_icon);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.action_users, menu);
//return true;
//MenuInflater Mymenu = getMenuInflater();
//Mymenu.inflate(R.menu.action_users, menu);
return super.onCreateOptionsMenu(menu);
}
#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();
switch (id) {
case R.id.action_settings:
// todo
return true;
case R.id.action_add_user:
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
return true;
case R.id.action_set_permission:
// todo
return true;
case R.id.action_delete_user:
// todo
return true;
default:
return super.onOptionsItemSelected(item);
}
//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;
}
}
#Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
Log.e("mytag","test");
}
#Override
public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
Log.d("mytag","xxxx");
}
#Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
//Log.d("mytag","xxxx");
}
}
action_users.xml
<!-- Add User -->
<item android:id="#+id/action_add_user"
android:icon="#drawable/ic_action_remove"
android:title="#string/add_user"
android:showAsAction="ifRoom" />
<!-- Set permission for a User-->
<item android:id="#+id/action_set_permission"
android:icon="#drawable/ic_action_remove"
android:title="#string/set_permission"
android:showAsAction="ifRoom" />
<!-- delete User-->
<item android:id="#+id/action_delete_user"
android:icon="#drawable/ic_action_remove"
android:title="#string/delete_user"
android:showAsAction="ifRoom" />
androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.locklock"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:uiOptions="splitActionBarWhenNarrow" >
<activity
android:name="com.locklock.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
can anyone outhere take a quick look into this please?
in advance, thanks a lot .
after a few days of testing , i finally found the answer .
in the xml menu file, i had to change this :
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myappname="http://schemas.android.com/apk/res-auto" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
myappname:showAsAction="always" />
</menu>
to this:
<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="com.myappname.MainActivity" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="always" />
</menu>
and it worked .
Try to set yourapp in action_users.xml
<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_search"
android:title="#string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>

Android.widget.framelayout cannot be cast to android.support.v4.view.viewpager

My app keeps crashing. I'm trying to create a tab swype navigation setup. here is my code.
adapter class
package tabsswipe.adapter;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
//import fragments
import edu.wmich.lab4_jjohns1119.MainFragment;
//import edu.wmich.tabswithswipe.PostFragment;
//import edu.wmich.tabswithswipe.SuggestFragment;
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
switch (index) {
case 0:
// Main fragment activity
return new MainFragment();
case 1:
// Suggest fragment activity
//return new SuggestFragment();
case 2:
// Post fragment activity
//return new PostFragment();
}
return null;
}
#Override
public int getCount() {
// get item count - equal to number of tabs
return 3;
}
}
Main activity
package edu.wmich.lab4_jjohns1119;
import tabsswipe.adapter.TabsPagerAdapter;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
public class MainActivity extends FragmentActivity implements TabListener {
private ViewPager viewPager;
private TabsPagerAdapter mAdapter;
private ActionBar actionBar;
//tab titles
private String[] tabs = {"Main", "Photo", "Share"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization
viewPager = (ViewPager)findViewById(R.id.container);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
//adding the tabs to the action bar
for(String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name)
.setTabListener(this));
}
//on viewpager swiping, make the respective tab selected
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
//on changing the page, make tab selected
actionBar.setSelectedNavigationItem(position);
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
Main fragment
package edu.wmich.lab4_jjohns1119;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
return rootView;
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.wmich.lab4_jjohns1119"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="edu.wmich.lab4_jjohns1119.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name = "edu.wmich.lab4_jjohns1119.MainFragment"
android:label = "#string/main_fragment_name">
</activity>
</application>
the goal is to add more tabs so i can swype to navigate between fragments. it looks like i just have a casting error but nothing looks wrong to me. Can anyone help?
EDIT: Here is my main XML
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.wmich.lab4_jjohns1119.MainActivity"
tools:ignore="MergeRootFrame" />
Fragment 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" >
Can you post your xml layout file? And also the stacktrace?
If I had to guess I would say double check that your container R.id.container is declared as a ViewPager in your layout file.
EDIT: after your comment
In your java code you use ' viewPager = (ViewPager)findViewById(R.id.container);'
Which means you try to cast the element called 'container' as a viewPager.
But in your xml file, the element named container is a 'FrameLayout'.
If you want to use a ViewPage, you need to change your layout, instead of:
<FrameLayout xmlns:android="schemas.android.com/apk/res/android"; xmlns:tools="schemas.android.com/tools"; android:id="#+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="edu.wmich.lab4_jjohns1119.MainActivity" tools:ignore="MergeRootFrame" />
you need:
<FrameLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:tools="schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.wmich.lab4_jjohns1119.MainActivity"
tools:ignore="MergeRootFrame" >
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Or if you do want to use a FrameLayout, you need to change your cast. Instead of:
viewPager = (ViewPager)findViewById(R.id.container);
use
FrameLayout layout = (FrameLayout)findViewById(R.id.container);
As you have been told it's probable that you made a mistake not declaring R.id.container as a ViewPager, but if everything is correct try doing Project->clean sometimes the IDE just gets confused and messes up some ids, specially if you have been copy pasting xml code
I would say the problem for your null pointer is in you switch. Make sure you have a fragment called 'MainFragment', add break and add a default: otherwise your switch will return null.
public Fragment getItem(int index) {
switch (index) {
case 0:
// Main fragment activity
return new MainFragment();
break;
case 1:
return new MainFragment();
break;
case 2:
return new MainFragment();
break;
default:
return new MainFragment();
break;
}
return null;

Categories

Resources