Android 无法解析符号“上下文”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21809486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot resolve symbol 'context'
提问by hexicle
I'm trying to write an Android app which automatically uploads a picture to a server, but I am stuck on just one line of code:
我正在尝试编写一个自动将图片上传到服务器的 Android 应用程序,但我只停留在一行代码上:
File f = File(context.getCacheDir(), "filename");
The error I get is
我得到的错误是
This puzzles me because I see so many examples on the web of people using context.getCacheDir()
just fine, whereas I get the error message.
It's probably something wrong with my IDE settings. I am using IntelliJ IDE.
这让我很困惑,因为我在网上看到很多人使用context.getCacheDir()
很好的例子,而我却收到了错误消息。我的IDE设置可能有问题。我正在使用 IntelliJ IDE。
Here's is the context of the context problem:
这是上下文问题的上下文:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
回答by Simon
You need to do some basic Java programming tutorials. Java is totally different to JavaScript.
您需要学习一些基本的 Java 编程教程。Java 与 JavaScript 完全不同。
Here, you use context
as a variable but you have neither declared it, or initialised it, hence the error.
在这里,您context
用作变量,但您既没有声明它,也没有初始化它,因此出现错误。
You could define it (and initialise at the same time)
您可以定义它(并同时初始化)
Context context = this;
since this
refers to the current object instance of a class and Activity
is a Context
, or more precisely, it extends
Context
.
因为this
指的是类的当前对象实例,并且Activity
是 a Context
,或者更准确地说是 it extends
Context
。
Alternatively, you could just use this
.
或者,您可以只使用this
.
File f = File(UploadToServer.this.getCacheDir(), "filename");
回答by dmSherazi
The error is bacuse you havent declared context
, neither it has been passed as a parameter
错误是因为你没有声明context
,也没有作为参数传递
change context.getCacheDir()
to getApplicationContext.getCacheDir()
or this.getCacheDir()
更改context.getCacheDir()
为getApplicationContext.getCacheDir()
或this.getCacheDir()
so
所以
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(context.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
will become
会变成
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if( requestCode == CAMERA_PIC_REQUEST)
{
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image =(ImageView) findViewById(R.id.PhotoCaptured);
image.setImageBitmap(thumbnail);
//create a file to write bitmap data
File f = File(getApplicationContext.getCacheDir(), "filename");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by Damo Gurcinovski
Add this :
添加这个:
Context context = this;
You need this to initialize context
你需要这个来初始化上下文
Then Alt+Enter
然后 Alt+Enter
You must assign context to this Activity
您必须为此活动分配上下文