简体   繁体   中英

error when trying to build the apk for my app

I am getting the following error when building the apk for my app to run on my cellphone, im not sure where the actually error is so im not sure how to solve it.

错误

I have looked through my code and I am not seeing any errors. Any help would be appreciated. Thank you

Here is some of my code:

public View getView(int position, View convertView, ViewGroup parent){         
   View v = convertView; 
   if(v == null){ 
     LayoutInflater inflater = ((Activity)context).getLayoutInflater(); 
         v = inflater.inflate(R.layout.cell, parent, false); 
   }  
    return v; 
 } 

The problem is here: ((Activity)context).getLayoutInflater(); . You can't cast context to an activity. For that call you should be doing:

context.getLayoutInflater();

If that doesn't work (because you are in a fragment, or outside the activity) then do:

getActivity().getLayoutInflater();

You can also use getApplicationContext , which will grab the context from anywhere in the app, verses the above getActivity which will get the context for the current activity:

getApplicationContext().getLayoutInflater();

Either way, you can't cast the context into an Activity.

Two Solutions one for getting application context using getApplicationContext() , second one in the adapter constructor contains this (Context context, int res, List obj) declare a private variable of type context and assign it in adapter like this this.context = context;

public View getView(int position, View convertView, ViewGroup parent){         
   View v = convertView; 
   if(v == null){ 
     LayoutInflater inflater = (getApplicationContext()).getLayoutInflater(); 
         v = inflater.inflate(R.layout.cell, parent, false); 
   }  
    return v; 
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM