簡體   English   中英

嘗試查找ViewById時拋出NullpointerException

[英]Nullpointerexception thrown when trying to findViewById

我有以下活動:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new StartFragment())
                .commit();
    }

    Button login = (Button) findViewById(R.id.loginButton);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MainActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    });

}

當我嘗試為R.id.loginButton調用findViewByID時,我得到了NPE,我猜這是因為loginButton在單獨的Fragment中,我具有:

public static class StartFragment extends Fragment {

    public StartFragment() {
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_main, container, false);

    }
}

但是,我不確定如何解決此問題,以便可以找到loginButton ID。 我以前沒有使用過片段,所以我意識到我可能正在使用它們/錯誤地實現了它們。 fragment_mainLinearLayout包含幾個按鈕,而activity_main除了一個FrameLayout什么也沒有。

嘗試在Fragment實現onCreateView(...)

@Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
 Bundle savedInstanceState) {
 View rootView = inflater.inflate(R.layout.fragment_main, container,
  false);

 View something = rootView.findViewById(R.id.something);
 something.setOnClickListener(new View.OnClickListener() { ... });

return rootView;
}

Button在片段布局( fragment_main.xml )中,不在活動布局( activity_main.xml )中。 onCreate()在生命周期中為時過早,無法在活動視圖層次結構中找到它,並返回null null上調用方法會導致NPE。

從片段編寫代碼以初始化按鈕,因為您的按鈕進入片段布局而不是活動布局。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container,
            false);
    Button login = (Button) rootView.findViewById(R.id.loginButton);
    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Intent intent = new Intent(MainActivity.this,
                    LoginActivity.class);
            startActivity(intent);
        }
    });

    return rootView;
}

並從Activity onCreate中刪除與登錄按鈕相關的代碼。

findViewById()可以參考根視圖。 如果不首先查看,將拋出空指針異常

在任何活動中,您都可以通過調用setContentView(someView);設置視圖setContentView(someView); 因此,當您調用findViewById() ,其引用了someView 同樣,findViewById()僅在該someView找到ID。 所以在這種情況下,空指針異常

對於片段,適配器,活動...。任何視圖的findViewById()僅會查找id是否在視圖中消失,或者,如果您對視圖進行膨脹,則還可以使用inflatedView.findViewById()從該視圖中獲取視圖。 inflatedView

簡而言之,請確保您在要引用的布局中包含id ,或者在適當的位置調用findViewById() (例如adapters getView() ,活動的onCreate()onResume()onPause() ,片段onCreateView() , ....)

也有關於UI和后台線程的想法,因為您不能有效地更新bg-threads中的UI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM