0

我有这个问题...

我的 android 应用程序有一个设置菜单来设置 IP 地址和图像。我可以使用正则表达式验证 IP 地址并在设备的根目录中查找图像,但想法是用户设置的图像必须替换应用程序主要活动中的默认图像......为此,我声明了一个 ImageView 并使用 R.java 文件中的正确 ID(包含默认图像的 ID)进行了初始化,然后将其替换为以下说明:

Bitmap bmap = BitmapFactory.decodeFile(selectedFile.getAbsolutePath());
Drawable dra = new BitmapDrawable(bmap);
imagen.setImageDrawable(dra);

其中 imagen 是 ImageView 对象。但由于某种原因,当我运行这段代码时,logcat 显示 imagen 为空......这意味着,我在上面代码的第三行中得到了 NullPointerException ......

这是设置菜单活动的完整代码:

public class pantallaConfiguracion extends Activity{
    private static final int REQUEST_PICK_FILE = 1;
    private EditText mFilePathTextView,ip;
    private Button mStartActivityButton, volver, guardar;
    private File selectedFile;
    private ImageView imagen;
    String dirIp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.configuraciones);
        ip = (EditText) findViewById(R.id.entradaIP);
        volver = (Button) findViewById(R.id.botonVolver);
        guardar = (Button) findViewById(R.id.botonGuardar);
        mFilePathTextView = (EditText)findViewById(R.id.entradaImg);
        mStartActivityButton = (Button)findViewById(R.id.botonBrowse);
        imagen = (ImageView) findViewById(R.id.imagen1);
        volver.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {
                finish();
            }
        });
        guardar.setOnClickListener(new OnClickListener(){
            @SuppressWarnings("deprecation")
            public void onClick(View v) {
                dirIp = ip.getText().toString();
                if(validarIp(dirIp) == false){
                    Toast.makeText(getApplicationContext(), "Dirección IP incorrecta.", Toast.LENGTH_SHORT).show();
                }else{
                    Main.IP_Server = dirIp;
                    descargarDatos.IP_Server = dirIp;
                    enviarDatos.IP_Server = dirIp;
                    Bitmap bmap = BitmapFactory.decodeFile(selectedFile.getAbsolutePath());
                    Drawable dra = new BitmapDrawable(bmap);
                    imagen.setImageDrawable(dra);
                    Toast.makeText(getApplicationContext(), "Cambios Efectuados Correctamente.", Toast.LENGTH_SHORT).show();
                }
            }
            private boolean validarIp(String dirIp) {
                String patron = "(([0-1]?[0-9]{1,2}\\.)|(2[0-4][0-9]\\.)|(25[0-5]\\.)){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))";
                Pattern expreg = Pattern.compile(patron);
                Matcher m;
                m = expreg.matcher(dirIp);
                if(m.matches() == false){
                    return false;
                }else{
                    return true;
                }
            }
        });
        mStartActivityButton.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                switch(v.getId()) {
                case R.id.botonBrowse:
                    Intent intent = new Intent(pantallaConfiguracion.this, FilePicker.class);
                    startActivityForResult(intent, REQUEST_PICK_FILE);
                    break;
                default:
                    break;
                }
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK) {
            switch(requestCode) {
            case REQUEST_PICK_FILE:
                if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
                    selectedFile = new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
                    mFilePathTextView.setText(selectedFile.getPath());              
                }
            }
        }
    }
}

这是 logcat 输出:

04-02 13:40:17.671: E/AndroidRuntime(3139): FATAL EXCEPTION: main
04-02 13:40:17.671: E/AndroidRuntime(3139): java.lang.NullPointerException
04-02 13:40:17.671: E/AndroidRuntime(3139):     at tian.proto.pantallaConfiguracion$2.onClick(pantallaConfiguracion.java:73)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.view.View.performClick(View.java:2485)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.view.View$PerformClick.run(View.java:9080)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.os.Handler.handleCallback(Handler.java:587)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.os.Looper.loop(Looper.java:130)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at java.lang.reflect.Method.invokeNative(Native Method)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at java.lang.reflect.Method.invoke(Method.java:507)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
04-02 13:40:17.671: E/AndroidRuntime(3139):     at dalvik.system.NativeStart.main(Native Method)

提前感谢您能给我的任何帮助!!

4

1 回答 1

0

您无法从设置活动访问主活动中的图像视图。您可以做的是将图像路径保存在 SharedPreference 中,然后在主要活动的 onResume() 方法中查看是否设置了属性,然后相应地实例化图像。您可能需要添加一些缓存以避免不必要的加载。

于 2013-04-02T19:22:09.843 回答