Unfortunately app has stopped in Android - android

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().

Related

setOnClickListener causes crash

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);
}

ViewGroup.onRestoreInstanceState() not called when inflated programatically

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.

findViewById() returns null

There are a few questions already asking about this, but I'm not finding a solution. I have a LiveWallpaper that is using its own subclass of PreferenceFragment to specify preferences. The solution most often cited is to assure that setContentView() is called before findViewByID(). I am not calling setContentView() at all because I do not have a layout specified. This app originally implemented the preferences using the deprecated methods like PreferenceActivity.getPreferenceManager() and without using a layout and worked just fine. I am trying to bring the code up-to-date in using PreferenceFragment.
Am I required to have a layout and if so, what would I have when I don't really want one?
Or is there a another way to get/set the View?
public class SetPreferenceActivity extends Activity {
private CheckBox redCheckBox;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main); ???
redCheckBox = (CheckBox)findViewById(R.id.redCheckBox); // returns null
getFragmentManager().beginTransaction().replace(android.R.id.content, new LiveWallpaperPreferenceFragment()).commit();
}
}
You can create views dynamically :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
textView.setText("Hello");
setContentView(textView);//<----set the view !
}
}

onClickListener in PreferenceActivity

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

Switch between 2 layouts in Android

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);
}

Categories

Resources