我有一個ImageView的,如果它是,空的和點擊時,應該從因特網加載圖像,並保存到內部存儲器中的活動。因此,活動如下:的NullPointerException在openFileOutput在活動
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
行了。我發現了幾個類似的問題,但所有的答案都暗示null
是一個缺失的上下文。由於在這種情況下它是一項活動,因此它顯然具有上下文(this
),並且它不爲空。我嘗試致電openFileOutput
(暗示爲this
),並通過context
成員。兩種方法都失敗。此外,我試圖從onClick
通過v.getContext()
成Download
作爲附加參數,該值是不是空的,但所產生的相同的異常爲好。 (如果是重要的,請求的文件的名稱是一個有效的文件名,例如「ABC」。)
可能有人闡明這一些輕?
這裏是堆棧的例子:
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
文件中發生錯誤,並且我看到parent
局部變量在此方法內爲null。下面是代碼:
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()
不會返回null,爲什麼它的工作原理不時?
當你說...名稱是一個有效的文件名,例如「abc」,是你嘗試過的實際文件名? – Squonk 2012-04-21 13:56:46
請提供完整的堆棧跟蹤。 – CommonsWare 2012-04-21 14:58:17
@MisterSquonk:是的,我也嘗試過'abc'。這個名字有什麼問題? – Stan 2012-04-21 18:57:19