5 Replies - 3257 Views - Last Post: 28 September 2012 - 05:26 PM

#1 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

ActionBar tabs:NullPointerException raised again

Posted 26 September 2012 - 09:00 PM

Hello.

It's me again with a nullpointerexception error "again" but this time it's with the actionbar tabs.i am laying my hands on several things as i learn to program on androids and i decided to create this actionbar with tabs for navigation.When i ran it i got a nullpointerException at a line which i commented.can someone please point out my mistake and show me where i got this whole thing wrong.i'm sorry if i have been such a bother,thing is,i'm new to this.


public class WetinDeyTabNavs extends SherlockFragmentActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wetin_dey_tabs);

		getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		getSupportActionBar().setDisplayShowTitleEnabled(false);

		ActionBar.Tab tab = getSupportActionBar()
				.newTab()
				.setText("Register")
				.setIcon(R.drawable.register_icon)
				.setTabListener(
						new MyTabListener<RegFrag>(this, "Register",
								RegFrag.class));

		getSupportActionBar().addTab(tab);

		tab = getSupportActionBar()
				.newTab()
				.setText("Login")
				.setIcon(R.drawable.login)
				.setTabListener(
						new MyTabListener<LogFrag>(this, "Login", LogFrag.class));
		getSupportActionBar().addTab(tab);
	}

	public class MyTabListener<T extends Fragment> implements
			ActionBar.TabListener {
		private Fragment mFragment;
		private final Activity mActivity;
		private final String mTag;
		private final Class<T> mClass;

		public MyTabListener(Activity activity, String tag, Class<T> clz) {
			mActivity = activity;
			mTag = tag;
			mClass = clz;
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			if (mFragment == null) {
				mFragment = Fragment.instantiate(mActivity, mClass.getName());
				ft.add(android.R.id.content, mFragment, mTag); // NullPointerException thrown here
			} else {
				ft.attach(mFragment);
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			if (mFragment != null) {
				ft.detach(mFragment);
			

			}
		}

	}
}




i laid my hands on a tutorial and on trying this i got the Exception.i need help.

Is This A Good Question/Topic? 0
  • +

Replies To: ActionBar tabs:NullPointerException raised again

#2 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: ActionBar tabs:NullPointerException raised again

Posted 26 September 2012 - 09:06 PM

It looks like your first parameter for ft.add() is wrong! I dont think you want to be using android.R.id.content. The reason it is wrong and you keep getting a nullPointer Error is because android.R.id.content does not exist in your layout! Make sure you are passing the correct id for your layout not the android.R.id.*.
Was This Post Helpful? 0
  • +
  • -

#3 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

Re: ActionBar tabs:NullPointerException raised again

Posted 27 September 2012 - 04:57 AM

alright.let me try and create a blank layout within the layout i used for this and give it an id and test it to see if it works.

Will get back to you.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

Re: ActionBar tabs:NullPointerException raised again

Posted 27 September 2012 - 05:31 AM

Am still getting the same thing.i added another linearlayout to the one i used for this activity as a layout and also changed the ft.add(android.R.content,.....,.....) to ft.add(R.id.fragment_container).i wrote it like this:

wetin_dey_tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>

</LinearLayout>




should i just remove the newly added layout i.e that inner linearlayout and simply give the outer an id or what?

appreciate the help.
Was This Post Helpful? 0
  • +
  • -

#5 H3R3T1C  Icon User is offline

  • Android Expert
  • member icon

Reputation: 277
  • View blog
  • Posts: 757
  • Joined: 30-March 07

Re: ActionBar tabs:NullPointerException raised again

Posted 27 September 2012 - 01:46 PM

I would suggest you use Framelayout to place your fragments in! Here is the layout I use when working with fragments:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fl1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</FrameLayout>



Then using my layout as an example your ft.add() should look like this:
ft.add(R.id.fl1, mFragment, mTag);


The thing to note here is that you are importing your R and not android.R.

Edit: Also make sure your fragments layouts have their root view fill or match parent!

This post has been edited by H3R3T1C: 27 September 2012 - 01:48 PM
Reason for edit:: add info.

Was This Post Helpful? 0
  • +
  • -

#6 emyjava  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 09-August 12

Re: ActionBar tabs:NullPointerException raised again

Posted 28 September 2012 - 05:26 PM

i decided to use the ViewPager instead and it worked fine.

This was what i wrote that i got from another tutorial:

WetinDeyTabNavs.java


package com.src.v4.applicatons.wetin.dey;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;

public class WetinDeyTabNavs extends SherlockFragmentActivity implements
		ActionBar.TabListener, ViewPager.OnPageChangeListener {

	private LogFrag logFrag;
	private RegFrag regFrag;

	private ViewPager mViewPager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wetin_dey_tabs);
		mViewPager = (ViewPager) findViewById(R.id.pager);

		mViewPager
				.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));
		mViewPager.setOnPageChangeListener(this);
		mViewPager.setPageMarginDrawable(R.drawable.boder_img);
		mViewPager.setPageMargin(5);

		final ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.addTab(actionBar.newTab().setText("Register")
				.setIcon(R.drawable.register_icon).setTabListener(this));
		actionBar.addTab(actionBar.newTab().setText("Login")
				.setIcon(R.drawable.login).setTabListener(this));
		
		actionBar.setDisplayShowTitleEnabled(true);

	}

	@Override
	public void onPageSelected(int position) {
		getSupportActionBar().setSelectedNavigationItem(position);
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
	}

	private class MainPagerAdapter extends FragmentPagerAdapter {
		public MainPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return (regFrag = new RegFrag());

			case 1:
				return (logFrag = new LogFrag());

			}
			return null;
		}

		@Override
		public int getCount() {
			return 2;
		}
	}
}



and the wetin_dey_tabs.xml

<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="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

</LinearLayout>




Thanks for all your support.if i was to still use my former code n your suggestion,i should put the fragment xml file within it?

please do answer this for future refs.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1