我有这个问题......
我的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为null ...这意味着,我在上面代码的第三行得到一个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)
提前感谢你们给我的任何帮助!!
答案 0 :(得分:0)
您无法从设置活动访问主活动中的imageview。你可以做的是将图像路径保存在SharedPreference中,然后例如在主活动的onResume()方法中查看属性是否已设置,然后相应地实例化图像。您可能需要添加一些缓存,以避免不必要的加载。