我目前正在尝试使用Android图库。而不是imageViews我使用webview。这些网页浏览量无法点击,因此可以向左或向右滑动图库。但在长视图上,网页视图应该可以点击。我的代码完美地工作到版本2.2。现在我有一个3.0设备进行测试,代码在longclick失败...代码如下。
这里出错了
customGallery g = (customGallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
// ZOOM FUNCTION !
//Detect longclicks on the gallery if the user preforms a longclick we check if view is clickable (clickable means we can zoom in on the webview)
g.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View v, int position, long id) {
//IT GOES WRONG RIGHT HERE
try{
if(v.isClickable()){
Toast.makeText(ImageGalleryActivity.this, "Zoom disabled", Toast.LENGTH_SHORT).show();
v.setClickable(false);
}else{
v.setClickable(true);
Toast.makeText(ImageGalleryActivity.this, "Zoom enabled", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.i("Exception", String.format("%s", e));
Toast.makeText(ImageGalleryActivity.this, "NULLPNTR", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
以下是活动的完整代码......
public class ImageGalleryActivity extends Activity {
private WebView web;
public int screenWidth;
public int screenHeight;’
public View vx;
//ArrayList needed to quickly store the images we get from the AssetManager
ArrayList <String> imgID = new ArrayList <String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Asset Manager needed to collect all the files in specific folders of assets
final AssetManager assetManager = getAssets();
Display display = getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Log.i("Intial Height", String.format("%d", screenHeight));
Log.i("Intial Width", String.format("%d", screenWidth));
//Try collecting all the images
try{
String [] filelist = assetManager.list("book1");
if (filelist == null){
//TODO errorCatching
}else{
for (int i = 0; i<filelist.length; i++){
String fileName = filelist[i];
Log.i("THE FILENAMES", fileName);
//Add the images to the ArrayList (stringtype)
imgID.add(fileName);
}
}
}catch (IOException e){
}
setContentView(R.layout.main);
customGallery g = (customGallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
// ZOOM FUNCTION !
//Detect longclicks on the gallery if the user preforms a longclick we check if view is clickable (clickable means we can zoom in on the webview)
g.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView parent, View v, int position, long id) {
//IT GOES WRONG RIGHT HERE
try{
if(v.isClickable()){
Toast.makeText(ImageGalleryActivity.this, "Zoom disabled", Toast.LENGTH_SHORT).show();
v.setClickable(false);
}else{
v.setClickable(true);
Toast.makeText(ImageGalleryActivity.this, "Zoom enabled", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Log.i("Exception", String.format("%s", e));
Toast.makeText(ImageGalleryActivity.this, "NULLPNTR", Toast.LENGTH_SHORT).show();
}
return true;
}
});
}
//Create imageAdapter class...
public class ImageAdapter extends BaseAdapter{
int imagebackground;
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.gallery);
imagebackground = a.getResourceId(R.styleable.gallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount(){
return imgID.size();
}
public Object getItem(int position){
return position;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
web = new WebView(mContext);
String currImg = imgID.get(position);
Log.i("Drawable", String.format("%s", imgID.get(position)));
String stringScreenWidth = String.format("%d", screenWidth-200);
Log.i("Current ScreenWidth", String.format("%d", screenWidth));
Log.i("Should be", stringScreenWidth);
web.loadDataWithBaseURL("file:///android_asset/book1/", "<html><meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=10 minimum-scale=1\"><body style='width:"+screenWidth+"px;margin:0;padding:0;min-width:100%;'><image style='width:"+screenWidth+"px;' src='"+currImg+"'/><body></html>", "text/html", "UTF-8", null);
//web.getSettings().setBuiltInZoomControls(true);
web.setVerticalScrollBarEnabled(false);
web.setHorizontalScrollBarEnabled(false);
web.setMinimumHeight(1024);
web.getSettings().setSupportZoom( true ); //Modify this
web.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);//
web.setLayoutParams( new customGallery.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
web.setClickable(false);
web.setFocusable(false);
web.setLongClickable(true);
return web;
}
}
}
答案 0 :(得分:0)
我怀疑使用空(null)View v调用你的onItemLongClick()
方法。我不知道为什么在Android 3.0中发生这种情况,而不是在2.x中,所以无法帮助你。我能给你的唯一指针就是使用try / catch是一种糟糕的编程习惯,只是为了捕获一个nullpointer异常。您最好将代码更改为
if (v != null && v.isClickable()) {
....
}
您应该只捕获异常以处理不可预见的情况(作为一种最后的手段),因为抛出和处理异常相对昂贵,并且检查对象是否为空且行为正确更有效。