0

I am putting three files 1.activity_main.xml ,2.MainActivity.java ,3.strings.xml files ....I am a new app developer having many issues with doing simple stuff with android apps

1.activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="faizsols.com.test.MainActivity">


<TextView
    android:text="@string/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="134dp"
    android:layout_marginTop="106dp"
    android:id="@+id/text1"
    android:textSize="30sp" />

<Button
    android:text="@string/but1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/text1"
    android:layout_alignStart="@+id/text1"
    android:layout_marginTop="99dp"
    android:onClick="rep"
    android:id="@+id/but1" />
</RelativeLayout>

2.MainActivity.java

    package faizsols.com.test;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Button;


    public class MainActivity extends AppCompatActivity {

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

TextView t1 = (TextView)findViewById(R.string.text1);
Button rpl = (Button)findViewById(R.string.but1);


public void rep(View view) {
    t1.setText("I am fine");
}

}

3.strings.xml

    <resources>
<string name="app_name">Test</string>
<string name="text1">hello</string>
<string name="but1">Reply</string>

</resources>
Faizaan Gagan
  • 722
  • 1
  • 8
  • 20
  • use this `TextView t1 = (TextView)findViewById(R.id.text1);` inside onCreate – GVillani82 Jan 21 '17 at 07:01
  • 1
    That code won't crash at the `setText()` call. It'll crash at the first `findViewById()` call. Are you sure you've got the details right? – Mike M. Jan 21 '17 at 07:02
  • It might be a silly question but I've spent weeks in disappointment.. – Faizaan Gagan Jan 21 '17 at 07:08
  • Weeks on this code? Just think about this: how does `findViewById` work if no view exists? There is no view until you call `setContentView` in `onCreate` – OneCricketeer Jan 21 '17 at 07:10
  • That code, as you have it posted, will crash from the first `findViewById()` call. It's nothing to do with your strings.xml, really, though those aren't valid `View` IDs. You should learn to find and interpret the stack trace. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Mike M. Jan 21 '17 at 07:16

7 Answers7

4

Use this:

package faizsols.com.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    private TextView t1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // You should initialize in onCreate()
        t1 = (TextView)findViewById(R.id.text1);
        Button rpl = (Button)findViewById(R.id.but1);
    }

    public void rep(View view) {
        t1.setText("I am fine");
    }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Aman Shekhar
  • 2,719
  • 1
  • 18
  • 29
1

Put these lines inside your onCreate below setContentView

TextView t1;
Button rpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1 = (TextView)findViewById(R.id.text1);
    rpl = (Button)findViewById(R.id.but1);
}
Suresh Kumar
  • 2,014
  • 3
  • 19
  • 32
1
Changes in MainActivity.class


         package faizsols.com.test;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.TextView;
        import android.widget.Button;


        public class MainActivity extends AppCompatActivity {

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

              TextView t1 = (TextView)findViewById(R.id.text1);
              Button rpl = (Button)findViewById(R.id.but1);


              rpl .setOnClickListener(new View.OnClickListener() {
                 @Override
                  public void onClick(View v) {
                      t1.setText("I am fine");
                   }
             });   


    }
  • Thanks a lot..Now it doesn't crash..But another one..It crashes after clicking the button – Faizaan Gagan Jan 21 '17 at 07:32
  • my repo isn't 15 yet .. :( – Faizaan Gagan Jan 21 '17 at 07:50
  • Mr. Hiren...You've downvoted my answer because I didn't select yours as the accepted one. The reason was that you declared the text view and button inside the onCreate method so I found the currently selected answer as a more correct alternative... – Faizaan Gagan Feb 27 '17 at 11:09
0

update your MainActivity with this code:

   package faizsols.com.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;

   public class MainActivity extends AppCompatActivity {
TextView t1;
Button rpl;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 t1 = (TextView)findViewById(R.id.text1);
rpl = (Button)findViewById(R.id.but1);
}

public void rep(View view) {
    t1.setText("I am fine");
}
}
Ankush Bist
  • 1,862
  • 1
  • 15
  • 32
0

you have to fetch the id in oncreate() method after the setConteview method called.

     package faizsols.com.test;

        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.TextView;
        import android.widget.Button;


        public class MainActivity extends AppCompatActivity {

 TextView t1;
  Button rpl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    t1 = (TextView)findViewById(R.id.text1);
  rpl = (Button)findViewById(R.id.but1);
    }




    public void rep(View view) {
        t1.setText("I am fine");
    }
0

See those lines:

TextView t1 = (TextView)findViewById(R.string.text1);
Button rpl = (Button)findViewById(R.string.but1);

It's wrong. You need to write R.id, not R.string.

TextView t1 = (TextView)findViewById(R.id.text1);
Button rpl = (Button)findViewById(R.id.but1);

Use this:

package faizsols.com.test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
    TextView t1 = (TextView)findViewById(R.id.text1);
    Button rpl = (Button)findViewById(R.id.but1);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void rep(View view) {
        t1.setText("I am fine");
    }
}
ra1ned
  • 337
  • 5
  • 19
0

I know the question is solved as of now, but I would like to add a problem that you might be facing with the recent version of Android Studio. It seems impossible to set text with TextView. As an alternative, I have been using EditText as it is easy to use and can be called and defined in any function (not only OnCreate).

Edit text can be found in the palette option under 'text' under the design layout and its use is very similar to TextView

 EditText txt =(EditText) findViewById(R.id.editText);
 txt.setText("This actually works");
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Welcome to Stack Overflow. What's the problem with setting text in a TextView? That's one of the most basic android functions and I don't remember ever being a problem. Also, the EditText allows the user to add text in the view so using that instead of a TexView is not always the desired solution. – Nikos Hidalgo Apr 06 '20 at 08:40