NullPointerException异常的openFileOutput的活动 [英] NullPointerException at openFileOutput in Activity

查看:148
本文介绍了NullPointerException异常的openFileOutput的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ImageView的,而如果它是空的,点击,要加载的图像来自互联网,并将其保存到内部存储器中的活动。因此,该活动是这样的:

I have an Activity with an ImageView, which, if it is empty and clicked, should load an image from Internet and save it to internal storage. So the activity looks like this:

public class PlaceCreate extends Activity {
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.create);
  context = this;

  ImageView img = (ImageView) findViewById(R.id.imageView);
  img.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        EditText edit = (EditText)findViewById(R.id.editTextName);
        Download(edit.getText().toString());
    }
  });
}

private boolean Download(String Name)
{
  try
  {
    URL u = new URL("http://example.com/img.png");
    HttpURLConnection c = (HttpURLConnection)u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();
    FileOutputStream f = context.openFileOutput(Name, Context.MODE_PRIVATE);
    InputStream in = c.getInputStream();
    byte[] buffer = new byte[1024];
    int len1 = 0;
    while((len1 = in.read(buffer)) > 0 )
    {
      f.write(buffer, 0, len1);
    }
    f.close();

    FileInputStream is = openFileInput(Name);
    Bitmap bitmap = BitmapFactory.decodeStream(is);
    is.close();
    ImageView img = (ImageView) findViewById(R.id.imageViewPlan);
    img.setImageBitmap(bitmap);
  }
  catch(IOException e)
  {
    return false;
  }
  return true;
}}

现在的问题是,我得到的NullPointerException与 openFileOutput 行。我发现了几个类似的问题,但所有的答案意味着是缺少上下文。因为它是在这种情况下的活动,它显然有一个上下文(),并且它不为空。席力图召 openFileOutput (用暗示),并通过上下文成员。两种方法都失败。我也试图通过 v.getContext()的onClick 下载作为附加参数,并且此值不是空的,但所产生的相同的异常为好。 (如果它是重要的,所要求的文件的名称是一个有效的文件名,例如ABC。)

The problem is that I get NullPointerException on the line with openFileOutput. I found a couple of similar questions, but all answers imply that null is a missing context. As it's an activity in this case, it obviously has a context (this), and it is not null. I tried to call openFileOutput (with this implied) and via the context member. Either approach fails. Also I tried to pass v.getContext() from onClick into Download as an additional parameter, and this value was not null, yet produced the same exception as well. (If it is important, the name of the file requested is a valid filename, for example "abc".)

可能有人阐明这一些轻?

Could someone shed some light on this?

下面是栈的实例:

04-22 00:45:10.659: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-22 00:45:10.909: E/AndroidRuntime(330): FATAL EXCEPTION: main
04-22 00:45:10.909: E/AndroidRuntime(330): java.lang.NullPointerException
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.app.ContextImpl.openFileOutput(ContextImpl.java:420)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:158)
04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate.Download(PlaceCreate.java:93)
04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate.access$0(PlaceCreate.java:84)
04-22 00:45:10.909: E/AndroidRuntime(330):  at com.example.scanner.PlaceCreate$3.onClick(PlaceCreate.java:67)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.view.View.performClick(View.java:2485)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.view.View$PerformClick.run(View.java:9080)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Handler.handleCallback(Handler.java:587)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.os.Looper.loop(Looper.java:123)
04-22 00:45:10.909: E/AndroidRuntime(330):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-22 00:45:10.909: E/AndroidRuntime(330):  at java.lang.reflect.Method.invokeNative(Native Method)
04-22 00:45:10.909: E/AndroidRuntime(330):  at java.lang.reflect.Method.invoke(Method.java:507)
04-22 00:45:10.909: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-22 00:45:10.909: E/AndroidRuntime(330):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-22 00:45:10.909: E/AndroidRuntime(330):  at dalvik.system.NativeStart.main(Native Method)

下面是一些调查结果。根据该协议栈,错误里面 ContextImpl occures 文件,我看到局部变量是这个方法里面空。这里是code:

And here is some more findings. According to the stack, the error occures inside ContextImpl file, and I see that parent local variable is null inside this method. Here is the code:

public FileOutputStream openFileOutput(String name, int mode)
  throws FileNotFoundException {
  final boolean append = (mode&MODE_APPEND) != 0;
  File f = makeFilename(getFilesDir(), name);
  try {
     FileOutputStream fos = new FileOutputStream(f, append);
     setFilePermissionsFromMode(f.getPath(), mode, 0);
     return fos;
  } catch (FileNotFoundException e) {
 }

 File parent = f.getParentFile();
 parent.mkdir();
 FileUtils.setPermissions(
     parent.getPath(),
     FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
     -1, -1);
 FileOutputStream fos = new FileOutputStream(f, append);
 setFilePermissionsFromMode(f.getPath(), mode, 0);
 return fos;
}

所以,现在的问题是,我如何才能确保调用 f.getParentFile()将不会返回空值,为什么它的工作原理不时?

So now the question is how can I make sure that the call to f.getParentFile() will not return null, and why does it work from time to time?

推荐答案

这是被证明是这里安装为SDK的一部分的Andr​​oid 2.3.3模拟器的功能。如果我用一个真实的设备与Android 2.3.3不会发生此​​问题。另外,code工作正常,如果我采用Android 4.0.3模拟器。所以,如果一些奇怪的事情发生,测试在另一个环境中可以提供一些线索。我希望这可以帮助别人类似的解决问题。

This is proved to be a feature of Android 2.3.3 emulator installed here as a part of SDK. The problem does not occur if I use a real device with Android 2.3.3. Also the code works ok if I use Android 4.0.3 emulator. So, if some weird things happen, tests in another environment can shed some light. I hope this can help someone else in similar problem solving.

这篇关于NullPointerException异常的openFileOutput的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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