我是Java的新手,但不是编程,但是有人可以帮助解释为什么我在处理从AsyncTask读取R.string时发生了崩溃?
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
我有一个AsyncTask类:
public class getBitmapFromURL extends AsyncTask<String, Void, Bitmap>{
ImageView bmImage;
private Context mContext;
public getBitmapFromURL( Context context, ImageView bmImage) {
this.bmImage = bmImage;
mContext = context;
}
protected Bitmap doInBackground(String... urls) {
try {
savebitmap( mIcon11, target_file[1]);
} catch (IOException e) {
e.printStackTrace();
}
}
和文件保存方法本身:
private File savebitmap(Bitmap bmp, String file_name) throws IOException {
//**** --- >>> This line causes the crash:
String app_name = mContext.getApplicationContext().getString(R.string.app_name);
Log.e( "APP_NAME INFO:", app_name);
File app_dest_folder = new File(Environment.getExternalStorageDirectory () + "/" + app_name );
if(!app_dest_folder.exists()){
app_dest_folder.mkdir();
...
调用AsyncTask是从GetContent类完成的:
public class GetContent extends AppCompatActivity {
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_content);
loadimages();
}
public void loadimages() {
new getBitmapFromURL(context, (ImageView)findViewById(R.id.wc_latest_iv)).execute(url);
}
...
答案 0 :(得分:1)
使用它来执行asynctask类
new getBitmapFromURL(GetContent.this, (ImageView)findViewById(R.id.wc_latest_iv)).execute(url);
这是因为你没有初始化
context
..