user7390943
user7390943

Reputation: 3

Android app Crashes when accessing a button

I am building an App in which, whenever i access a button like Button btn1= (Button)findViewById(R.id._id); When i am putting this piece of code inside my project and run my project, it crashes my app, But when i //comment this line and then run everything fine. I don't know what's the problem! below is My xml code:

<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" />

My Java Code:

@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);

Upvotes: 0

Views: 49

Answers (2)

M Ayaz Khan
M Ayaz Khan

Reputation: 74

Use ImageButton Instead of Button eg.

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

Upvotes: 0

Sri
Sri

Reputation: 1336

You have to use ImageButton. ImageButton is not related to Button and you cannot cast it.

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

Thanks Sriram

Upvotes: 2

Related Questions