【问题标题】:requireActivity().getApplicationContext() "may produce a NullPointerExeption"requireActivity().getApplicationContext() “可能会产生 NullPointerExeption”
【发布时间】:2021-12-01 09:58:56
【问题描述】:

下面是我的应用程序的一些代码。我需要帮助的部分在 SettingFragment.java 中的requireActivity()

MainActivity.java

...
public class MainActivity extends AppCompatActivity {
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      ...
   }

   public void iniciarIntent3() {   //this method is called by a button in activity_main.xml
        Intent intent = new Intent(this, Settings.class);
        startActivityForResult(intent, 1);
   }
}

Settings.java

...

public class Settings extends AppCompatActivity implements SettingsFragment.SendToActivity {
   ...
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      ...
      if (savedInstanceState == null) {
         getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.settings, new SettingsFragment())
            .commit();
      }
   }

   @Override
   public void onAttachFragment(Fragment fragment) {
      if (fragment instanceof SettingsFragment) {
         SettingsFragment settingsFragment = (SettingsFragment) fragment;
         settingsFragment.setSendToActivity(this);
      }
   }
   public void send(int result) {
      ...
   }
}   

SettingsFragment.java

public class SettingsFragment extends PreferenceFragmentCompat {

   SendToActivity callback;

   public void setSendToActivity (SendToActivity callback) {
      this.callback = callback;
   }

   public interface SendToActivity {
      void send(int result);
   }
   
   if (editTextPreference != null) {
      editTextPreference.setOnBindEditTextListener(new 
      EditTextPreference.OnBindEditTextListener() {
         @Override
         public void onBindEditText(@NonNull EditText editText) {
            editText.setInputType(InputType.TYPE_CLASS_NUMBER);
            editText.setText("");                
            editText.setBackground(ContextCompat.getDrawable(requireActivity()
               .getApplicationContext(), R.drawable.fondo_edittextpreference));
         }
      });
   }
   ...
}

我只是想确保 SettingsFragment 类中的 requireActivity() 不会抛出空指针异常。你能帮我检查一下吗?

【问题讨论】:

  • 这完全取决于您执行requireActivity() 调用时片段的状态以及当时它是否附加到活动。您可以通过使用setBackgroundResource() 或从EditText 获取Context 来消除requireActivity() 调用。
  • @CommonsWare 对,我将使用setBackgroundResource()。谢谢。

标签: java android android-activity fragment


【解决方案1】:

您可以执行以下任一操作,而不是 requireActivity().getApplicationContext()

  1. editText.setBackground(ContextCompat.getDrawable(editText.getContext(), R.drawable.fondo_edittextpreference))
  2. editText.setBackgroundResource(R.drawable.fondo_edittextpreference)

【讨论】:

    【解决方案2】:

    requireActivity()

    如果 Activity 出于任何原因为 null,则可以抛出 null。 我相信如果用户存在应用程序或预先停止它等,它可以为空。或者在你所在的上下文中从未创建过一个

    只是尝试捕捉

    Activity activity;
    try {
        activity = requireActivity()
        editText.setBackground(
            ContextCompat.getDrawable(
                activity.getApplicationContext(),
                R.drawable.fondo_edittextpreference));
    } catch (NullPointerException orTypeOfException) {
        //handle here when the activity is null
    }
    

    【讨论】:

    • 好的,谢谢。
    • 这是有缺陷的。 requireActivity() 会抛出异常,但永远不会返回 null。 else 永远不会到达,因为应用程序会崩溃。
    • 正如@Michiel 所说,requireActivity() 的点永远不会为空。通常它用于内联用法,您不希望 lint 在 IDE 中乱扔警告。一般来说,如果您需要 Activity 及其 null,那么逻辑可能有缺陷,应该会引发不可恢复的错误。
    猜你喜欢
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2020-11-05
    • 2013-07-28
    • 2021-10-20
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多