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!