Main Activity.
package com.yuyu.zujian;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
public Button register;
public ToggleButton marriged;
public RadioButton male;
public EditText username, password;
public Spinner position;
public CheckBox reading, swimming;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager (). beginTransaction ()
. Add (R.id.container, new PlaceholderFragment ())
. Commit ();
}
username = (EditText) findViewById (R.id.username01);
password = (EditText) findViewById (R.id.password);
male = (RadioButton) findViewById (R.id.male);
reading = (CheckBox) findViewById (R.id.reading);
swimming = (CheckBox) findViewById (R.id.swimming);
marriged = (ToggleButton) findViewById (R.id.marriged);
position = (Spinner) findViewById (R.id.position);
String [] str = {"CEO", "CFO", "PM"};
ArrayAdapteraa = new ArrayAdapter (this, android.R.layout.simple_spinner_item, str);
position.setAdapter (aa);
register = (Button) findViewById (R.id.register);
register.setOnClickListener (new OnClickListener () {
public void onClick (View v) {
Bundle b = new Bundle ();
b.putString ("username", "User Name:" + username.toString ());
b.putString ("password", "password:" + password.toString ());
if (male.isChecked ()) {
b.putString ("gender", "Gender: Male");
} Else {
b.putString ("gender", "Gender: Female");
}
String temp = "hobby";
if (reading.isChecked ()) {
temp + = "read";
}
if (swimming.isChecked ()) {
temp + = "swim";
}
b.putString ("hobby", temp);
if (marriged.isChecked ()) {
b.putString ("marriged", "Marital Status: Married");
} Else {
b.putString ("marriged", "Marital Status: Single");
}
b.putString ("position", "position:" + position.getSelectedItem () toString ().);
Intent intent = new Intent (MainActivity.this, ResultActivity.class);
intent.putExtra ("data", b);
startActivity (intent);
}
});
}
}
Corresponding xml:
xmlns: tools = "http://schemas.android.com/tools"
android: id = "@ + id/LinearLayout1"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin">android: id = "@ + id/TableLayout01"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: collapseColumns = "3"
android: stretchColumns = "1"
tools: ignore = "UselessParent">android: id = "@ + id/tableRow1"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / username"
android: id = "@ + id/TextView01"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: labelFor = "@ + id/username01"
android: inputType = "text"
android: text = ""
android: id = "@ + id/username01"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
android: id = "@ + id/tableRow2"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / password"
android: id = "@ + id/TextView02"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: labelFor = "@ + id / password"
android: text = ""
android: id = "@ + id / password"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: inputType = "textPassword" />
android: id = "@ + id/tableRow3"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / sex"
android: id = "@ + id/TextView03"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: id = "@ + id / gender_g"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / man"
android: id = "@ + id / male"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: text = "@ string / woman"
android: id = "@ + id / female"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
android: id = "@ + id/tableRow4"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / marry"
android: id = "@ + id/TextView04"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: text = ""
android: id = "@ + id / marriged"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
android: id = "@ + id/tableRow5"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / hobby"
android: id = "@ + id / Thobby"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: text = "@ string / reading"
android: id = "@ + id / reading"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: text = "@ string / swimming"
android: id = "@ + id / swimming"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
android: id = "@ + id/tableRow6"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">android: text = "@ string / position"
android: id = "@ + id/TextView05"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: id = "@ + id / position"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
android: id = "@ + id/tableRow7"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content">
android: text = "@ string / cancel"
android: id = "@ + id / cancel"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
style = "android:? attr / borderlessButtonStyle" />
android: text = "@ string / register"
android: id = "@ + id / register"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
style = "android:? attr / borderlessButtonStyle" />
There are two problems:
1, in position.setAdapter (aa); Department certainly wrong, debug it, found all findViewById stuff is null. The stuff is defined in the main.xml.
2, register.setOnClickListener (new OnClickListener () {***} This function comes in on an error, try other projects, it seems as long as the call setOnClickListener go wrong, unreasonable ah, depressed.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
What are you SDK 4.4 is a fragment of the right layout layout, so you have to do findview
in PlaceholderFragment inside of onviewcreateOr delete the following passage, not to commit fragment
if (savedInstanceState == null) {
getFragmentManager (). beginTransaction ()
. Add (R.id.container, new PlaceholderFragment ())
. Commit ();
}
Reply:
If you do not commit fragment layout file inside the things we should party to setContentView (R.layout.activity_main);
This layout file is loaded inside oh.
Reply:
Is there a function in mainActivity PlaceholderFragment, and too long, did not stick up before: as
@ 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;
}
@ Override
public boolean onOptionsItemSelected (MenuItem item) {
/ / Handle action bar item clicks here. The action bar will
/ / Automatically handle clicks on the Home / Up button, so long
/ / As you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId ();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected (item);
}
/ **
* A placeholder fragment containing a simple view.
* /
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment () {
}
@ Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate (R.layout.fragment_main, container, false);
return rootView;
}
}
Reply:
Follow the prompts two, delete the fragment_main files, all definitions are placed in the main document, you can.
4.4 SDK will also have two layout appears, is currently not clear relationship between the two can only delete one :) Thank you for another operation of the
Reply:
fragment understanding is a component activity are recommended to achieve the 4.0 fragment later, so you can fit pad, you can go to the specific details read the official document
No comments:
Post a Comment