java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference

后端 未结 4 826
青春惊慌失措
青春惊慌失措 2020-12-17 20:25

I have a class called MainActivity.java that call an AsyncTask class. The last class have a findViewById() that in execution retur

4条回答
  •  太阳男子
    2020-12-17 21:03

    put imageview as a variable of your class

    private ImageView image;
    

    on your onCreate initialize

    image = (ImageView) findViewById(R.id.image);
    
    
    public class Connection extends AsyncTask {
            public String result;
    
            //I know, this isn't correct, how can i do?
            public MainActivity MainActivity;
    
            @Override
            protected String doInBackground(String... arg0) {
            // TODO Auto-generated method stub
                //...
    
            return "a string";
        }
    
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
                    //...
    
            // Where the error is generated
            //do other stuff with your imageview
    
            //...
    
        }
    }
    

提交回复
热议问题