我无法在活动之间切换,当我点击button2
时,应用程序崩溃了。
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>
这是我的第二项活动
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>
答案 0 :(得分:1)
在SendMessageActivity
(即send_message.xml
)的布局中,您没有ID为“button1
”的按钮。
所以在做的时候:
Button getSendButton = (Button) findViewById(R.id.button1);
findViewById
返回null
,因此getSendButton.setOnClickListener(/***/);
会抛出NullPointerException
。
应该是:
Button getSendButton = (Button) findViewById(R.id.sendButton);
答案 1 :(得分:0)
您必须将onclick指定给我在您的activity_main xml中看不到的特定按钮。
只需将onclick事件添加到按钮并删除onclicklistener即可。
要添加onclick侦听器,您可以转到设计器页面,在属性中,您将找到一个标记为onclick.Just使用下拉列表将onlick方法分配给该标记。
答案 2 :(得分:0)
SendMessageActivity中的控件映射存在错误
<% @students.each do |student| %>
<div style="height: <%= student.height %>px; width: <%= student.width %>px;">
<!-- other student stuff -->
</div>
<% end %>
而不是 button1 它应该是R.id. sendButton