空指针的问题显示,从资产的图像文件夹的Andr​​oid 2.2 SDK [英] Null-pointer issue displaying an image from assets folder Android 2.2 SDK

查看:166
本文介绍了空指针的问题显示,从资产的图像文件夹的Andr​​oid 2.2 SDK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了看两个例子对堆栈,但不能让他们的工作。我只是试图从资产的文件夹抓取的图像,并将其设置在ImageView的,却得到了一个空指针返回。我究竟做错了什么?

主要活动:     包com.xxx.xxx;

 进口的java.io.InputStream;

进口android.app.AlertDialog;
进口android.graphics.Bitmap;
进口android.os.Bundle;
进口android.view.View;
进口android.view.Window;
进口android.widget.ImageView;
进口android.widget.ViewFlipper;

公共类SamplesViewFlipper扩展SamplesViewCreator {

    私人位图returnedImage;
    ImageView的imgView;
    ViewFlipper VF;
    私人字符串imageName =testImage.png;

    @覆盖
    公共无效的onCreate(包savedInstanceState){

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        returnedImage = getImageFromAsset(imageName);

        imgView =(ImageView的)findViewById(R.id.dynamicImageView);
        imgView.setImageBitmap(returnedImage); //< -null指针发生在这里

        VF =(ViewFlipper)findViewById(R.id.SamplesViewFlipper);
        的setContentView(R.layout.view_flipper_samples);

    }

    公共无效buttonClickHandler(查看视图){

        开关(view.getId()){

            案例R.id.nextSampleButton:

                vf.showNext();

                打破;

            案例R.id.backSampleButton:

                vf.show previous();

                打破;

        }

    }

}
 

扩展类:

 包com.xxx.xxx;

进口java.io.IOException异常;
进口的java.io.InputStream;

进口android.app.Activity;
进口android.content.res.AssetManager;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;

公共类SamplesViewCreator延伸活动{

     私人InputStream的是;
     私人位图位图;

    公共位图getImageFromAsset(字符串imageName){

        AssetManager mngr = getAssets();
        尝试 {

            是= mngr.open(文件:/// android_asset /样品/+ imageName);
            位= BitmapFactory.de codeStream(是);
                    //也尝试过文件/+ imageName每例如对堆栈

        }赶上(最终IOException异常E){

            e.printStackTrace();

        }

        返回的位图;

    }

}
 

和我的两个XML文件:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / SamplesLayout
    机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>
    < RelativeLayout的
        机器人:ID =@ + ID / samples_menu
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dp
        机器人:重力=中心
        机器人:后台=#0061F9>
        <按钮机器人:ID =@ + ID / nextSampleButton
            机器人:layout_marginRight =10dp
            机器人:重力=中心
            机器人:layout_width =50dp
            机器人:layout_height =30dp
            机器人:文本=下一步
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =13dp
            机器人:layout_weight =1
            机器人:背景=@可绘制/按钮
            机器人:layout_alignParentRight =真
            机器人:的onClick =buttonClickHandlerSamples/>
        <按钮机器人:ID =@ + ID / backSampleButton
            机器人:layout_marginLeft =10dp
            机器人:重力=中心
            机器人:layout_width =50dp
            机器人:layout_height =30dp
            机器人:文本=后退
            机器人:文字颜色=#FFFFFF
            机器人:TEXTSIZE =13dp
            机器人:layout_weight =1
            机器人:背景=@可绘制/按钮
            机器人:的onClick =buttonClickHandler/>
    < / RelativeLayout的>
    <的LinearLayout机器人:ID =@ + ID / SamplesViewFlipperLayout
        机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>
        < ViewFlipper机器人:ID =@ + ID / SamplesViewFlipper
            机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>
            <! - 将意见ViewFlipper  - >
            <的LinearLayout
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT
                机器人:以下属性来=@扪/ header_pad_left
                机器人:paddingRight =@扪/ header_pad_right
                机器人:paddingBottom会=@扪/ header_pad_bot
                机器人:paddingTop =@扪/ header_pad_top
                机器人:方向=垂直
                机器人:背景=@颜色/背景>

            < / LinearLayout中>
        < / ViewFlipper>
    < /线性
 

布局>

 < XML版本=1.0编码=UTF-8&GT?;
< ImageView的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / dynamicImageView
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT
    机器人:SRC =@可绘制/ default_samples_image/>
 

编辑:

也试过这个,但仍获得未发现异常文件。

 公共位图getImageFromAsset()抛出IOException异常{

        尝试 {

            是= getAssets()开(test3.png)。

                    位= BitmapFactory.de codeStream(是);

        }赶上(最终IOException异常E){

            e.printStackTrace();

        }

        的System.out.println(位图+位图);
        返回的位图;

    }
 

解决方案

在哪里文件:/// android_asset /样品/+ imageName 来自何处?如果您的层次结构看上去像资产/ file_name.jpg,你只需拨打打开(file_name.jpg)。换句话说,尝试更换你的文件:/// android_asset /样品/+ imageName 只有 imageName

退房的API演示,特别是ReadAsset.java类:

 尝试{
        InputStream的是= getAssets()开(read_asset.txt)。
 

...

,其中资产文件夹的样子

I looked at the 2 examples on Stack, but can't get them to work. I'm simply trying to grab an image from a folder in assets and set it as in ImageView, but get a null pointer returned. What am I doing wrong?

Main Activity: package com.xxx.xxx;

import java.io.InputStream;

import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class SamplesViewFlipper extends SamplesViewCreator {

    private Bitmap returnedImage;
    ImageView imgView;
    ViewFlipper vf;
    private String imageName = "testImage.png";

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        returnedImage = getImageFromAsset(imageName);

        imgView = (ImageView) findViewById(R.id.dynamicImageView);
        imgView.setImageBitmap(returnedImage); //<-null pointer happens here

        vf = (ViewFlipper) findViewById(R.id.SamplesViewFlipper);
        setContentView(R.layout.view_flipper_samples); 

    }

    public void buttonClickHandler(View view) {

        switch (view.getId()) {

            case R.id.nextSampleButton:

                vf.showNext();

                break;

            case R.id.backSampleButton:

                vf.showPrevious();

                break;

        }

    }

}

Extender Class:

package com.xxx.xxx;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class SamplesViewCreator extends Activity {

     private InputStream is;
     private Bitmap bitmap;

    public Bitmap getImageFromAsset(String imageName) {

        AssetManager mngr = getAssets();
        try {

            is = mngr.open("file:///android_asset/Samples/" + imageName);
            bitmap = BitmapFactory.decodeStream(is);
                    //also tried "Files/" + imageName per example on Stack

        } catch (final IOException e) {

            e.printStackTrace();

        }

        return bitmap;

    }

}

And my two xml files:

<?xml version="1.0" encoding="utf-8"?>      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/SamplesLayout"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <RelativeLayout
        android:id="@+id/samples_menu"
        android:layout_width="fill_parent" 
        android:layout_height="50dp" 
        android:gravity="center"
        android:background="#0061F9">
        <Button android:id="@+id/nextSampleButton"  
            android:layout_marginRight="10dp"
            android:gravity="center"    
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="Next"
            android:textColor="#FFFFFF"
            android:textSize="13dp"
            android:layout_weight="1"
            android:background="@drawable/button"
            android:layout_alignParentRight="true"
            android:onClick="buttonClickHandlerSamples"/>
        <Button android:id="@+id/backSampleButton"
            android:layout_marginLeft="10dp"
            android:gravity="center"    
            android:layout_width="50dp"
            android:layout_height="30dp"
            android:text="Back"
            android:textColor="#FFFFFF"
            android:textSize="13dp"
            android:layout_weight="1"
            android:background="@drawable/button"
            android:onClick="buttonClickHandler"/>
    </RelativeLayout>
    <LinearLayout android:id="@+id/SamplesViewFlipperLayout"
        android:layout_width="wrap_content" android:layout_height="wrap_content">
        <ViewFlipper android:id="@+id/SamplesViewFlipper"
            android:layout_width="wrap_content" android:layout_height="wrap_content">
            <!--adding views to ViewFlipper-->  
            <LinearLayout
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:paddingLeft="@dimen/header_pad_left"
                android:paddingRight="@dimen/header_pad_right"
                android:paddingBottom="@dimen/header_pad_bot"
                android:paddingTop="@dimen/header_pad_top"
                android:orientation="vertical"
                android:background="@color/background" >

            </LinearLayout>
        </ViewFlipper>
    </Linear

Layout>

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dynamicImageView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/default_samples_image"/>

EDIT:

Also tried this, but still get a file not found exception.

Public Bitmap getImageFromAsset() throws IOException {

        try {

            is = getAssets().open("test3.png");

                    bitmap = BitmapFactory.decodeStream(is);

        } catch (final IOException e) {

            e.printStackTrace();

        }

        System.out.println("bitmap is " + bitmap);
        return bitmap;

    }

解决方案

Where is "file:///android_asset/Samples/" + imageName coming from? If your hierarchy looks like assets/file_name.jpg, you would just call open(file_name.jpg). In other words, try replacing your file:///android_asset/Samples/" + imageName with just imageName.

Check out the API Demos, specifically the ReadAsset.java class:

try {
        InputStream is = getAssets().open("read_asset.txt");

...

where the assets folder looks like

这篇关于空指针的问题显示,从资产的图像文件夹的Andr​​oid 2.2 SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆