Can't set values in second activity - java

So I'm making a simple application that just takes some information from the user i.e Name, address etc and puts it in the the textviews in the next activity but when I put in the values and move to the next activity there's nothing displayed in the TextViews.
Here's my first activity
public class MainActivity extends Activity {
public String Name;
public String Age;
public String Address;
public String City;
public String phoneno;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText name = (EditText) findViewById(R.id.editText1);
Name = name.getText().toString();
EditText Amge = (EditText) findViewById(R.id.agee);
Age = Amge.getText().toString();
EditText Address2 = (EditText) findViewById(R.id.address);
Address = Address2.getText().toString();
EditText City2 = (EditText) findViewById(R.id.city);
City = City2.getText().toString();
EditText phone2 = (EditText) findViewById(R.id.phone);
phoneno = phone2.getText().toString();
final ImageView D = (ImageView) findViewById(R.id.done);
D.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
}
});
}
#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);}}
Here's my second activity
public class second extends MainActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView N1 = (TextView) findViewById(R.id.name1);
N1.setText(Name);
TextView A1 = (TextView) findViewById(R.id.age1);
A1.setText(Age);
TextView Ad1 = (TextView) findViewById(R.id.address1);
Ad1.setText(Address);
TextView C1 = (TextView) findViewById(R.id.city1);
C1.setText(City);
TextView P1 = (TextView) findViewById(R.id.phone1);
P1.setText(phoneno);}
public static void main(String[] args) {
// TODO Auto-generated method stub
}}
Here's my xml for the main activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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:orientation="horizontal"
android:background="#drawable/bg"
tools:context="com.example.randomtests.MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:textSize="21dp"
android:textColor="#ffffff"
android:text="Please enter the required info"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView1"
android:src="#drawable/bar" />
<EditText
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/agee"
android:layout_below="#+id/agee"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPostalAddress"
android:textColor="#000000" />
<EditText
android:id="#+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/address"
android:layout_below="#+id/address"
android:layout_marginTop="17dp"
android:ems="10"
android:hint="#string/city"
android:textColor="#000000" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:ems="10"
android:gravity="top"
android:textColor="#000000"
android:hint="#string/namu"
android:inputType="textPersonName" />
<EditText
android:id="#+id/agee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_below="#+id/editText1"
android:layout_marginTop="14dp"
android:ems="10"
android:hint="#string/age"
android:inputType="phone"
android:textColor="#000000" />
<EditText
android:id="#+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/city"
android:layout_below="#+id/city"
android:layout_marginTop="15dp"
android:ems="10"
android:hint="#string/phone"
android:inputType="phone"
android:textColor="#000000" />
<ImageView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/phone"
android:layout_marginRight="14dp"
android:layout_marginTop="12dp"
android:src="#drawable/button" />
Here's my xml for the second activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg" >
<TextView
android:id="#+id/info1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textColor="#ffffff"
android:textSize="33dp"
android:layout_centerHorizontal="true"
android:text="#string/yourinfo"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/name1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/info1"
android:textColor="#ffffff"
android:layout_marginLeft="19dp"
android:layout_marginTop="26dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/age1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/name1"
android:layout_below="#+id/name1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/address1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/age1"
android:layout_below="#+id/age1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/city1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/address1"
android:layout_below="#+id/address1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phone1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/city1"
android:layout_below="#+id/city1"
android:textColor="#ffffff"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceMedium" />

After you declare your Intent , try sending your data with this
//this use key-value
intent.putExtra("USER_NAME" , "your string");
intent.putExtra("USER_AGE" , "your string");
and etc.
and to other Activity try use
Bundle extra = getIntent().getExtras();
String userName = extra.getString("USER_NAME");
String userAge = extra.getString("USER_AGE");

When you declare your Intent here:
Intent intent = new Intent(MainActivity.this, second.class);
Try sending some data with it using Extras based on what view is being clicked on
For example:
intent.putExtra(Intent.EXTRA_INTENT, **Your String Here**);
And in the activity receiving the intent, use something like:
String data = intent.getStringExtra(Intent.EXTRA_INTENT);
To fetch the data from the intent

Related

Get value of a EditText in another layout

I have an dialog that appears where the user can costumize how many points they get from each action. The dialog is based on a layout that is not the main_activity. the main activity is the layout that is used in setContentView in the onCreate method.
when I try to get the values I am only getting null, (because the layout where the EditText is is not set as the contentview.... maybe?)
How do I solve this problem so that I get the value of the edittext in the "settings" layout and can use that value in the MainActivity?
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings();
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(){
EditText newBurnedPoints = findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}
SETTINGS LAYOUT:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp"
android:id="#+id/settings_layout"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_one"
style="#style/settings_headings"
/>
<TextView
android:id="#+id/name_two_settings"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/name_team_two"
style="#style/settings_headings"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<EditText
android:id="#+id/name_one_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_one"
android:maxLength="20"
android:singleLine="true"
/>
<EditText
android:id="#+id/name_two_edit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="#string/team_two"
android:maxLength="20"
android:singleLine="true"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/burned_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/burned_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="3"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/one_hand_lyre_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/one_hand_lyre_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/home_run_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/home_run_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="5"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rev_points"
android:paddingTop="16dp"
style="#style/settings_headings"/>
<EditText
android:id="#+id/rev_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="1"
android:maxLength="2"
android:inputType="number"
android:singleLine="true"/>
</LinearLayout>
MAIN LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"
>
<LinearLayout
android:id="#+id/team_names"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/name_one"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_one"
style="#style/Headings"
/>
<TextView
android:id="#+id/name_two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/team_two"
style="#style/Headings"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/team_points"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/points_one"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/field_team"/>
<ToggleButton
android:id="#+id/field_team_toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="#string/team_one"
android:textOn="#string/team_two"
style="#style/buttons"/>
</LinearLayout>
<TextView
android:id="#+id/points_two"
android:layout_width="0dp"
android:layout_height="88dp"
android:layout_weight="1"
android:autoSizeTextType="uniform"
android:text="0"
style="#style/TeamPoints"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/events"
android:paddingTop="16dp"
style="#style/Headings"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
>
<Button
android:id="#+id/burned_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="burned"
android:text="#string/burned"
style="#style/buttons"/>
<Button
android:id="#+id/lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="lyre"
android:text="#string/lyre"
style="#style/buttons"
/>
<Button
android:id="#+id/one_hand_lyre_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="oneHandLyre"
android:text="#string/oneHandLyre"
style="#style/buttons"
/>
<Button
android:id="#+id/homeRun_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="homeRun"
android:text="#string/homeRun"
style="#style/buttons"
/>
<Button
android:id="#+id/rev_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="rev"
android:text="#string/rev"
style="#style/buttons"
/>
</TableLayout>
<Button
android:id="#+id/reset_button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginTop="16dp"
android:onClick="reset"
android:text="#string/reset"
style="#style/buttons"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/points_edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:src="#android:drawable/ic_menu_edit"
android:layout_margin="16dp" />
</LinearLayout>
I would strongly recommend using SharedPreferences. This way, you can access the data even if you restart your application. If that does not work for you, you can use Intent.
Consider reading this StackOverflow post.
Using SharedPreferences:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.points), pointsPerActivity);
editor.commit();
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = 1;
// or you can store the value somewhere
// getResources().getInteger(R.integer.defaultPointsPerActivity);
int pointsPerActivity = sharedPref.getInt(pointsPerActivity, defaultValue);
pass view object in applySettings(v);
private int teamOnePoints=0;
private int teamTwoPoints=0;
private boolean teamOneField=true;
private int burnedPoints = 1;
private int lyrePoints= 3;
private int oneHandLyrePoints = 5;
private int homeRunPoints=5;
private int revPoints=1;
private String teamOneName = "Lag 1";
private String teamTwoName = "Lag 2";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton toggle = (ToggleButton)
findViewById(R.id.field_team_toggle); //activates toggle button
toggle.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
if (isChecked) {
//changes between the teams
teamOneField=false;
} else {
teamOneField=true;
}
}
});
FloatingActionButton fab = findViewById(R.id.points_edit_button);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openDialog();
}
});
}
public void openDialog() {
AlertDialog.Builder builder = new
AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = this.getLayoutInflater();
View v = inflater.inflate(R.layout.settings, null);
builder.setView(v);
builder.setTitle(getResources().getString(R.string.edit_rules));
builder.setPositiveButton(getResources().getString(R.string.ok_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
applySettings(v);
}
});
builder.setNegativeButton(getResources().getString(R.string.cancel_button),
null);
builder.show();
}
private void applySettings(View view){
EditText newBurnedPoints = view.findViewById(R.id.burned_edit);
burnedPoints=Integer.parseInt(newBurnedPoints.getText().toString());
EditText newLyrePoints = view.findViewById(R.id.lyre_edit);
lyrePoints=Integer.parseInt(newLyrePoints.getText().toString());
EditText newOneHandLyrePoints = view.findViewById(R.id.one_hand_lyre_edit);
oneHandLyrePoints=Integer.parseInt
(newOneHandLyrePoints.getText().toString());
EditText newHomeRunPoints = view.findViewById(R.id.home_run_edit);
homeRunPoints=Integer.parseInt(newHomeRunPoints.getText().toString());
EditText newRevPoints = view.findViewById(R.id.rev_edit);
revPoints=Integer.parseInt(newRevPoints.getText().toString());
EditText newTeamOneName = view.findViewById(R.id.name_one_edit);
teamOneName= newTeamOneName.getText().toString();
TextView setNewTeamOneName = (TextView)findViewById(R.id.name_one);
setNewTeamOneName.setText(teamOneName);
EditText newTeamTwoName =view.findViewById(R.id.name_two_edit);
teamTwoName= newTeamTwoName.getText().toString();
TextView setNewTeamTwoName = (TextView)findViewById(R.id.name_two);
setNewTeamTwoName.setText(teamTwoName);
}

How to pass users EditText input in a Textview in another activity

I have 2 textboxes with a users name and Id number in my main activity.
When clicking the edit button the user is sent to second activity where they can edit their name and Id number.
Then when they click Save, the edited input should override the written text in the two existing textviews in the first activity.
But when I click the Save button, my app crashes...
Furthermore, main activity has also a capture profile pic. feauture, but when in landscape mode, my captured image. disappear.
I'm totally new to Android and this is a school assignment with due on Sunday... Can someone please help me!!
This is my first activity's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.SecondActivity2"
android:background="#F0FFFF">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="8"
android:id="#+id/EditName"
android:layout_marginTop="42dp"
android:hint="Solveig Mortensen"
android:layout_marginLeft="160dp"
android:background="#87CEEB"
android:textSize="20dp"
android:textColor="#000000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditName"
android:text="#string/edit_name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameLabel" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="8"
android:id="#+id/EditID"
android:hint="123456"
android:layout_below="#+id/EditName"
android:layout_alignLeft="#+id/EditName"
android:layout_alignStart="#+id/EditName"
android:layout_marginTop="40dp"
android:background="#87CEEB"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#id/EditID"
android:text="#string/edit_id"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/IdLabel" />
<CheckBox
android:id="#+id/Yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Y"
android:onClick="onCheckboxClicked"
android:layout_marginTop="27dp"
android:layout_below="#+id/AndrStatus"
/>
<CheckBox android:id="#+id/NO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/N"
android:onClick="onCheckboxClicked"
android:layout_marginTop="30dp"
android:layout_below="#+id/Yes"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cancel"
android:id="#+id/buttonCancel"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000000"
android:background="#87CEEB" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/save_btn"
android:id="#+id/buttonSave"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#000000"
android:background="#87CEEB" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/android_developer"
android:id="#+id/AndrStatus"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:textSize="20dp"
android:textColor="#000000" />
</RelativeLayout>
AND second activity's xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.solveigdoan.cameraactivity.CameraActivity">
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#F0FFFF" />
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/profile_picture"
android:layout_centerHorizontal="true"
android:layout_marginLeft="40dp"
android:textColor="#000000" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="#string/take_photo"
android:layout_below="#+id/textview1"
android:background="#87CEEB"
android:layout_marginBottom="5dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:layout_below="#+id/button1"
android:background="#BCC6CC" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:ems="9"
android:id="#+id/Name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignLeft="#+id/ID"
android:layout_alignStart="#+id/ID"
android:layout_below="#+id/imageView1"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="Solveig Mortensen" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/Name"
android:text="#string/name"
android:textSize="20dp"
android:textColor="#000000"
android:id="#+id/NameTitle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="9"
android:id="#+id/ID"
android:layout_alignTop="#+id/Name"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="40dp"
android:textSize="20dp"
android:background="#87CEEB"
android:textColor="#000000"
android:hint="123456" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_alignBaseline="#id/ID"
android:text="#string/id_nr"
android:textSize="20dp"
android:textColor="#000000" />
<Button
android:id ="#+id/push_button"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="#string/edit_btn"
android:background="#drawable/button_bg_round"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textSize="12dp"
android:textColor="#000000" />
<TextView
android:id="#+id/tvView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:layout_below="#+id/ID"
android:layout_marginTop="20dp" />
</RelativeLayout>
My main java (named CameraActivity):
public class CameraActivity extends Activity {
Button BtnTakePhoto;
ImageView imgTakenPhoto;
Button push_button;
TextView Name;
TextView ID;
private static final int CAMERA_REQUEST = 1888;
private static final int EDIT_REQUEST = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
push_button = (Button) findViewById(R.id.push_button);
push_button.setOnClickListener(new push_buttonClicker());
imgTakenPhoto = (ImageView) findViewById(R.id.imageView1);
BtnTakePhoto = (Button) findViewById(R.id.button1);
BtnTakePhoto.setOnClickListener(new BtnTakePhotoClicker());
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imgTakenPhoto.setImageBitmap(thumbnail);
}
if (requestCode == EDIT_REQUEST) {
Bundle b = getIntent().getExtras();
Name = (TextView) findViewById(R.id.Name);
ID = (TextView) findViewById(R.id.ID);
Name.setText(b.getCharSequence("Name"));
ID.setText(b.getCharSequence("ID nr"));
}
}
class BtnTakePhotoClicker implements Button.OnClickListener
{
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}class push_buttonClicker implements Button.OnClickListener{
public void onClick(View v){
Intent Intent = new Intent(CameraActivity.this, SecondActivity2.class);
CameraActivity.this.startActivityForResult(Intent, EDIT_REQUEST);
}}}
AND my secondActivity.java:
public class SecondActivity2 extends AppCompatActivity {
TextView tvView;
EditText EditName;
EditText EditID;
Button buttonCancel;
Button buttonSave;
CheckBox checkBox;
TextView Name;
TextView ID;
private static final int RESULT_OK = 10;
//private static final int RESULT_CANCELED = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second2);
// CheckBox Yes = (CheckBox) findViewById(R.id.Yes);
// CheckBox NO = (CheckBox) findViewById(R.id.NO);
buttonSave = (Button) this.findViewById(R.id.buttonSave);
buttonCancel = (Button) findViewById(R.id.buttonCancel);
EditName = (EditText) findViewById(R.id.EditName);
EditID = (EditText) findViewById(R.id.EditID);
buttonSave.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();
data.putExtra("name", EditName.getText().toString());
data.putExtra("ID", EditID.getText().toString());
setResult(RESULT_OK, data);
finish();
}
});}}
change your code inside onActivityResult under EDIT_REQUEST condition
String name = data.getStringExtra("name");
String ID = data.getStringExtra("ID");
Name.setText(name);
ID.setText(ID);
no need to create a Bundle there, it will solve your problem.

Motion event error on ImageButton click

public class AddActivity extends Activity implements OnClickListener{
String[] info = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
ImageButton submit = (ImageButton)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
submit.setOnClickListener((OnClickListener) this);
add1.setOnClickListener((OnClickListener) this);
add2.setOnClickListener((OnClickListener) this);
add3.setOnClickListener((OnClickListener) this);
int id = v.getId();
switch(id){
case R.id.submit:{
submitEntry(info);
break;
}
case R.id.add1:{
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
break;
}
}
}
}
This is the code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/key_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<EditText
android:id="#+id/keyString"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:visibility="invisible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/site_string"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new" />
<EditText
android:id="#+id/site1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site2"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:textAppearance="?android:attr/textAppearanceLarge"
android:visibility="invisible" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/add3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_new"
android:visibility="invisible" />
<EditText
android:id="#+id/site3"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:text="#string/url_hint"
android:visibility="invisible"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/submit_buttom" />
</LinearLayout>
</ScrollView>
</LinearLayout>
And this is the XML. The add1, add2, add3 and submit ImageButtons are all in a ScrollView.
When I press the add1 ImageButton, I want the add2 and site2 ImageButtons to become visible but instead, it throws the following error.
Motion event has invalid pointer count 0; value must be between 1 and 16.
What am I doing wrong?
PS: All the findViewById() calls are in the onClick() method because a NullPointerExeption is thrown if I call them in the onCreate().
Those findViewByIdcalls in onClickdon't make sense. Not sure why you are getting a null pointer exception calling them in onCreate.onClick is never called in this instance because nothing in the creation of the Activity is assigning the buttons to look at your onClick method; the buttons will default to having no listener assigned. It also doesn't look like a good idea to use the Activity as the onClickListener as well.
Your code should look something like this:
public class AddActivity extends Activity {
// https://source.android.com/source/code-style.html
// info -> mInfo; non-public, non-static field!
String[] mInfo = new String[11];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_layout);
TextView keyString = (TextView)findViewById(R.id.keyString);
TextView site1 = (TextView)findViewById(R.id.site1);
TextView site2 = (TextView)findViewById(R.id.site2);
TextView site3 = (TextView)findViewById(R.id.site3);
Button submit = (Button)findViewById(R.id.submit);
ImageButton add1 = (ImageButton)findViewById(R.id.add1);
ImageButton add2 = (ImageButton)findViewById(R.id.add2);
ImageButton add3 = (ImageButton)findViewById(R.id.add3);
add1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
add2.setVisibility(View.VISIBLE);
site2.setVisibility(View.VISIBLE);
}
});
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
submitEntry(mInfo);
}
});
}
The findViewById() calls and especially the setOnClickListener() calls should have been inside the onCreate(). With setOnClickListener() inside the onClick() i doubt the onClick was ever called.
We would need more logs to find the exact issue.

NullPointer exception when using getText().toString().trim();

I have the following class:
public class SHConfigureContactFragment extends Fragment{
private EditText name;
private EditText description;
private EditText primaryNumber;
private EditText secondaryNumber;
private EditText email;
private EditText skype;
private Byte[] photo;
private Boolean isDualPane;
private FragmentManager fragmentManager;
private SHContactMenuFragment menuFragment;
private SHConfigureContactFragment contactFragment;
private DatabaseControllerLibrary controller;
private Contact contact;
private SHPatient patient;
private int patientId;
public View rootView;
public int selectedIndex;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(savedInstanceState != null) {
this.selectedIndex = savedInstanceState.getInt("SELECTED_INDEX");
this.contact = (Contact) savedInstanceState.getSerializable("CONTACT");
}
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable("CONTACT", contact);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
controller = SHController.getInstance(getActivity());
patient = ((SHController) controller).getPatient();
patientId = patient.getId();
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
rootView = inflater.inflate(R.layout.sh_fragment_contact_edit, container, false);
Button button = (Button) rootView.findViewById(R.id.addContactButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
boolean isValid = true;
// assigning IDs based on how many contacts are created. no reused IDs son!
int id = controller.getContactsCount();
id++;
String newName = name.getText().toString().trim();
String newDescription = description.getText().toString().trim();
String newPrimaryNumber = primaryNumber.getText().toString().trim();
String newSecondaryNumber = secondaryNumber.getText().toString().trim();
String newEmail = email.getText().toString().trim();
String newSkype = skype.getText().toString().trim();
if(newName.length()==0 || newName.matches(".*\\d.*")) {
name.setText("");
isValid = false;
}
if(newPrimaryNumber.length()==0) {
primaryNumber.setText("");
isValid = false;
}
if (isValid){
Contact updatedContact;
ContactConfiguration updatedConfiguration;
if (contact != null){
id = contact.getId();
}
updatedContact = new Contact(id, newName, newDescription, newPrimaryNumber, newSecondaryNumber, newEmail, newSkype, null);
updatedConfiguration = new ContactConfiguration(patientId, patientId, false, updatedContact);
if (contact == null){
controller.addContact(updatedContact, patientId);
}
else{
controller.updateContact(updatedContact, patientId);
}
}
}
});
return rootView;
}
private void setupTextFieldsByContact(Contact contact) {
name = (EditText) rootView.findViewById(R.id.editContactName);
description = (EditText) rootView.findViewById(R.id.editContactDescription);
primaryNumber = (EditText) rootView.findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) rootView.findViewById(R.id.editContactSecondaryNumber);
email = (EditText) rootView.findViewById(R.id.editContactEmail);
skype = (EditText) rootView.findViewById(R.id.editContactSkype);
if (contact != null) {
name.setText(contact.getName());
description.setText(contact.getDescription());
primaryNumber.setText(contact.getPrimaryNumber());
secondaryNumber.setText(contact.getSecondaryNumber());
email.setText(contact.getEmail());
skype.setText(contact.getSkype());
}
}
And I have the corresponding XML file for its layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/background"
android:orientation="vertical" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Name"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Name"
android:imeOptions="actionNext"
android:inputType="textPersonName"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactDescription"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Description"
android:imeOptions="actionNext"
android:inputType="textShortMessage"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/numbers"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="15dp"
android:text="Primary and Secondary Numbers"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<RelativeLayout
android:id="#+id/NumberFields"
android:layout_width="fill_parent"
android:layout_height="50dp" >
<EditText
android:id="#+id/editContactPrimaryNumber"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_marginLeft="35dp"
android:layout_weight="2"
android:background="#drawable/border"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="Primary"
android:inputType="phone"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<EditText
android:id="#+id/editContactSecondaryNumber"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="25dp"
android:layout_weight="1"
android:background="#drawable/border"
android:ems="10"
android:gravity="center_vertical|center_horizontal"
android:hint="Secondary"
android:inputType="phone"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
</RelativeLayout>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Email"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactEmail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Email"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<TextView
android:id="#+id/skype"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="15dp"
android:text="Skype ID"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/action_bar"
android:textSize="25sp" />
<EditText
android:id="#+id/editContactSkype"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/border"
android:ems="10"
android:hint="Skype ID"
android:imeOptions="actionNext"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:textSize="30sp" />
<Button
android:id="#+id/addContactButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/sh_button_selector"
android:padding="#dimen/padding_medium"
android:text="Save Contact"
android:textColor="#color/text_white"
android:textSize="#dimen/text_size_take_survey_button"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Now when I run my app I get a NullPointerException within the onCreateView() within the onClick at the line:
String newName = name.getText().toString().trim();
Now I have tried printing out the variables, debugging but I just cannot seem to figure out why it is getting a NullPointerException. It may be something simple that I'm overlooking, I've just been looking at it for so long I may just be going crazy.
I think your EditText 'name' is not initialized.
You have not initialized those EditText, so you are getting NPE. initialize those components first ..From your code call this method setupTextFieldsByContact() in onCreate it will solve your problem.
Of course you're getting a NullPointerException...you never initialized your EditTexts in the onCreate method. Add this to your onCreate:
name = (EditText) findViewById(R.id.editContactName);
description = (EditText) findViewById(R.id.editContactDescription);
primaryNumber = (EditText) findViewById(R.id.editContactPrimaryNumber);
secondaryNumber = (EditText) findViewById(R.id.editContactSecondaryNumber);
email = (EditText) findViewById(R.id.editContactEmail);
skype = (EditText) findViewById(R.id.editContactSkype);
That way you'll be calling methods on objects that actually exist.
your EditText 'name' is not initialized. you have to initialized your EditText first

Returning values from a custom dialog view

I'm trying to make a custom dialog for my android application. I get null pointer exceptions when I attempt to access the fields from the 'ok' button method. It seems like my fields fade from existence as soon as the onclick listener fires. Is there a better way to do this?
addchars.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/scrollBox">
<RelativeLayout
android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/addChars">
<AutoCompleteTextView android:layout_height="wrap_content" android:id="#+id/inCharName" android:hint="#string/txtCharacterNameBox" android:layout_below="#+id/textView1" android:layout_centerHorizontal="true" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</AutoCompleteTextView>
<TextView android:id="#+id/textView2" android:layout_height="wrap_content" android:gravity="center" android:layout_width="wrap_content" android:text="#string/txtHP" android:layout_below="#+id/inCharName" android:layout_alignParentLeft="true" android:layout_marginLeft="22dp"></TextView>
<TextView android:id="#+id/textView3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtTempHP" android:layout_below="#+id/inCharName" android:layout_alignParentRight="true" android:layout_marginRight="57dp"></TextView>
<EditText android:id="#+id/inHP" android:digits="-0123456789" android:minEms="3" android:gravity="center" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_below="#+id/textView2" android:layout_alignLeft="#+id/textView2">
</EditText>
<EditText android:id="#+id/inTempHP" android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="3" android:layout_width="wrap_content" android:layout_alignBaseline="#+id/inHP" android:layout_alignBottom="#+id/inHP" android:layout_alignLeft="#+id/textView3"></EditText>
<TextView android:id="#+id/TextView01" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inHP" android:layout_alignLeft="#+id/inHP" android:text="#string/txtInitRoll"></TextView>
<TextView android:id="#+id/TextView02" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inTempHP" android:layout_alignLeft="#+id/inTempHP" android:text="#string/txtInitBonus"></TextView>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView01" android:layout_alignLeft="#+id/TextView01" android:id="#+id/inInitRoll"></EditText>
<EditText android:digits="0123456789" android:minEms="3" android:layout_height="wrap_content" android:gravity="center" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView02" android:layout_alignLeft="#+id/TextView02" android:id="#+id/inInitBonus"></EditText>
<TextView android:id="#+id/TextView03" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="#string/txtAC" android:layout_below="#+id/inInitRoll" android:layout_alignRight="#+id/textView2"></TextView>
<EditText android:id="#+id/inAC" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView03" android:layout_alignLeft="#+id/TextView03"></EditText>
<TextView android:id="#+id/TextView04" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_above="#+id/inAC" android:layout_alignLeft="#+id/inInitBonus" android:text="#string/txtFortitude"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView04" android:layout_alignLeft="#+id/TextView04" android:id="#+id/inFort"></EditText>
<TextView android:id="#+id/TextView06" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inFort" android:layout_alignLeft="#+id/inFort" android:text="#string/txtWill"></TextView>
<TextView android:id="#+id/TextView05" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="#+id/inAC" android:layout_alignLeft="#+id/inAC" android:text="#string/txtRef"></TextView>
<EditText android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_below="#+id/TextView05" android:layout_alignLeft="#+id/inInitRoll" android:id="#+id/inReflex"></EditText>
<EditText android:id="#+id/inWill" android:digits="0123456789" android:gravity="center" android:minEms="3" android:layout_height="wrap_content" android:inputType="phone" android:maxLength="2" android:layout_width="wrap_content" android:layout_above="#+id/chkRoll" android:layout_alignLeft="#+id/TextView06"></EditText>
<CheckBox android:text="#string/txtRollBox" android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="#+id/chkRoll" android:layout_below="#+id/inReflex" android:layout_alignLeft="#+id/TextView05" android:layout_alignParentRight="true"></CheckBox>
</RelativeLayout>
</ScrollView>
And this is my alertdialog:
addBtn.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
//this block is to prep the layout for the alert dialog.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = inflater.inflate(R.layout.addchars, (ViewGroup) findViewById(R.id.scrollBox));
//taking this part out to try a better way.
new AlertDialog.Builder(initiative.this)
.setTitle("Add a Character")
.setView(addView)
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//ui controls
AutoCompleteTextView characterName = (AutoCompleteTextView) findViewById(R.id.inCharName);
EditText hp = (EditText) findViewById(R.id.inHP);
EditText tempHp = (EditText) findViewById(R.id.inTempHP);
EditText initRoll = (EditText) findViewById(R.id.inInitRoll);
EditText initBonus = (EditText) findViewById(R.id.inInitBonus);
EditText ac = (EditText) findViewById(R.id.inAC);
EditText fort = (EditText) findViewById(R.id.inFort);
EditText reflex = (EditText) findViewById(R.id.inReflex);
EditText will = (EditText) findViewById(R.id.inWill);
CheckBox rollInit = (CheckBox) findViewById(R.id.chkRoll);
//variables for various checks
entriesOk=false;
needToRoll=false;
if(characterName.getText().length() > 0)
{
holdCharacterName = characterName.getText().toString();
if(hp.getText().length() > 0)
{
holdHp = hp.getText().toString();
if(initBonus.getText().length() > 0)
{
holdInitBonus = initBonus.getText().toString();
if(ac.getText().length() > 0)
{
holdAc = ac.getText().toString();
if(fort.getText().length() > 0)
{
holdFort = fort.getText().toString();
if(reflex.getText().length() > 0)
{
holdReflex = reflex.getText().toString();
if(will.getText().length() > 0)
{
holdWill = will.getText().toString();
if(tempHp.getText().length() == 0)
{
holdTempHp = "0";
tempHp.setText("0");
}
else
{
holdTempHp = tempHp.getText().toString();
}
if(rollInit.isChecked())
{
entriesOk = true;
needToRoll = true;
}
else
{
if(initRoll.getText().length() > 0)
{
holdInitRoll = initRoll.getText().toString();
entriesOk = true;
}
}
}
}
}
}
}
}
}
btnLogic();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton)
{
//do nothing
}
}).show();
addView.findViewById(R.id.inHP) instead of findViewById(R.id.inHP)
The problem is that you are calling addView.findViewById inside your onClick event, the onclick doesn't really happen until somebody clicks on it, I'll suggest that you make addView class member variable so that it can persist in memory. That'll solve your problem.

Categories

Resources