【问题标题】:"java.lang.IllegalArgumentException: view must not be null" Program Closes when navigated“java.lang.IllegalArgumentException:视图不能为空”程序在导航时关闭
【发布时间】:2017-12-20 18:51:40
【问题描述】:

每当我导航到 ProfileActivity 时,应用程序就会关闭。

致命异常:主进程:hfad.com.hallofmemesprototype,PID: 19092 java.lang.RuntimeException:无法启动活动 组件信息{hfad.com.hallofmemesprototype/hfad.com.hallofmemesprototype.Profile.ProfileActivity}: java.lang.IllegalArgumentException: 视图不能为空

这是“ProfileActivity”代码。

public class ProfileActivity extends AppCompatActivity {
private static final int ACTIVITY_NUM = 3;

private Context mContext = ProfileActivity.this;

private ProgressBar mProgressBar;
private ImageView profilePhoto;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    setupBottomNavigationView();
    setupToolbar();
    setupActivityWidgets();
    setProfileImage();
}

private void setProfileImage(){
    String imgURL = "www.androidzone.org/wp-content/uploads/2013/02/android-musical2.jpg";
    UniversalImageLoader.setImage( imgURL, profilePhoto, mProgressBar, "https://");
}

private void setupActivityWidgets(){
    mProgressBar = findViewById(R.id.profileProgressBar);
    mProgressBar.setVisibility(View.GONE);
    profilePhoto = findViewById(R.id.profile_photo);
}

private void setupToolbar() {
    Toolbar toolbar = findViewById(R.id.profileToolBar);
    setSupportActionBar(toolbar);

    ImageView profileMenu = findViewById(R.id.profileMenu);
    profileMenu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(mContext, AccountSettingsActivity.class);
            startActivity(intent);
        }
    });

}

/**
 * BottomNavigationView setup
 */
private void setupBottomNavigationView() {
    BottomNavigationViewEx bottomNavigationViewEx = findViewById(R.id.bottomNavViewBar);
    BottomNavigationViewHelper.bottomNavigationView(bottomNavigationViewEx);
    BottomNavigationViewHelper.enableNavigation(mContext, bottomNavigationViewEx);
    Menu menu = bottomNavigationViewEx.getMenu();
    MenuItem menuItem = menu.getItem(ACTIVITY_NUM);
    menuItem.setChecked(true);

}

}

【问题讨论】:

    标签: android-studio imageview


    【解决方案1】:

    您必须检查您使用findViewById() 获得的所有视图是否确实存在于activity_profile.xml 布局中。

    一个或多个视图实际上并不存在,并且您有一个空值获取引用。

     findViewById(R.id.profileProgressBar);
     findViewById(R.id.profile_photo);
     findViewById(R.id.profileToolBar);
     findViewById(R.id.profileMenu);
     findViewById(R.id.bottomNavViewBar);
    

    最初,您在尝试查找此视图的引用时没有遇到任何错误,因为它们确实存在,但在另一个布局中,但不在您通过 Activity 中的 setContentView() 加载的 activity_profile.xml 中。

    【讨论】:

      【解决方案2】:

      这种类型的异常在错误的变量声明和 初始化。

      试试这个

      用 var 替换你的 weights 声明和初始化 val。

      如果一定要用java

      final TextView helloTextView = (TextView)findViewById(R.id.text_view_id);
      

      对于 Kotlin

      val helloTextView = findViewById(R.id.text_view_id) as TextView
      

      您可以参考this链接了解更多详情

      【讨论】:

        猜你喜欢
        • 2016-01-13
        • 2020-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-01
        • 2020-08-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多