Reputation: 1
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>
Upvotes: 0
Views: 470
Reputation: 1073
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
Upvotes: 0
Reputation: 2106
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.
Upvotes: 0
Reputation: 93842
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);
Upvotes: 1