从AsyncTask中的上下文中读取R.string

问题描述 投票:0回答:1

我是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;
    }

在doInBackground方法中:

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);
}

...
java android
1个回答
1
投票

使用它来执行asynctask类

   new getBitmapFromURL(GetContent.this, (ImageView)findViewById(R.id.wc_latest_iv)).execute(url);

这是因为你没有初始化qazxsw poi ..

© www.soinside.com 2019 - 2024. All rights reserved.