嗨,ImageButton
当我 setOnClickListener 时出现“strzalkaLewo”问题,我在 logCat 中有此错误:
进入
pozycjaZdjecia = bundle.getInt("zdjecie");
imgPath = ExternalStorageDirectoryPath
+ "/DailyPhotos/"+nazwa_projektu;
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.pokaz_slajdow);
strzalkaLewo = (ImageButton) findViewById(R.id.strzalkal);
strzalkaLewo.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
}
});
photoList = readFileList();
imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
imageSwitcher.setFactory(this);
/*
*
*/
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
imageSwitcher.setOnTouchListener(touchListener);
gallery = (Gallery)findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter( photoList,this));
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long when) {
newFilePath = photoList.get(position);
Bitmap bm = decodeSampledBitmapFromUri(photoList.get(position), 480,
360);
//Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
// Bitmap bm = BitmapFactory.decodeFile(photoList.get(position));
BitmapDrawable bd = new BitmapDrawable(bm);
imageSwitcher.setImageDrawable(bd);
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
gallery.setSelection(pozycjaZdjecia, true);
}
我还使用 OnTouchListener 在图像之间切换:
enter private OnTouchListener touchListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN) //lokalizacja poczatkowa.
{
downX=(int) event.getX();//
return true;
}
else if(event.getAction()==MotionEvent.ACTION_UP) //lokalizacja zakonczona
{
upX=(int) event.getX();//
int index = 0;
if(upX-downX>60)//?
{
//?
if(gallery.getSelectedItemPosition()==0)
index=gallery.getCount()-1;
else
index=gallery.getSelectedItemPosition()-1;
}
else if(downX-upX>60)//?
{
//?
if(gallery.getSelectedItemPosition()==(gallery.getCount()-1))
index=0;
else
index=gallery.getSelectedItemPosition()+1;
}
日志猫:
09-18 11:52:18.888: E/AndroidRuntime(6397): FATAL EXCEPTION: main
09-18 11:52:18.888: E/AndroidRuntime(6397): java.lang.NullPointerException
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.example.dailyphotos.GaleriaImg$1.onTouch(GaleriaImg.java:133)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.View.dispatchTouchEvent(View.java:7122)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.View.dispatchPointerEvent(View.java:7307)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.os.MessageQueue.nativePollOnce(Native Method)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.os.MessageQueue.next(MessageQueue.java:125)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.os.Looper.loop(Looper.java:124)
09-18 11:52:18.888: E/AndroidRuntime(6397): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-18 11:52:18.888: E/AndroidRuntime(6397): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 11:52:18.888: E/AndroidRuntime(6397): at java.lang.reflect.Method.invoke(Method.java:511)
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-18 11:52:18.888: E/AndroidRuntime(6397): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-18 11:52:18.888: E/AndroidRuntime(6397): at dalvik.system.NativeStart.main(Native Method)