Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid - NullPointerException on ViewPager
    primarykey
    data
    text
    <p>I am trying to create dialog with fragment but when I call the setAdapter method I have a Java NullPointerException, and I can't find the error. </p> <p>Java Code:</p> <pre><code>public class MainActivity extends FragmentActivity { Context myContext; private MyAdapter mAdapter; private ViewPager mPager; String TAG = "Dialog"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myContext = this; findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { final Dialog dialog = new Dialog(myContext); dialog.setContentView(R.layout.layout_dialog); mAdapter = new MyAdapter(getSupportFragmentManager()); mPager = (ViewPager) dialog.findViewById(R.id.myViewPager); **mPager.setAdapter(mAdapter);** //Line 39, the error is on this line. dialog.show(); } }); } public class MyAdapter extends FragmentPagerAdapter { int counts; public MyAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { return 3; } @Override public Fragment getItem(int position) { switch (position) { case 0: return new ImageFragment(R.drawable.icecream); case 1: return new ImageFragment(R.drawable.cupcake); case 2: return new ImageFragment(R.drawable.donut); default: return null; } } } } </code></pre> <p>Fragment class:</p> <pre><code>public class ImageFragment extends Fragment { private final int imageResourceId; public ImageFragment(int imageResourceId) { this.imageResourceId = imageResourceId; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.activity_dialog_item, container, false); ImageView imageView = (ImageView) view.findViewById(R.id.imageView1); imageView.setImageResource(imageResourceId); return view; } </code></pre> <p>}</p> <p>activity_main xml code:</p> <pre><code>&lt;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"&gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="20dip" android:layout_marginTop="30dip" android:text="Show Dialog" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>layout_dialog.xml code:</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;android.support.v4.view.ViewPager android:id="@+id/myViewPager" android:layout_width="match_parent" android:layout_height="match_parent" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>activity_dialog_item.xml code:</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/cupcake" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Error:</p> <pre><code>03-06 13:58:22.546: E/AndroidRuntime(818): FATAL EXCEPTION: main 03-06 13:58:22.546: E/AndroidRuntime(818): java.lang.NullPointerException 03-06 13:58:22.546: E/AndroidRuntime(818): at com.example.viewpager.MainActivity$1.onClick(MainActivity.java:39) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.view.View.performClick(View.java:2485) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.view.View$PerformClick.run(View.java:9080) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Handler.handleCallback(Handler.java:587) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Handler.dispatchMessage(Handler.java:92) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.os.Looper.loop(Looper.java:123) 03-06 13:58:22.546: E/AndroidRuntime(818): at android.app.ActivityThread.main(ActivityThread.java:3683) 03-06 13:58:22.546: E/AndroidRuntime(818): at java.lang.reflect.Method.invokeNative(Native Method) 03-06 13:58:22.546: E/AndroidRuntime(818): at java.lang.reflect.Method.invoke(Method.java:507) 03-06 13:58:22.546: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-06 13:58:22.546: E/AndroidRuntime(818): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-06 13:58:22.546: E/AndroidRuntime(818): at dalvik.system.NativeStart.main(Native Method) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload