asifa
asifa

Reputation: 771

Unfortunately the app has stopped working - android

This happens on button click

My Activity.XML file

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

<EditText android:id="@+id/enter_number"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/enter_number"
    android:layout_weight="1"
    />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_calculate" 
    android:onClick="calculate"
    />
</LinearLayout>

.java file

    package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TextView;

public class Tables extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the message from the intent
        int number=0;
        int i=1;
        int next=0;
        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
        number=Integer.parseInt(message);
        TextView textView = new TextView(this);

        String[] myString = new String[10]; //create array
        for (i = 1; i <= myString.length; i++) { 
             next= number + next;
             myString[i] =  String.valueOf(number)+'x'+String.valueOf(i)+'='+String.valueOf(next)+'\n';
            // textView.setText(myString[i]);  
            // setContentView(textView);
        }
        StringBuilder builder = new StringBuilder();
        for (String s : myString){
          builder.append(s+" ");
        textView.setText(builder.toString());
        }
        setContentView(textView);

    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_tables, menu);
        return true;
    }
}

I think the error is during displaying. But can't find the solution.

Thanks,

Upvotes: 0

Views: 1882

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234857

It looks like the layout file is for your main activity, not for the Tables activity. Make sure that your MainActivity class has a method with the signature public void calculate(View view) so the activity can respond to the button click. It's likely that this is the problem, rather than a problem with displaying the Tables activity.

Upvotes: 0

Arpit Patel
Arpit Patel

Reputation: 1571

You have to define a calculate method in your activty. As you have set the onClick property in the layout file, when you click the button, the Android Framework will try to call the follow method of Tables activity :

  public void calculate(View v){
      //put you code here this will be execute when the button is clicked
  }

Upvotes: 1

Related Questions