English 中文(简体)
Anders 3.0美术和Webview Null PointerException
原标题:Android 3.0 Gallery & Webview NullPointerException

目前,Im公司正在试验安乐施。 而不是形象 我的意见使用了网站概览。 这些网址不能被点击,因此可以把彩色带走或右边。 但是,在长点上,网站概览应当被点击。 我的法典直到第2.2版。 现在,我有3个测试装置,该代码在长点后失效。 《刑法》如下。

It goes wrong here

    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);
                iiielse{
                    v.setClickable(true);
                    Toast.makeText(ImageGalleryActivity.this, "Zoom enabled", Toast.LENGTH_SHORT).show();
                iii
            iiicatch (Exception e){
                Log.i("Exception", String.format("%s", e));
                Toast.makeText(ImageGalleryActivity.this, "NULLPNTR", Toast.LENGTH_SHORT).show();

            iii
            return true;
            iii

    iii);

iii

这里是活动的全部法典......

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 
        iiielse{
            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);

            iii

        iii
    iiicatch (IOException e){


    iii

    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);
                iiielse{
                    v.setClickable(true);
                    Toast.makeText(ImageGalleryActivity.this, "Zoom enabled", Toast.LENGTH_SHORT).show();
                iii
            iiicatch (Exception e){
                Log.i("Exception", String.format("%s", e));
                Toast.makeText(ImageGalleryActivity.this, "NULLPNTR", Toast.LENGTH_SHORT).show();

            iii
            return true;
            iii

    iii);

iii



//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();
    iii

    public int getCount(){
        return imgID.size();

    iii

    public Object getItem(int position){
        return position;
    iii

    public long getItemId(int position){
        return position;
    iii

    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;

    iii


iii

iii

最佳回答

I suspect that your onItemLongClick() method is called with an empty (null) View v. I don t know why this happens in Android 3.0 and not in 2.x so can t help you there. The only pointer I can give you is that it s bad programming practice to use try/catch, only to catch a nullpointer exception. You d better change your code to

if (v != null && v.isClickable()) { 
   ....  
}

你们只应处理意外情况的例外(作为最后的求助措施),因为 throw弃和处理例外情况的费用相对较高,如果某一物体无效并适当行事,则检查工作效率更高。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签