-7

I am trying to call an activity when ever an element from my GridView is clicked it will open in a swiping activity and it will show the respective image but I am getting an error on setting as

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.union.project, PID: 2194
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.union.project/com.union.project.Swipeview.Main4Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
 at android.app.ActivityThread.access$800(ActivityThread.java:151)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:135)
 at android.app.ActivityThread.main(ActivityThread.java:5254)
 at java.lang.reflect.Method.invoke(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:372)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
 at com.union.project.Swipeview.Main4Activity.onCreate(Main4Activity.java:26)
 at android.app.Activity.performCreate(Activity.java:5990)
 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
 at android.app.ActivityThread.access$800(ActivityThread.java:151) 
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
 at android.os.Handler.dispatchMessage(Handler.java:102) 
 at android.os.Looper.loop(Looper.java:135) 
 at android.app.ActivityThread.main(ActivityThread.java:5254) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at java.lang.reflect.Method.invoke(Method.java:372) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
 04-27 01:17:54.025 2194-2194/com.union.project I/Process: S

this is the error I am getting when I click on anyitem it show the toast but then crashes my fragment code

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_two, container, false);
    GridView gridView=(GridView)view.findViewById(R.id.gridView2);
    gridView.setAdapter(new UAdapter(getActivity()));
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(getActivity(),"Pic"+(position)+"Selected",Toast.LENGTH_SHORT).show();
            Intent intent= new Intent(view.getContext(),Main4Activity.class);
            intent.putExtra("pic",position);
            startActivity(intent);
        }
    });
    return view;
}

my swipe view code `

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main4);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    viewPager =(ViewPager)findViewById(R.id.viewpager1);
    adapter =new CustomeSwipeAdapter2(this);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0));
}`

this is the activity that I want to start but getting the error can some one tell me how to fix this .

USER9561
  • 1,084
  • 3
  • 15
  • 41
Neelay Srivastava
  • 100
  • 1
  • 2
  • 17
  • 1
    Read your stack trace - `android.util.AndroidRuntimeException: requestFeature() must be called before adding content`. – Mike M. Apr 27 '16 at 04:58
  • 1
    Possible duplicate of [requestFeature() must be called before adding content](http://stackoverflow.com/questions/4250149/requestfeature-must-be-called-before-adding-content) – Mike M. Apr 27 '16 at 04:59
  • as you said I have changed but I am still gettin an error see the edited error – Neelay Srivastava Apr 27 '16 at 05:22
  • thx everyone fixed the problum it was my mistake in swipe view i set the id of different button and image view thx for the help – Neelay Srivastava Apr 27 '16 at 05:43

4 Answers4

4

Your error itself gives answer of your question.

requestFeature() must be called before adding content

That means your second Activity needs this change

requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main4);

requestWindowFeature() must be before setContentView()

After your updated question

now your error is

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference

you need to check this What is a Null Pointer Exception, and how do I fix it?

Community
  • 1
  • 1
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

call requestWindowFeature like bellow

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);
}
shobhan
  • 1,460
  • 2
  • 14
  • 28
0

As mentioned above, you need to call requestWindowFeature(Window.FEATURE_NO_TITLE); before setContentView method.

am110787
  • 316
  • 1
  • 2
  • 9
  • I removed the window feature then also I am getting error? – Neelay Srivastava Apr 27 '16 at 05:09
  • `super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.mainmenu); ` – am110787 Apr 27 '16 at 05:14
  • Alternatively, you can try this as well in your Activity tag in manifest file `android:theme="@android:style/Theme.NoTitleBar.Fullscreen" ` – am110787 Apr 27 '16 at 05:17
0

The problem is in these lines

viewPager =(ViewPager)findViewById(R.id.viewpager1);
adapter =new CustomeSwipeAdapter2(this);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0));

First : check whether you have a ViewPager layout with id of viewpager1 in your activity_main4 xml

Second : how do you pass the images to your adapter ?! are there link or you pass drawable itself ?! you dnt pass anything to your adapter !!!!!!!!!!!!!

Third : when your adapter is empty , you cant use viewPager.setCurrentItem(getIntent().getIntExtra("pic", 0)); on it

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50