Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid viewpager activity problems
    primarykey
    data
    text
    <p>I've been working on an Android activity which contains a ViewPager with 10 images on it and an Image View below which will display the image of the ViewPager upon click.</p> <p>When I try to run it after entering through my login screen, the deal crashes.</p> <p>I'm not sure where are the errors LogCat warns about so if you could help me out I'd be very grateful.</p> <p>Here are the LogCat messages:</p> <pre><code>FATAL EXCEPTION: main java.lang.NullPointerException at com.example.logger.SecondActivity$MyPagerAdapter.setPrimaryItem (SecondActivity.java:80) at android.support.v4.view.ViewPager.populate (ViewPager.java:1066) at android.support.v4.view.ViewPager.populate (ViewPager.java:914) at android.support.v4.view.ViewPager.onMeasure (ViewPager.java:1436) at android.view.View.measure (View.java:15848) at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728) at android.widget.RelativeLayout.onMeasure (RelativeLayout.java:477) at android.view.View.measure (View.java:15848) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5012) at com.android.internal.widget.ActionBarOverLayout.onMeasure(ActionBarOverLayout.java:302) at android.view.View.measure (View.java:15848) at android.view.ViewGroup.measureChildWithMargins (ViewGroup.java:5012) at android.widget.FrameLayout.onMeasure (FrameLayout.java:310) at android.view.View.measure (View.java:15848) </code></pre> <p><strong>SecondActivity.java</strong></p> <pre><code>package com.example.logger; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.support.v4.app.FragmentStatePagerAdapter; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.view.ViewGroup; public class SecondActivity extends Activity { private static final int NUM_PAGES = 10; private ViewPager mpager; private MyPagerAdapter mPagerAdapter; private int [] pics = {R.drawable.android_interfaz_layout_estructura_final, R.drawable.baixa, R.drawable.baixada, R.drawable.greenprogressbar, R.drawable.layout_keyboard, R.drawable.linerlay, R.drawable.merge1, R.drawable.o2zds, R.drawable.regbd, R.drawable.s7qrs}; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); mPagerAdapter = new MyPagerAdapter(null); mpager = (ViewPager) findViewById(R.id.viewer); mpager.setAdapter (mPagerAdapter); } public void showImage (int position, View view) { //View is your view that you returned from instantiateItem //and position is it's position in array you can get the image resource id //using this position and set it to the ImageView } private class MyPagerAdapter extends PagerAdapter { SecondActivity activity; public MyPagerAdapter (SecondActivity activity){ this.activity=activity; } @Override public int getCount() { // TODO Auto-generated method stub return pics.length; } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return false; } @Override public Object instantiateItem(View collection, int position) { ImageView view = new ImageView (SecondActivity.this); view.setImageResource (pics[position]); ((ViewPager) collection).addView(view, 0); return view; } @Override public void setPrimaryItem (ViewGroup container, int position, Object object) { super.setPrimaryItem (container, position, object); activity.showImage(position,(View)object); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.second, menu); return true; } private OnClickListener mPageClickListener = new OnClickListener() { public void onClick (View v) { // TODO Auto-generated method stub //aquí anirà la funció per traslladar la image de la gallery a la pantalla Integer picId = (Integer) v.getTag(); mpager.setVisibility(View.VISIBLE); mpager.setCurrentItem(v.getId()); } @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }; } </code></pre> <p><strong>End SecondActivity.java</strong></p> <p><strong>Activity_Second.xml</strong></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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".SecondActivity" &gt; &lt;android.support.v4.view.ViewPager android:padding="16dp" android:id="@+id/viewer" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_weight="0.30" /&gt; &lt;ImageView android:id="@+id/big_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_below="@+id/viewer" android:layout_weight="0.70" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>End Activity_Second.xml</strong></p> <p>Just in case I'll add the MainActivity as well.</p> <p><strong>Main_Activity.java</strong></p> <pre><code>package com.example.logger; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); attachHandlers(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private void attachHandlers() { findViewById(R.id.login).setOnClickListener(this); } @Override public void onClick(View arg0) { if(arg0.getId() == R.id.login) { Intent i = new Intent(this, SecondActivity.class); startActivity(i); } } } </code></pre> <p><strong>End MainActivity.java</strong></p> <p><strong>Activity_Main.xml</strong></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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" &gt; &lt;Button android:id="@+id/login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/Login" android:layout_alignParentBottom="true" android:background="@drawable/shapes" android:textColor="@color/text" /&gt; &lt;EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/input_u" android:layout_marginTop="10dp" android:textColor="@color/text" /&gt; &lt;EditText android:id="@+id/password" android:layout_below="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/input_p" android:layout_marginTop="10dp" android:textColor="@color/text" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p><strong>End Activity_Main.xml</strong></p> <p>I'd be very grateful if I could receive some insight into how to fix these problems.</p> <p>Yours sincerely,</p> <p>Mauro.</p>
    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.
    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