-1

I have made an android app named Yash on AIDE. It has an edittext,textview & a button. It is designed to first take input through the edittext then when we press the button, it should display the edittext text on the textview. It starts & takes the input but as I press the button, it hangs & shows:"Unfortunately Yash has stopped."

Xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />

</EditText>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    android:id="@+id/oButton"/>
    />
<Button android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:text="Go!"
    android:onClick="bc"
    android:id="@+id/goButton"></Button>
</LinearLayout>

Java:

package com.mycompany.myapp4;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.Toast;
import android.widget.EditText;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;

 public class MainActivity extends Activity
{

  /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}


public void bc(View view){
    EditText t = (EditText) findViewById(R.id.editText);
    String s = t.getText().toString();

    TextView textView = (TextView) findViewById(R.id.oButton);
    textView.setText(s);

}
    }
Yash
  • 1
  • 1
  • 5
    Look for errors in LogCat – Nyavro Nov 09 '15 at 09:09
  • You must have received a crashlog entrie shownig where exactly the exception was thrown. – Hermann Klecker Nov 09 '15 at 09:34
  • @Herrmann Klecker - I would not be surprised if the next error is a NumberFormat Exception. You always should check the user input to be what you expect. But in fact there are two mistakes here: the missing getText() and the missing input check – Bö macht Blau Nov 09 '15 at 09:37
  • Me neither. But guessing is not quite an analytic approach. – Hermann Klecker Nov 09 '15 at 09:38
  • 1
    Guessing is what I can do as long as I see no logcat :). So, @Yash, would you please post the Logcat? – Bö macht Blau Nov 09 '15 at 09:40
  • 1
    Yash, you've just edited your question in a way which makes my answer look totally out of context :( Which it was not as there really was a mistake in how you were trying to get at the user input. - Having said that, do you know where to find a detailed crash report? It's printed out to console and in Android development it's called LogCat. We really need that to help you – Bö macht Blau Nov 09 '15 at 09:51
  • Well, the good news is that your code (as at the time of this comment) runs without problems in an Android Studio emulator. The bad news is that with AIDE it seems to be difficult to access the LogCat, maybe [this SO question-and-answers](http://stackoverflow.com/questions/21513591/viewing-logcat-in-tablet) can help you there. Then again, maybe you should download Android Studio, which is the officially supported IDE for Android. It takes some time getting used to it, but in the long run it's worth the effort. – Bö macht Blau Nov 09 '15 at 11:23

3 Answers3

1

The following line

String s = t.toString();

won't work like expected, because this way 's' is the String representation of the EditText object 't'. Most likely you meant to write

String s = t.getText().toString();
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • Therefore it would crash on the following line `int n = new Integer(s).intValue();`. However, letting him know where the error is does not teach him how to deal with exceptions. – Hermann Klecker Nov 09 '15 at 09:36
  • It crashes and shows:"Unfortunately Yash has stopped" I even if I remove the following line int n = new Integer(s).intValue(); – Yash Nov 09 '15 at 09:45
0

Make sure you have declared your activity in AndroidManifest.xml

Nyavro
  • 8,806
  • 2
  • 26
  • 33
0

try on this way.

public class MainActivity extends Activity {
    EditText t;
    TextView textView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initilization();
    }

    public void initilization() {
        // TODO Auto-generated method stub
        t = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.oButton);

        String s = t.getText().toString();
        if (!TextUtils.isEmpty(s)) {
            textView.setText(s);
        }else {
            // check log its null
        }
    }
}
Hardik Parmar
  • 712
  • 2
  • 13
  • 28