android - XML / Java Button Click to change TextView -


i realitavely new java coding , trying use button press (in android app) update text view. code using causing app crash whenever try press button.

here xml

<linearlayout> <textview         android:id="@+id/mytextview_text_view"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="0" />  <button         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:onclick="calculatenewnumber"         android:text="calculate new number" /> </linearlayout> 

and here java

package com.example.android.myapp;  import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.textview;  public class mainactivity extends appcompatactivity {  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main); }  int startnumber = 0;  public void calculatenewnumber(view view) {     startnumber = startnumber + 1;     display(startnumber); }  private void display(int number) {     textview mytextview = (textview) findviewbyid(             r.id.mytextview_text_view);     mytextview.settext("" + number); } } 

thanks can give.

the code works fine problem must xml, assuming have provided in full here.

using xml, mainactivity code works fine me.

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"     android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity">      <linearlayout         android:layout_height="wrap_content"         android:layout_width="fill_parent">         <textview             android:id="@+id/mytextview_text_view"             android:layout_width="60dp"             android:layout_height="wrap_content"             android:text="0" />          <button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:onclick="calculatenewnumber"             android:text="calculate new number" />     </linearlayout>  </relativelayout> 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -