我試圖找到一種方法,在刷卡我的Android application.I圖像找到如何做到這一點與TextView的一個例子,但我不能做ImageView.Here同樣的事情,是原來的與TextView的代碼:Android的刷卡圖片
HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());
// Add some views to it
final int[] backgroundColors =
{ Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW };
for (int i = 0; i < 5; i++) {
TextView textView = new TextView(getApplicationContext());
textView.setText(Integer.toString(i + 1));
textView.setTextSize(100);
textView.setTextColor(Color.BLACK);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(backgroundColors[i]);
realViewSwitcher.addView(textView);
}
// set as content view
setContentView(realViewSwitcher);
,這裏是我試圖用ImageViews做代碼:
HorizontalPager realViewSwitcher = new HorizontalPager(getApplicationContext());
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("one", R.drawable.one);
map.put("two", R.drawable.two);
map.put("three", R.drawable.three);
img1.setImageResource(map.get("one"));
img2.setImageResource(map.get("two"));
img3.setImageResource(map.get("three"));
realViewSwitcher.addView(img1);
realViewSwitcher.addView(img2);
realViewSwitcher.addView(img3);
setContentView(realViewSwitcher);
的圖像一,二,三是在繪製folder.When我用我的代碼它拋出我一個例外:
LogCat:
08-09 09:21:24.097: ERROR/AndroidRuntime(743): Uncaught handler: thread main exiting due to uncaught exception
08-09 09:21:24.115: ERROR/AndroidRuntime(743): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stampii.stampii/com.stampii.stampii.cards.Cards}: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.os.Looper.loop(Looper.java:123)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at java.lang.reflect.Method.invoke(Method.java:521)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at dalvik.system.NativeStart.main(Native Method)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): Caused by: java.lang.NullPointerException
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at com.stampii.stampii.cards.Cards.onCreate(Cards.java:30)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
08-09 09:21:24.115: ERROR/AndroidRuntime(743): ... 11 more
任何想法如何讓事情工作?
沒錯的viewpager,它的工作!非常感謝!另一個問題是:我怎麼可以這樣做:當我點擊我想讓它顯示兩個按鈕,仍然在圖像上能夠刷卡images.Any建議我應該怎麼用? –
請問未來的參考問題!這個新問題很重要 –
問題問! –