0

I am realitavely new to Java coding and am trying to use a button press (in an Android App) to update a text view. The code I am currently using is causing my app to crash whenever I try to press the button.

Here is my 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 is my 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 for any help you can give.

  • 1
    What kind of error it gives to you? – Francisco Romero Jun 04 '15 at 00:40
  • Please take a look at this, http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this and show us your error – almightyGOSU Jun 04 '15 at 01:14
  • i had tested with your code it work fine. did you set android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" to your linear layout? – NaiveBz Jun 04 '15 at 03:05

1 Answers1

-2

The code itself works fine so the problem must be with your XML, assuming you have provided it in full here.

Using this XML, your MainActivity code works fine for 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>
Nick Schroeder
  • 1,340
  • 1
  • 13
  • 17
  • the exception is only on clicking the button ,so I guess the layout is working fine – rahul.ramanujam Jun 04 '15 at 01:30
  • Did you actually try the XML I posted? Since you aren't providing any error details, I can't say for sure, but your exception might be raised because your LinearLayout is missing required fields like height, width, etc... – Nick Schroeder Jun 04 '15 at 01:34