I am here to seek some help with my code which i am facing a dead end road with. I'm trying to pass values from screen1.java using Intent to screen2.java. Passing the values is fine and I managed to get through it; however, when I check using if statement the program crash down. Here are my files, plzzzzzzzzzzz help
screen1.java
package test.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class screen1 extends Activity
{
static String strKey = "Hello";
static final String strValue = "Hello";
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.screen1);
//** button A
Button A = (Button) findViewById(R.id.btnClickA);
A.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
strKey = "NAME";
i.setClassName("packageName", "packageName.IntentClass");
String term = "Hello";
i.putExtra("packageName.term", term);
//i.putExtra(strKey, strValue);
startActivity(i);
}
});
//**
//** button B
Button B = (Button) findViewById(R.id.btnClickB);
B.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen3.class);
startActivity(i);
}
});
//**
}
}
screen2.java
package test.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class screen2 extends Activity
{
public void onCreate(Bundle icicle)
{
Bundle extras = getIntent().getExtras();
String term = extras.getString("packageName.term");
System.out.println("--- Name is -->"+ term);
if(term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name")){
super.onCreate(icicle);
setContentView(R.layout.screen3);
Button b = (Button) findViewById(R.id.btnClick3);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
} else {
super.onCreate(icicle);
setContentView(R.layout.screen2);
Button b = (Button) findViewById(R.id.btnClick2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
// DOES NOT WORK !!!!!!!!!
System.out.println("--- Name is -->"+ term);
}
}
Layouts:
screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in the first Screen"
/>
<Button
android:id ="#+id/btnClickA"
android:background="#drawable/sleep0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Screen 2"
/>
<Button
android:id ="#+id/btnClickB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Screen 3"
/>
<ImageView android:id="#+id/icon" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:src="#drawable/icon"
android:layout_gravity="center"/>
</LinearLayout>
screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in Screen 2"
/>
<Button
android:id ="#+id/btnClick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
/>
</LinearLayout>
screen3.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- ScrollView: will allow the layout to be scroll Up/Down and Left/right -->
<ScrollView
android:id="#+id/widget54"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="You are in Screen 3"
/>
<Button
android:id ="#+id/btnClick3"
android:background="#drawable/sss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close"
/>
<ImageView android:id="#+id/sssq" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:src="#drawable/sssq"
android:layout_gravity="center" />
</LinearLayout>
</ScrollView>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.android">
<application android:icon="#drawable/icon">
<activity android:name="screen1"
android:label="SCREEN 1">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="screen2"
android:label="SCREEN 2">
</activity>
<activity android:name="screen3"
android:label="SCREEN 3">
</activity>
</application>
</manifest>
=====
The error is caused by these lines of code in screen2.java:
if(term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name")){
super.onCreate(icicle);
setContentView(R.layout.screen3);
Button b = (Button) findViewById(R.id.btnClick3);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
else {
super.onCreate(icicle);
setContentView(R.layout.screen2);
Button b = (Button) findViewById(R.id.btnClick2);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
**notice if I get rid of the entire IF statement and go with only the ELSE the program works fine.
i.setClassName("packageName", "packageName.IntentClass") is not required, if you are passing information to Intent constructor. Btw Is your Screen3 call working fine ?
You're passing the data incorrectly, try this:
public void onClick(View arg0) {
Intent i = new Intent(screen1.this, screen2.class);
strKey = "NAME";
//NO NEED FOR THIS, REMOVE -> i.setClassName("packageName", "packageName.IntentClass");
String term = "Hello";
//Add your string to a bundle:
Bundle b = new Bundle();
b.putString("packageName.term", term);
//Then add the bundle to your intent
i.putExtra(b);
//i.putExtra(strKey, strValue);
startActivity(i);
}
The way you retrieve it in screen2 activity is fine, I would just check if term is null:
if(term != null && (term.equalsIgnoreCase("Hello") || term.equalsIgnoreCase("Name"))){
...
Related
I am very new to coding, this being only my 2nd semester in community college and therefore I have a lot to learn.
In my Android Development course, I have been tasked to use the program I've created from the Android Development lesson here - https://developer.android.com/training/basics/firstapp/starting-activity#java - as the basis for a new program which instead has 2 textboxes and which displays the content the user inputs into those textboxes, onto the display screen, by clicking the 1 send button.
I am stuck.
I've been at this since last Tuesday when the challenge was issued and I've been Google searching, DuckDuckGo searching, searching through stackoverflow, searching through here - https://developer.android.com/docs/ - and I've tried many different approaches but I can't seem to find out how to accomplish this.
!!For the record: I'm not asking someone to code the program/app for me. I'm simply trying to understand how to get this 1 button to send 2 textbox contents to the display - I want to learn!!
//* activity_main.xml */
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/ColorActivity"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:layout_toRightOf="#+id/Number"
android:hint="edit_name_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_below="#+id/Name"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_centerHorizontal="true"
android:text="button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Number" />
<EditText
android:id="#+id/Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:hint="edit_phone_message"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Name" />
</RelativeLayout>
//* DisplayMessge.java */
package com.example.testapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.example.testapp.R;
public class DisplayMessage extends Activity {
private TextView name;
private TextView number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
name = findViewById(R.id.NameText);
number = findViewById(R.id.NumberText);
Intent getText = getIntent();
String TheName = getText.getStringExtra("Name");
String TheNumber = getText.getStringExtra("Number");
name.setText(TheName);
number.setText(TheNumber);
}
}
//* MainActivity.java */
package com.example.testapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.testapp.DisplayMessage;
import com.example.testapp.R;
public class MainActivity extends AppCompatActivity {
Button button;
private EditText name;
private EditText number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization of the EditText and the Button
name = (EditText) findViewById(R.id.Name);
number = (EditText) findViewById(R.id.Number);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);
String mName = name.getText().toString();
String mNumber = number.getText().toString();
//Checking if the Entries are empty
if (mName != null && mNumber != null) {
intent.putExtra("Name", mName);
intent.putExtra("Number", mNumber);
startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Text Entries Missing", Toast.LENGTH_SHORT).show();
}
}
});
}
}
//* AndroidManifest.xml */
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayMessage">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
</intent-filter>
</activity>
</application>
</manifest>
//* display_activity.xml */
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="#+id/NameText"
android:layout_weight="1"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/NumberText"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
You have to implement the onClick function and send the intent from there.
public class MainActivity extends AppCompatActivity {
Button button;
private EditText name;
private EditText number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialization of the EditText and the Button
name = (EditText) findViewById(R.id.Name);
number = (EditText) findViewById(R.id.Number);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), DisplayMessage.class);
String mName = name.getText().toString();
String mNumber = number.getText().toString();
//Checking if the Entries are empty
if(mName!=null&&mNumber!=null) {
intent.putExtra("Name", mName);
intent.putExtra("Number", mNumber);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(),"Text Entries Missing",Toast.LENGTH_SHORT).show();
}
}
});
and the Display Class:
public class DisplayMessage extends Activity {
private TextView name;
private TextView number;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_activity);
name = findViewById(R.id.NameText);
number = findViewById(R.id.NumberText);
Intent getText = getIntent();
String TheName =getText.getStringExtra("Name");
String TheNumber = getText.getStringExtra("Number");
name.setText(TheName);
number.setText(TheNumber);
}
Also don't forget to add your displayActivity to the AndroidManifest.xml
<activity android:name=".DisplayMessage">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
</intent-filter>
</activity>
Now you have to create a second user interface for the DisplayMessageActivity go to res/layout, right click on the layout folder and create a new layout named display_activity. This is my code for the display_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
<TextView
android:layout_gravity="center"
android:id="#+id/NameText"
android:layout_weight="1"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/NumberText"
android:layout_gravity="center"
android:textSize="18sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Lastly this is the activity_main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/ColorActivity"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="#+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:layout_toRightOf="#+id/Number"
android:hint="edit_name_message"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:layout_below="#+id/Name"
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_centerHorizontal="true"
android:text="button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Number" />
<EditText
android:id="#+id/Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:ems="10"
android:gravity="center"
android:hint="edit_phone_message"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Name" />
</RelativeLayout>
Just reduce your two functions (snedNumber and sendName) to only one function. Inside this function you get the text of both textfields and put them into your intentextra. then simply start your new activity
Do it like this
public void sendInfo(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText name = (EditText) findViewById(R.id.Name);
String message = name.getText().toString();
EditText number = (EditText) findViewById(R.id.Number);
String message2 = number.getText().toString();
intent.putExtra("firstString", message);
intent.putExtra("secondString", message2);
startActivity(intent);
}
And in your DisplayMessageActivity class onCreate method get it like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
Bundle extra = getIntent().getExtras();
if (extra != null){
String message = extra.getString("firstString");
String message2 = extra.getString("secondString");
}
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I can not understand why my application does not open a new activity on a button click.I have a button in my layout file(id is showButton),I need to open another activity when I click on that button.But it does not work and my application will crash.therefore I added a try catch block and get the android monitor result as following.
activity_place_picker.xml
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".PlacePickerActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Launch Places API Picker"
android:id="#+id/pickerButton"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Place Holder"
android:id="#+id/saveButton"
android:layout_below="#+id/pickerButton"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Place Holder"
android:id="#+id/showButton"
android:layout_below="#+id/saveButton"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView3"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/poweredBy"
android:src="#drawable/powered_by_google_light"/>
This is Place picker class where I have included the onClick listner to the button to open a new activity.
PlacePickerActivity
public class PlacePickerActivity extends AppCompatActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mName;
private TextView mAddress;
private TextView mAttributions;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
Context ctx;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker);
mName = (TextView) findViewById(R.id.textView);
mAddress = (TextView) findViewById(R.id.textView2);
mAttributions = (TextView) findViewById(R.id.textView3);
Button showButton = (Button) findViewById(R.id.showButton);
Button pickerButton = (Button) findViewById(R.id.pickerButton);
pickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
Intent intent = intentBuilder.build(PlacePickerActivity.this);
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException
| GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
showButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
Intent i = new Intent(ctx, PlacesActivity.class);
ctx.startActivity(i);
}catch (Exception e){
e.printStackTrace();
}
}
});
}
#Override
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST
&& resultCode == Activity.RESULT_OK) {
//Extracting place information from the API
final Place place = PlacePicker.getPlace(this, data);
final String place_Id = place.getId().toString();
final String place_Name = place.getName().toString();
final String place_Address = place.getAddress().toString();
final String place_PhoneNumber = place.getPhoneNumber().toString();
final String place_Website = place.getWebsiteUri().toString();
//Get rating as a float value and converting to string
final float rating = place.getRating();
final String place_Rating = String.valueOf(rating);
final String place_LatLng = place.getLatLng().toString();
final String function_Save = "save_Place";
final String function_Show = "show_Place";
String attributions = (String) place.getAttributions();
if (attributions == null) {
attributions = "";
}
mName.setText(place_Name);
mAddress.setText(place_Address);
mAttributions.setText(Html.fromHtml(attributions));
//Accessing the save button and show button in the view and making it visible after selecting a place
final View save = findViewById(R.id.saveButton);
save.setVisibility(View.VISIBLE);
//Passing the Extracted place details to the background worker class
save.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
BackgroundWorker backgroundWorker = new BackgroundWorker(PlacePickerActivity.this);
backgroundWorker.execute(function_Save,place_Id,place_Name,place_Address,place_PhoneNumber,place_Website,place_Rating,place_LatLng);
}
});
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
PlacesActivity.java
package com.truiton.placepicker;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class PlacesActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_places);
}
}
activity_places.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.truiton.placepicker.PlacesActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello world"
android:id="#+id/textView" />
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.truiton.placepicker">
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBpIGvJfE8eKhoEFCMqV8NrFkWRjTAnNyQ" />
<activity
android:name=".PlacePickerActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlacesActivity"></activity>
</application>
</manifest>
Android Monitor result when the button clicks
Android Monitor result
Edited : This is the crash log
Crash log
can anyone help me with this?
Write the class name PlacePickerActivity in the intent, your method should be
showButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
try {
Intent i = new Intent(PlacePickerActivity.this , PlacesActivity.class);
ctx.startActivity(i);
}catch (Exception e){
e.printStackTrace();
}
}
});
I found the answer for the question.
This
link helped me to find the answer for the problem. In PlacePickerActivity I have just declare Context ctx;and it does not contain any primitive value. But I have use the it as an argument to open a new activity which will give me a NullPointerException.So instead of
showButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(ctx, PlacesActivity.class);
ctx.startActivity(i);
}
});
I used
showButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent i = new Intent(PlacePickerActivity.this, PlacesActivity.class);
PlacePickerActivity.this.startActivity(i);
}
});
And this fixed my problem.Thank you for every one.
I want from user to input his name in one EditText widget, then to input his weight in other EditText widget, and after that to proceed that data from one activity to another one.
Weight input should later be included in one formula, and name input should be settled in TextView widget with result of the formula.
But, when i start my app, at the end Android Studio shows me NullPointerException. I suppose that err is somewhere in data sharing between activities.
So, here is the code. If you can, pls help me :)
ActivityOne (name of activity is InformacijeM) XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_informacije_m"
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.alkometar.InformacijeM"
android:background="#drawable/activitybackground">
<TextView
android:text="Ukucaj svoje ime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:textSize="25dp"
android:textColor="#android:color/black"
android:textStyle="bold"
android:id="#+id/UkucajIme" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/strelamanja"
android:id="#+id/strelica"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/ImeInput"
android:layout_marginTop="12dp"
android:layout_below="#+id/UkucajIme"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="#+id/MasaInput"
android:layout_marginBottom="53dp"
android:layout_above="#+id/strelica"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<TextView
android:text="Ukucaj svoju masu u kg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/UkucajMasu"
android:textSize="25dp"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginBottom="30dp"
android:layout_above="#+id/MasaInput"
android:layout_centerHorizontal="true" />
</RelativeLayout>
ActivityOne (InformacijeM) Java file:
package com.example.alkometar;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class InformacijeM extends AppCompatActivity {
EditText ImeTxt, MasaTxt ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_informacije_m);
// INICIRANJE VREDNOSTI
EditText ImeTxt = (EditText)findViewById(R.id.ImeInput);
final String str1 = ImeTxt.getText().toString();
EditText MasaTxt = (EditText)findViewById(R.id.MasaInput);
final String str2 = MasaTxt.getText().toString();
ImageButton btn = (ImageButton)findViewById(R.id.strelica);
// PREBACIVANJE AKTIVITIJA
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
startActivity(intent);
}
});
}}
ActivityTwo (Name of activity is RezultatM) XML file:
<?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:id="#+id/activity_rezultat_m"
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.alkometar.RezultatM"
android:background="#drawable/activitybackground">
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="97dp"
android:id="#+id/rezultatTekst" />
<Button
android:text="Rezultat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:id="#+id/button" />
</RelativeLayout>
ActivityTwo (RezultatM) Java file:
package com.example.alkometar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class RezultatM extends AppCompatActivity {
TextView rezultat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultat_m);
rezultat = (TextView) findViewById(R.id.rezultatTekst);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Bundle extras = getIntent().getExtras();
{
String str1 = extras.getString("keyIme");
String str2 = extras.getString("keyMasa");
double masad = Double.valueOf(str2);
double nekibroj = 0.8;
double result = masad * nekibroj;
rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result);
}
};
});
}}
Strings.xml file:
<resources>
<string name="app_name">Alkometar</string>
<string name="ImeInput">ImeInput</string>
<string name="MasaInput">MasaInput</string>
</resources>
AndroidManifest XML file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alkometar">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Pol">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".InformacijeM" />
<activity android:name=".StanjeM" />
<activity android:name=".InformacijeZ" />
<activity android:name=".StanjeZ" />
<activity android:name=".PijemM" />
<activity android:name=".RezultatM"></activity>
</application>
</manifest>
And here is the exception in logCat:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.alkometar, PID: 30732
java.lang.NullPointerException
at com.example.alkometar.RezultatM$1.onClick(RezultatM.java:28)
at android.view.View.performClick(View.java:4463)
at android.view.View$PerformClick.run(View.java:18770)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
Thank you very much once again! :)))
Change your Activity Two class to:
public class RezultatM extends AppCompatActivity {
TextView rezultat;
String str1,str2;
double masad;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rezultat_m);
rezultat = (TextView) findViewById(R.id.rezultatTekst);
Button button = (Button) findViewById(R.id.button);
//Intent intent = getIntent();
//str1 = intent.getStringExtra("keyIme");
//str2 = intent.getStringExtra("keyMasa");
//getting values from shared preferences
SharedPreferences shared = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
str1=shared.getString("keyIme", "");
str2=shared.getString("keyMasa", "");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if(str2!=null){
masad = Double.parseDouble(str2);
}
double nekibroj = 0.8;
double result = masad * nekibroj;
rezultat.setText(str1 + "Nivo alkohola u tvojoj krvi je:" + result);
};
});
}}
Also in your Activity one you are starting wrong intent
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
// startActivity(intent); //here you are starting another class..check it
//change it to
startActivity(intent1);
change your InformacijeM.java class:
public class InformacijeM extends AppCompatActivity {
EditText ImeTxt, MasaTxt ;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_informacije_m);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// INICIRANJE VREDNOSTI
EditText ImeTxt = (EditText)findViewById(R.id.ImeInput);
final String str1 = ImeTxt.getText().toString();
EditText MasaTxt = (EditText)findViewById(R.id.MasaInput);
final String str2 = MasaTxt.getText().toString();
save_data(str1,str2);
ImageButton btn = (ImageButton)findViewById(R.id.strelica);
// PREBACIVANJE AKTIVITIJA
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent1 = new Intent(InformacijeM.this, RezultatM.class);
intent1.putExtra("keyIme", str1);
intent1.putExtra("keyMasa", str2);
Intent intent = new Intent(InformacijeM.this, StanjeM.class);
startActivity(intent);
}
});
}
public void save_data(String s1,String s2){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("keyIme", s1);
editor.putString("keyMasa", s2);
editor.commit();
}
}
two created screens that has two button, each button links to the other, so two screens with two buttons, I don't know how to add a link to the third screen, eclipse was running it but no use
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to another screen" />
</LinearLayout>
main2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="(main2.xml) how do get to the 3rd" />
</LinearLayout>
AppActivity.java
package com.mkyong.android;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, App2Activity.class);
startActivity(intent);
}
});
}
}
App2Activity.java
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(App2Activity.this, App3Activity.class);
startActivity(intent);
}
});
}
}
Make sure you have registered the App3Activity in AndroidManifest.
EDIT
Here is the complete solution, just copy paste into respective classes
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 1 (main.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to to go second screen" />
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 2 (main2.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me to go to third screen" />
</LinearLayout>
main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I'm screen 3 (main3.xml)"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
AppActivity
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class AppActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AppActivity.this, App2Activity.class);
startActivity(intent);
}
});
}
}
App2Activity
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(App2Activity.this, App3Activity.class);
startActivity(intent);
}
});
}
}
App3Activity
public class AppActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your_package_name"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".App2Activity" >
</activity>
<activity
android:name=".App3Activity" >
</activity>
</application>
</manifest>
I am not able to switch between activities, when I click on the button2, the application crashes down.
MainActivity.Java
<pre>package com.example.againtry;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Declare our View Variables and assign them the Views from the layout file
final TextView answerLabel = (TextView) findViewById(R.id.textView1);
Button getAnswerButton = (Button) findViewById(R.id.button1);
getAnswerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// The Button was clicked, so the update the answer label with an answer
String answer = "";
// Randomly select one of three answers: Yes, No or May be
// Update the label with our dynamic answer
Random randomGenerator = new Random(); // Construct a new Random number Generator
int randomNumber = randomGenerator.nextInt(3);
answer = Integer.toString(randomNumber);
// update the label with our dynamic answer
answerLabel.setText(answer);
}});
Button btnSimple= (Button) findViewById(R.id.button2);
btnSimple.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// From One Activity to another
Intent intent = new Intent(MainActivity.this, SendMessageActivity.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;
}}</pre>
This is my Second Activity
SendMessageActivity.Java
<pre>package com.example.againtry;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
final EditText editSMS = (EditText) findViewById(R.id.editText1);
final EditText editPhoneNum = (EditText) findViewById(R.id.phoneNum);
Button getSendButton = (Button) findViewById(R.id.button1);
getSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String phoneNo = editPhoneNum.getText().toString();
String sms = editSMS.getText().toString();
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.send_message, menu);
return true;
}
}</pre>
Manifest
<pre> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.againtry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:maxSdkVersion="10" android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:maxSdkVersion="10" android:name="android.permission.SEND_SMS"/>
<uses-permission android:maxSdkVersion="10" android:name="android.permission.READ_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.againtry.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.againtry.SendMessageActivity"
android:label="#string/title_activity_send_message" >
</activity>
</application>
</manifest></pre>
activity_main<pre>
<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:gravity="bottom"
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=".MainActivity" >
<Button
android:id="#+id/button1"
style="#android:style/ButtonBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="70dp"
android:text="Enlighten Me!" />
<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_marginTop="177dp"
android:textSize="32sp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignLeft="#+id/button1"
android:layout_marginBottom="44dp"
android:text="Message" />
</RelativeLayout></pre>
send_message.xml<pre>
<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=".SendMessageActivity" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Enter your message" >
<requestFocus />
</EditText>
<Button
android:id="#+id/sendButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_alignParentRight="true"
android:text="#string/button_send" />
<TextView
android:id="#+id/messageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/editText1"
android:layout_alignParentTop="true"
android:layout_marginTop="77dp"
android:text="TextView" />
<EditText
android:id="#+id/phoneNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/messageView1"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:text="Enter Phone Number" android:inputType="number"/>
</RelativeLayout></pre>
In the layout of SendMessageActivity (i.e send_message.xml), you have no buttons with id "button1".
So when doing :
Button getSendButton = (Button) findViewById(R.id.button1);
findViewById returns null and hence getSendButton.setOnClickListener(/***/); throws a NullPointerException.
It should be :
Button getSendButton = (Button) findViewById(R.id.sendButton);
You have to assign onclick to a particular button which i don't see in your activity_main xml.
just add onclick event to your button and remove onclicklistener and it should work.
to add onclick listener you can go to designer page and in properties you will find a tag as onclick.Just assign the onlick method to that tag using dropdown.
there is mistake in mapping of control in SendMessageActivity
public class SendMessageActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_message);
final EditText editSMS = (EditText) findViewById(R.id.editText1);
final EditText editPhoneNum = (EditText) findViewById(R.id.phoneNum);
Button getSendButton = (Button) findViewById(R.id.button1);
instead of button1 it should be R.id.sendButton