您好我在这里用text.for显示图像。这是我的代码。如果我运行此应用程序,它显示日志cat错误。
例如:a.jpeg,名字就像我想要的那样。
但不幸的是,这是错误的。
任何人都可以帮助我
照片类
public class Photos extends Activity {
Button button1;
String mImages;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photos);
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d
};
String[] description_images = getIntent().getExtras().getStringArray("[{image1,image2,image3,image4}]"); //ARRAY OF ALL TEXT ASSOCIATED TO IMAGES
// String[] mImages1 = getIntent().getExtras().getStringArray("mImages");
// String[] mText={"Text1","Text2","Text3"};
@Override
public int getCount() {
return mImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
@Override
public View instantiateItem(ViewGroup container, int position) {
LayoutInflater inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.image_view, null);
final ImageView imageView = (ImageView)view.findViewById(R.id.iv_image);
final TextView textView = (TextView )view.findViewById(R.id.iv_text);
textView.setText(description_images[position]);
imageView.setImageResource(mImages[position]);
container.addView(view, 0);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
logcat错误
07-17 16:37:11.391: E/AndroidRuntime(562): FATAL EXCEPTION: main
07-17 16:37:11.391: E/AndroidRuntime(562): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imagegallery/com.example.imagegallery.Photos}: java.lang.NullPointerException
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.os.Looper.loop(Looper.java:123)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-17 16:37:11.391: E/AndroidRuntime(562): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 16:37:11.391: E/AndroidRuntime(562): at java.lang.reflect.Method.invoke(Method.java:521)
07-17 16:37:11.391: E/AndroidRuntime(562): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-17 16:37:11.391: E/AndroidRuntime(562): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-17 16:37:11.391: E/AndroidRuntime(562): at dalvik.system.NativeStart.main(Native Method)
07-17 16:37:11.391: E/AndroidRuntime(562): Caused by: java.lang.NullPointerException
07-17 16:37:11.391: E/AndroidRuntime(562): at com.example.imagegallery.Photos$ImagePagerAdapter.<init>(Photos.java:36)
07-17 16:37:11.391: E/AndroidRuntime(562): at com.example.imagegallery.Photos$ImagePagerAdapter.<init>(Photos.java:29)
07-17 16:37:11.391: E/AndroidRuntime(562): at com.example.imagegallery.Photos.onCreate(Photos.java:25)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-17 16:37:11.391: E/AndroidRuntime(562): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-17 16:37:11.391: E/AndroidRuntime(562): ... 11 more