2017-12-03 79 views
1

我正在Android Studio 3.0中編寫一個國際象棋應用程序,當用戶點擊棋盤佈局上的按鈕btnPopUpWindow時,我想要一個PopupWindow出現。這裏」棋盤佈局XML:爲什麼findViewById()會崩潰relativeLayout init命令?

activity_chessboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidchess.MainActivity" 
    android:id="@+id/chessboardlayout"> 

    <Button 
     android:id="@+id/popUpButton" 
     android:layout_width="172dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Pop Up Menu" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginRight="8dp" /> 

</android.support.constraint.ConstraintLayout> 

的XML佈局上述由Chessboard.java加載。顯然,棋盤將是彈出窗口的父項,這就是爲什麼我想使用RelativeLayout引用來彈出窗口來查看棋盤。

Chessboard.java

public class Chessboard extends Activity { 

Button btnPopUpWindow; 
RelativeLayout relativeLayout; 

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

    btnPopUpWindow = findViewById(R.id.popUpButton);      // This works 
    relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout); // <--- CRASHES HERE!!! 
    btnPopUpWindow.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      // https://www.youtube.com/watch?v=wxqgtEewdfo 

      gameTextArea.setText("Pop up Menu appears..."); 

     } 
    }); 
} 

及其該relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout);線,其崩潰的應用程序。

我假設findViewById()未能找到ID chessboardlayout,但我不明白爲什麼。我在Android Studio中找不到R.id目錄,但我注意到,當我輸入命令時,chessboardlayout出現在下拉選項中。另外,當我通過調試器時,我會看到Id。所以我認爲Id已經正確提交。

但是,當我按照調試器,findViewById()調用返回null,這不可能是好的。特別是,無法啓動活動performLaunchActivity()ActivityThread.java拋出異常不知道這是怎麼回事。

我已經通過其他findViewById()線程在StackOverview搜索彈出,很多這些問題看似乎圍繞着由findViewById()不匹配的局部變量Button x = findViewById(R.id.TextFieldHere);例如返回的項目。但是,這似乎並不是這種情況...除非我不明白RelativeLayout應該如何在這裏工作......?任何意見/見解表示讚賞,謝謝。

回答

3

您的R.id.chessboardlayoutandroid.support.constraint.ConstraintLayout,因此您不能將它投射到RelativeLayout或它會導致ClassCastException

所以,如果你仍然想使用的RelativeLayout作爲佈局的根,改變你的activity_chessboard.xml這樣:

activity_chessboard.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="me.androidchess.MainActivity" 
    android:id="@+id/chessboardlayout"> 

    <Button 
     android:id="@+id/popUpButton" 
     android:layout_width="172dp" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginTop="8dp" 
     android:text="Pop Up Menu" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     android:layout_marginRight="8dp" /> 
</RelativeLayout> 

然後在你的活動使用findViewById()正常。

setContentView(R.layout.activity_chessboard); 
btnPopUpWindow = findViewById(R.id.popUpButton);      
relativeLayout = (RelativeLayout)findViewById(R.id.chessboardlayout);