can anyone please tell me what's wrong with this:
package applicationTest.ppr.com;
import android开发者_StackOverflow.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainClass extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Merdas para teste");
setContentView(R.layout.main);
}
}
my main.xml and android manifest are good. the app crashes when i added the textview stuff, and altered the default text specified in my main.xml. If I remove this, the app works.. but I can't figure out why...
Thanks a bunch!
you should before get any view , set the Content of your activity so try this :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.text);
tv.setText("Merdas para teste");
}
You need to call setContentView
before calling findViewById
.
SetContentView
is what builds the views and attaches it to the window. Since you have the calls reversed, findViewById
is probably returning null, causing the setText
call to throw an NPE.
精彩评论