Popüler 35 Lint Warning ve Çözümleri

EgemenMede
Etiya
Published in
4 min readFeb 5, 2018

Android geliştirme yapanlar için olmazsa olmaz statik kod analizlerinden olan Lint’in karşımıza çıkardığı Warning mesajları için çözümlere aşağıdaki yazımızdan ulaşabilirsiniz.

  1. ‘getColor(int)’ is deprecated as of API 23: Android 6.0 (Marshmallow)

getResources().getColor(R.color.white);

Yerine,

ContextCompat.getColor(context, R.color.white);

Bunun için aşağıdaki importu da gerçekleştirmeniz gerekiyor.

import android.support.v4.content.ContextCompat;

2. Number formatting does not take into account locale settings. Consider using ‘String.format’ instead.

usageUsedData.setText(myFamily.getDataValueUsed().toString());

Yerine,

usageUsedData.setText(String.format(“%s”, myFamily.getDataValueUsed().toString()));

3. Unchecked assignment: ‘java.util.ArrayList’ to ‘java.util.List<java.lang.String>’

public List<String> mPartiesTask = new ArrayList();

Yerine,

public List<String> mPartiesTask = new ArrayList<>();

4. AppCompatDialogFragment.setupDialog can only be called from within the same library group (groupId=com.android.support)

@SuppressLint(“RestrictedApi”)
@Override
public void setupDialog(Dialog dialog, int style) {
super.setupDialog(dialog, style);
this.dialog = dialog;
drawDialog();
}

5. Call requires API level 24 (current min is 23): new android.icu.text.SimpleDateFormat
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.getDefault());

android.icu.text.SimpleDateFormat

Yerine,

java.text.SimpleDateFormat

import edilmeli.

6. Call requires API level 24 (current min is 23): android.icu.text.DateFormat#parse
Ya da
Call requires API level 24 (current min is 23): android.icu.text.DateFormat#format

import android.icu.text.DateFormat;

Yerine,

import java.text.DateFormat;

import edilmeli.

7. To get local formatting use ‘getDateInstance()’, ‘getDateTimeInstance()’, or ‘getTimeInstance()’, or use ‘new SimpleDateFormat(String template, Locale locale)’ with for example ‘Locale.US’ for ASCII dates.

formatters = new SimpleDateFormat(“dd/MM/yyyy”);

Yerine,

formatters = new SimpleDateFormat(“dd/MM/yyyy”, Locale.getDefault());

8. Use ‘new SparseArray<String>(…)’ instead for better performance

private final HashMap<Integer, String> saveSearchInput = new HashMap<>();

Yerine,

private final SparseArray<String> saveSearchInput = new SparseArray<>();

9. Number formatting does not take into account locale settings. Consider using ‘String.format’ instead.

tvCount.setText(totalElements.toString());

Yerine,

tvCount.setText(String.format(“%s”, totalElements.toString()));

10. Parameter ‘view’ is not used in either this method or any of its derived methods

public void redeemOnClick(View view) {
redeemProcess();
}

Yerine,

public void redeemOnClick() {
redeemProcess();
}

11. Field can be converted to a local variable

Global tanımlama yerine
private SuperToast superActivityToast;

Yerine,

Kullanıldığı yere
SuperToast superActivityToast;

12. Declaration can have final modifier

private List<ProductContent> contentList = new ArrayList<>();

Yerine,

private final List<ProductContent> contentList = new ArrayList<>();

13. Can be private

Context context;

Yerine,

private Context context;

14. Variable ‘totalElements’ is never used

Integer totalElements = customerProducts.getProductList().getTotalElements();

Yerine,

Silin!

15. Implicitly using the default locale is a common source of bugs: Use ‘String.format(Locale, …)’ instead

tvPrice.setText(String.format(“%d c$”, value));

Yerine,

tvPrice.setText(String.format(“%s c$”, value));

16. String literal in ‘setText’ can not be translated. Use Android resources instead.

tvOrderDeliveryDatePrice.setText(“20–12–2019”);

Yerine,

tvOrderDeliveryDatePrice.setText(getResources().getString(R.string.deliverydate));

17. Do not concatenate text displayed with ‘setText’. Use resource string with placeholders.

pay_card_number.setText(pay_card_number.getText() + “-”);

Yerine,

pay_card_number.setText(String.format(“%s”, pay_card_number.getText() + “-”));

18. Method only calls its super
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

Yerine,

Silin!

19. ‘getDrawable(int)’ is deprecated as of API 22: Android 5.1 (Lollipop)

tvNotificationCount.setBackground(getResources().getDrawable(R.drawable.badge_notification_bg));

Yerine,

tvNotificationCount.setBackground(ContextCompat.getDrawable(context, R.drawable.badge_notification_bg));

20. ‘setActionView(android.view.MenuItem, int)’ is deprecated

MenuItem cartItem = mMenu.findItem(R.id.action_cart);
MenuItemCompat.setActionView(cartItem, R.layout.notification_update_count_layout);
notificationCount1 = (RelativeLayout) MenuItemCompat.getActionView(cartItem);

Yerine,

MenuItem cartItem = mMenu.findItem(R.id.action_cart);
cartItem.setActionView(R.layout.notification_update_count_layout);
MenuItemCompat.getActionView(cartItem);
notificationCount1 = (RelativeLayout) cartItem.getActionView();

21. ‘optionalControlCharManuel == true’ can be simplified to ‘optionalControlCharManuel’

if (optionalControlCharManuel == true || optionalControlChar() == true)

Yerine,

if (optionalControlCharManuel || optionalControlChar())

22. ‘if’ statement can be replaced with ‘return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);’

if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);

Yerine,

return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item);

23. Unnecessary unboxing ‘formattedLongDate.longValue()’

String time = Utils.getTimeAgo(context, formattedLongDate.longValue());

Yerine,

String time = Utils.getTimeAgo(context, formattedLongDate);

24. Casting ‘findViewById(R.id.tvInvisibleError)’ to ‘TextView’ is redundant

TextView tvInvisibleError = (TextView) findViewById(R.id.tvInvisibleError);

Yerine,

TextView tvInvisibleError = findViewById(R.id.tvInvisibleError);

25. Unchecked call to ‘add(E)’ as a member of raw type ‘java.util.ArrayList’

ArrayList images = new ArrayList<>();

Yerine,

ArrayList<String> images = new ArrayList<>();

26. String values are compared using ‘’, not ‘equals()’

if (simpleActivityReturn == Utils.OK)

Yerine,

if ((Utils.OK).equals(simpleActivityReturn))

27. Expected resource of type id

rangeSeekbar.setId(2);

Yerine,

rangeSeekbar.setId(View.generateViewId());

28. Toast created but not shown: did you forget to call ‘show()’ ?

Toast.makeText(context, String.valueOf(id), Toast.LENGTH_LONG);

Yerine,

Toast.makeText(context, String.valueOf(id), Toast.LENGTH_LONG).show();

29. ‘expandActionView(android.view.MenuItem)’ is deprecated

MenuItemCompat.expandActionView(searchMenuItem);

Yerine,

searchMenuItem.expandActionView();

30. ‘android.support.v4.view.MenuItemCompat.OnActionExpandListener’ is deprecated

MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
finishMyActivity();
return true;
}
});

Yerine,

searchMenuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
@Override
public boolean onMenuItemActionExpand(MenuItem menuItem) {
return true;
}

@Override
public boolean onMenuItemActionCollapse(MenuItem menuItem) {
finishMyActivity();
return true;
}
});

31. ‘StringBuilder builder’ can be replaced with ‘String’

StringBuilder builder = new StringBuilder();
builder.append(“%.”);
builder.append(String.valueOf(decimalLength));
builder.append(“f”);
return builder.toString();

Yerine,

return “%.” + String.valueOf(decimalLength) + “f”;

32. ‘setOnPageChangeListener(android.support.v4.view.ViewPager.OnPageChangeListener)’ is deprecated

pager.setOnPageChangeListener(this);

Yerine,

pager.addOnPageChangeListener(this);

33. Implicitly using the default locale is a common source of bugs: Use ‘String.format(Locale, …)’ instead

public static String formattedRating(Float value) {
return String.format(“%.2f”, value);
}

Yerine,

public static String formattedRating(Float value) {
return String.format(Locale.getDefault(), “%.2f”, value);
}

34. ‘setBackgroundDrawable(android.graphics.drawable.Drawable)’ is deprecated as of API 16: Android 4.1 (Jelly Bean)

parentView.get(i).setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.border_set_error));

Yerine,

parentView.get(i).setBackgroundResource(R.drawable.border_set_error);

35. Missing recycle() calls
This ‘TypedArray’ should be recycled after use with ‘#recycle()’

public ColorPicker setColors(int resId) {
TypedArray ta = context.getResources().obtainTypedArray(resId);
colors = new ArrayList<>();
for (int i = 0; i < ta.length(); i++) {
colors.add(new ColorPal(ta.getColor(i, 0), false));
}
return this;
}

Yerine,

public ColorPicker setColors(int resId) {
TypedArray ta = context.getResources().obtainTypedArray(resId);
colors = new ArrayList<>();
for (int i = 0; i < ta.length(); i++) {
colors.add(new ColorPal(ta.getColor(i, 0), false));
}
ta.recycle();
return this;
}

Yazıyı beğendiyseniz, alkışlamayı unutmayın.

--

--

EgemenMede
Etiya
Editor for

Mobile Software Manager, Software Developer, Android&Flutter Lover, Technology Editor, Father, Storyteller..