-2

I'm writing an android app that needs to use setText() on a TextView, which should be easy, but every time, the app crashes right when it gets to the method. I have another app that I've coded exactly the same, as far as I can tell, (other than variable names of course) which works. My best guess is that it has something to do with references, but I've checked everything there that I know how to check.

Here's my XML layout code:

<TextView
    android:id="@+id/test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Yay!" />

And my Java Activity code:

package com.example.campgames;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    test=(android.widget.TextView)findViewById(R.id.test);
    lowerElem=(CheckBox)findViewById(R.id.lower_elem);

}

public void click(View view){
    test.setText(R.string.inside);
    setContentView(R.layout.activity_results);
}

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

}

Thanks for your help, and let me know if I'm doing anything wrong. First time poster!

Paul
  • 1
  • 1
    possible duplicate of [setText(getString(R.strings.whatever) or setText(R.strings.whatever)?](http://stackoverflow.com/questions/10120867/settextgetstringr-strings-whatever-or-settextr-strings-whatever) – Himanshu Agarwal Dec 30 '14 at 07:37
  • I dont think "test.setText(R.string.inside)" is the actual cause of crash. – A.R. Dec 30 '14 at 08:04
  • 1
    http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Dec 30 '14 at 08:09

5 Answers5

1

Try using

String textToSet = (String) yourContext.getResources().getText(R.string.inside); test.setText(textToSet);

Aown Raza
  • 2,023
  • 13
  • 29
1

Try This:

public void click(View view){
test.setText(getResources().getString(R.string.inside));

}

instead of

test.setText(R.string.inside);

Deepmala singh M
  • 379
  • 1
  • 16
0

Try this....

 public void click(View view){
        test.setText(getResources().getString(R.string.inside));
    }

Happy coding :-)

Akash Jindal
  • 505
  • 3
  • 7
0

Try This:

 test.setText(getResources().getString(R.String.mycar));

instead of

test.setText(R.string.inside);
Himanshu Agarwal
  • 4,623
  • 5
  • 35
  • 49
0

Remove this line from click() method.

setContentView(R.layout.activity_results);
inverse12
  • 186
  • 1
  • 7