I was trying to implement view pager .Encountering a null pointer exception.
I am Quit new to this concept.
Cannot figure out the issue.
please help
logcat
03-10 12:26:39.323: E/AndroidRuntime(10593): FATAL EXCEPTION: main
03-10 12:26:39.323: E/AndroidRuntime(10593): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ib.qezyplay/com.ib.qezyplay.pagerMainActivity}: java.lang.NullPointerException
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.os.Looper.loop(Looper.java:137)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread.main(ActivityThread.java:4895)
03-10 12:26:39.323: E/AndroidRuntime(10593): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 12:26:39.323: E/AndroidRuntime(10593): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 12:26:39.323: E/AndroidRuntime(10593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-10 12:26:39.323: E/AndroidRuntime(10593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-10 12:26:39.323: E/AndroidRuntime(10593): at dalvik.system.NativeStart.main(Native Method)
03-10 12:26:39.323: E/AndroidRuntime(10593): Caused by: java.lang.NullPointerException
03-10 12:26:39.323: E/AndroidRuntime(10593): at com.ib.qezyplay.pagerMainActivity.onCreate(pagerMainActivity.java:42)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.Activity.performCreate(Activity.java:5163)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-10 12:26:39.323: E/AndroidRuntime(10593): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
03-10 12:26:39.323: E/AndroidRuntime(10593): ... 11 more
pagerMainActivity
package com.ib.qezyplay;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Window;
@SuppressWarnings("deprecation")
public class pagerMainActivity extends FragmentActivity implements TabListener{
private static final String TAG = null;
private MyAdapter myAdapter;
private ActionBar actionBar;
private ViewPager viewPager;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.pager_main_activity);
Intent InfoFromSignScreen = getIntent();
Log.e(TAG,"ENTERED **************** 1 ");
viewPager =(ViewPager)findViewById(R.id.pager);
myAdapter = new MyAdapter(getSupportFragmentManager());
viewPager.setAdapter(myAdapter);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
ActionBar.Tab tab1=actionBar.newTab();
tab1.setText("Countries");
tab1.setTabListener(this);
ActionBar.Tab tab2=actionBar.newTab();
tab2.setText("Channels");
tab2.setTabListener(this);
ActionBar.Tab tab3=actionBar.newTab();
tab3.setText("Genres");
tab3.setTabListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.e(TAG, " *****************TAB SELECTED POSTION *******************" +tab.getPosition());
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.e(TAG, " *****************TAB SELECTED POSTION *******************" +tab.getPosition());
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
Log.e(TAG, " *****************TAB SELECTED POSTION *******************" +tab.getPosition());
}
public class MyAdapter extends FragmentPagerAdapter
{
private String[] titles = { "Countries", "Channels", "Genres" };
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Fragment fragment =null;
if(arg0 == 0)
{
fragment = new Countries();
}
if(arg0 == 1)
{
fragment = new Genres();
}
if(arg0 == 2)
{
fragment = new UserMainScreen();
}
return null;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 3;
}
}
}
pagerMainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:minHeight="20dp"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ib.qezyplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="com.ib.coverflow.StudyCenterApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/myAppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/myAppTheme"
android:windowSoftInputMode="stateHidden|adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".pagerMainActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:screenOrientation="landscape">
</activity>
<service
android:name="com.octoshape.android.service.StreamService"
android:process=":OctoProc" />
</application>
</manifest>
countries.java
package com.ib.qezyplay;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Countries extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.countries_layout, container, false);
return rootView;
}
}
there are other two fragments same as above