Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My App works and appears on the screen in the emulator. I get 2 numbers for num1 and num2 and use the 'Add' button and show the answer in 'answer'. I am able to enter a number in num1 but unable to enter for num2 as I could not place the cursor on num2.

If I click the 'Add' button it throws an error stating 'unfortunately app stopped working'. In the design mode I have checked the 'clickable' property and in 'onClick' I have also selected the 'onButtonClick'.

Java
public class MainActivity extends Activity {

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

    }
     public void onButtonClick(View v)
     {
         int n1,n2,ans=0;
         EditText e1=(EditText) findViewById(R.id.num1);
         EditText e2=(EditText) findViewById(R.id.num2);
        TextView view_answer=(TextView) findViewById(R.id.answer);
         n1=Integer.parseInt(e1.getText().toString());
         n2=Integer.parseInt(e2.getText().toString());
        ans=n1+n2;
        view_answer.setText(Integer.toString (ans));
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }


}


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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add "
        android:id="@+id/add"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:clickable="true"
        android:nestedScrollingEnabled="false"
        android:/>

    <textview>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"

        android:id="@+id/answer"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:width="123dp"
        android:removed="#ffcbffff" />

    <edittext>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/num1"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/add"
        android:layout_alignEnd="@+id/add"
        android:layout_marginTop="45dp"
        android:width="65dp"
        android:removed="#fffeffa0" />

    <edittext>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/num2"
        android:width="65dp"
        android:removed="#ffffa6f0"
        android:layout_below="@+id/num1"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:textIsSelectable="true"
        android:height="45dp"
        android:layout_marginTop="88dp" />
</edittext></edittext></textview></relativelayout>


<resources>

    <string name="app_name">My Application</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>

    <string name="num1"></string>
    <string name="num2"></string>
    <string name="answer"></string>
    <string name="add">Add</string>

</resources>
Posted
Updated 24-Jan-15 0:42am
v2
Comments
Richard MacCutchan 24-Jan-15 6:43am    
Your resource XML is badly formed, you have closing angle-brackets where they should not be.
Mohibur Rashid 24-Jan-15 9:15am    
how about the logcat. Post the log cat output

1 solution

"Unfortunately", the activity_main.xml is in a mess, among other things, there are improperly formed tags, invalid attributes, and wrong letter case. Change to this:

XML
<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"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:/>
    <TextView
        android:id="@+id/answer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="90dp"
        android:width="123dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    <EditText
        android:id="@+id/num1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/add"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/add"
        android:layout_marginTop="45dp"
        android:width="65dp" />
    <EditText
        android:id="@+id/num2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/num1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:width="65dp"
        android:height="45dp"
        android:textSize="20dp" />
</RelativeLayout>

The reason for not being able to edit "num2" edittext is the inclusion of
android:textIsSelectable="true"

Refer: Android UI Layouts and Controls
That should solve your problems.
It is highly recommended that you re-learn android development from these 12 winning articles at http://www.codeproject.com/Competitions/757/Android-Tutorial-Contest.aspx.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900