Android viewpager活动问题
我一直在开发一个Android活动,它包含一个带有10个图像的ViewPager,下面有一个图像视图,单击后会显示ViewPager的图像 当我通过登录屏幕进入后尝试运行它时,交易崩溃 我不确定LogCat警告的错误在哪里,因此如果您能帮助我,我将非常感激 以下是LogCat消息:Android viewpager活动问题,android,imageview,android-viewpager,project,logcat,Android,Imageview,Android Viewpager,Project,Logcat,我一直在开发一个Android活动,它包含一个带有10个图像的ViewPager,下面有一个图像视图,单击后会显示ViewPager的图像 当我通过登录屏幕进入后尝试运行它时,交易崩溃 我不确定LogCat警告的错误在哪里,因此如果您能帮助我,我将非常感激 以下是LogCat消息: FATAL EXCEPTION: main java.lang.NullPointerException at com.example.logger.SecondActivity$MyPagerAdapter.set
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)
SecondActivity.java
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
}
};
}
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);
}
}
}
结束SecondActivity.java
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
}
};
}
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);
}
}
}
活动\u Second.xml
<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" >
<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"
/>
<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"
/>
</RelativeLayout>
<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" >
<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"
/>
<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"
/>
<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"
/>
</RelativeLayout>
结束MainActivity.java
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
}
};
}
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);
}
}
}
活动\u Main.xml
<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" >
<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"
/>
<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"
/>
</RelativeLayout>
<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" >
<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"
/>
<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"
/>
<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"
/>
</RelativeLayout>
结束活动\u Main.xml
<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" >
<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"
/>
<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"
/>
</RelativeLayout>
<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" >
<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"
/>
<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"
/>
<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"
/>
</RelativeLayout>
如果我能得到一些关于如何解决这些问题的见解,我将不胜感激
你诚挚的
毛罗。改变这个
mPagerAdapter = new MyPagerAdapter(null)
到
构造函数看起来像
public MyPagerAdapter (SecondActivity activity){
this.activity=activity; //activity is null
}
SecondActivity.java中最有可能的第80行是activity.showImage(位置,(视图)对象)代码>和活动
为空
更改您的Pageradapter代码
class MyPagerAdapter extends PagerAdapter{
MyPagerAdapter (Context context) {
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView((View) object);
}
@Override
public int getCount() {
return pics.length;
}
@Override
public Object instantiateItem(View collection, int position) {
ImageView iv = new ImageView(SecondActivity.this);
((ViewPager) collection).addView(iv, 0);
iv.setImageResource(pics[position]);
return iv;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
}
它正在工作
public class SecondActivity extends Activity {
private static final int NUM_PAGES = 10;
private ViewPager mpager;
private MyPagerAdapter mPagerAdapter;
private int[] pics = { R.drawable.flower1, R.drawable.flower10,
R.drawable.flower11, R.drawable.flower13, R.drawable.flower2,
R.drawable.flower6, R.drawable.flower8, R.drawable.flower7,
R.drawable.image3, R.drawable.flower3 };
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
mPagerAdapter = new MyPagerAdapter(this,pics);
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 {
Activity activity;
int imageArray[];
public MyPagerAdapter(SecondActivity act, int[] imgArra) {
imageArray = imgArra;
activity = act;
}
public int getCount() {
return imageArray.length;
}
public Object instantiateItem(View collection, int position) {
ImageView view = new ImageView(activity);
System.out.println(position);
view.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
view.setScaleType(ScaleType.FIT_XY);
view.setBackgroundResource(imageArray[position]);
((ViewPager) collection).addView(view, 0);
return view;
}
@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
@Override
public Parcelable saveState() {
return null;
}
}
@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
}
};
}由于未在MyPagerAdapter
类中获取活动的上下文,因此出现空指针错误
在SecondActivity
中更改适配器的初始化,如下所示:
而不是这个
mPagerAdapter = new MyPagerAdapter(null);
这样做
mPagerAdapter = new MyPagerAdapter(SecondActivity.this)
非常感谢:现在它还没有崩溃…屏幕上根本没有显示任何东西。我是否遗漏了一些必须编程到布局中的东西,也许?@user3082271我正在回答另一个问题,所以请wait@user3082271检查编辑。希望它有助于显示图像没有什么非常感谢你:viewpager现在工作得很好。有一个g早上好。@user3082271不,你接受了某个人的答案。你只能接受一个答案。接受对你帮助最大的答案。我把你的问题投了赞成票。非常感谢:现在它还没有崩溃……屏幕上根本没有显示任何内容。我是不是遗漏了一些必须编入布局的内容?总是很乐意帮助。投票赞成如果有帮助,请回答。因为我投票给了你的问题@user3082271