2017-01-08 13 views
0

我建立一個應用程序中,每當我訪問諸如 Button btn1= (Button)findViewById(R.id._id); 當我把這段代碼我的項目中和 運行我的項目一個按鈕,它崩潰我的應用程序時,但當我/ /評論這一行,然後運行一切正常。我不知道有什麼問題!下面 是我的XML代碼:的Android應用程序崩潰訪問按鈕

<WebView 
    android:layout_width="match_parent" 
    android:layout_height="430dp" 
    android:id="@+id/WebField" 
    android:layout_above="@+id/btnFB" /> 
<ImageButton 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:id="@+id/btnNYT" /> 

我的Java代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button buttonNYT = (Button) findViewById(R.id.btnNYT); // this line has 
    WebView myWebView = (WebView)findViewById(R.id.WebField); 
+1

使用'ImageButton'而不是'Button' –

+0

另外,請發佈例外。你可以在Android logcat中看到它 – torkleyy

回答

0

使用的ImageButton而不是按鈕如。

ImageButton buttonNYT = (ImageButton) findViewById(R.id.btnNYT); 
2

你必須使用的ImageButton。 ImageButton與Button沒有關係,你不能施放它。

ImageButton buttonNYT = (ImageButton) findViewById(R.id.btnNYT); // this line has 

感謝 斯利拉姆