Button start_game;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
start_game = (Button) findViewById(R.id.start_game);
start_game.setOnClickListener(this);
setContentView(R.layout.welcome);
}
I don't know why, but if I remove the line setOnClickListener my app starts (of course my button doesn't do anything then).
Logcat gives me this:
java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.testabc/de.test.testabc.Welcome}: java.lang.NullPointerException
You have to inflate your layout before getting the UI elements, otherwise findViewById returns null and hence you got the NullPointerException at the line start_game.setOnClickListener(this);.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome); //layout inflated now it's ok to get your button
start_game = (Button) findViewById(R.id.start_game);
start_game.setOnClickListener(this);
}
Related
Im trying to make the contentView change on a button click, but the app crash when i start it by some reason.
This is my onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b1 = (Button) findViewById(R.id.bStart);
b1.setOnClickListener((OnClickListener) mHandler);
mHandler = new View.OnClickListener() {
public void onClick(View v) {
switch(v.getId()) {
case R.id.bStart:
// it was the first button
setContentView(R.layout.cards);
break;
}
}
};}
Your approach is wrong. setContentView() method should be called only once in onCreate() method and it shouldn't be invoked by clicking the button. In addtion, findViewById() methods should be called after calling setContentView() method. If you want to replace your layout, consider using fragments or do some manipulation on the views inside the layout without calling setContentView() method again.
I am new in Android.I get "Unfortunately app has stopped" when i run following code.
public class MainActivity extends Activity {
Button btn;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn.setOnClickListener(onClickList);
}
private OnClickListener onClickList= new OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(edit.getText());
}
};
Wrong:
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
Issue:
You are trying to find views before setting layout to Activity. So call setContentView() first and then you can find whichever views you want.
Correct:
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click);
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
Currently you are accessing views from current Activity before setting layout for Activity .Call setContentView before accessing views from xml as:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_click); // set layout here
btn=(Button)findViewById(R.id.button);
edit=(EditText)findViewById(R.id.text);
btn.setOnClickListener(onClickList);
}
You should call findViewById() after you call setContentView().
I've got a ViewGroup which will fire onRestoreInstanceState() if inflated by my activity
setContentView(). If I try to inflate it with getLayoutInflater().inflate(), then it won't.
I've tried to set an ID for it as said in Restoring view hierarchy from saved state does not restore views added programatically. I also have an ID in the layout file, and getID() returns the same every activity recreation, yet it won't restore nor save the instance state.
How can I overcome this?
(Edit) Code I'm using at the moment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.main); //this code will work as intended
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
View content = getLayoutInflater().inflate(R.layout.main, null);
setContentView(content); //this code will also work
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.another_layout);
View content = getLayoutInflater().inflate(R.layout.main, null); //now this view won't fire the method
}
The point is that I don't want to use that view as my layout for the activity. I need to inflate it some other way.
I have this onClickListener in onCreate method of my PreferenceActivity, but it gives me error.
Here is the PrefereceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something.
}
});
}
it gives me this error:
unable to start activity componentinfo java.lang.nullpointerexception
any idea what am I doing wrong?
EDIT: My SettingsPreference opens Dialog that holds that "button2".
The Button is causing your NullPointerException because you haven't set a layout and therefore it is null. You shouldn't need to use buttons in a PreferenceActivity anyway.
There's a perfectly good example of using PreferenceActivity over at the Android developer site: http://developer.android.com/reference/android/preference/PreferenceActivity.html
I have two layouts, what is the best way to switch between them programatically (not using an xml file).
And how can I do it?
Basically there are two ways.
1.) you got one XML containing all components you want to use. The ones which are not available at the moment, should bis hidden. When the user should have the possibility to use them, just make them visible
2.) this is definitly the better solution because Android is concipated for this method.
You have 2 Activities and 2 layout XML files. When you want to display another layout, start the second Activity.
in your first Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(StackOverflowActivity.this, Login.class);
startActivityForResult(i, LOGIN_REQUEST);
}
});
}
your second Activity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}